File: error.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (403 lines) | stat: -rw-r--r-- 9,082 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 **********************************************************************
 *
 * G_fatal_error (msg)
 *      char *msg          One line error message
 *
 * G_warning (msg)
 *      char *msg
 *
 *   Gives the message, msg, to the user.  Also print a message to 
 *   $GISBASE/GIS_ERROR_LOG if the file exists and is writeable; 
 *   and to the file $HOME in the users home directory if $HOME is set.
 *   G_warning() returns, which G_fatal_error() exits.
 *
 *   note:  By default, the message is handled by an internal routine
 *      which prints the message to the screen.   Using G_set_error_routine()
 *      the programmer can have the message handled by another routine.
 *      This is especially useful if the message should go to a particular
 *      location on the screen when using curses or to a location on
 *      a graphics device (monitor).
 *          
 **********************************************************************
 * G_set_error_routine (error_routine)
 *      int (*error_routine)()
 *
 *   Establishes error_routine as the routine that will handle 
 *   the printing of subsequent error messages. error_routine
 *   will be called like this:
 *      error_routine(msg, fatal)  
 *         char *msg ;
 **********************************************************************
 * G_unset_error_routine ()
 *
 *   After this call subsequent error messages will be handled in the
 *   default method.  Error messages are printed directly to the
 *   screen:
 *           ERROR: message
 *   -or-    WARNING: message
 *
 **********************************************************************/
/*
     Throughout the code the references to these routines attempt to
	send format strings and arguments.  It seems that we expect
	these to handle varargs, so now they do.    7-Mar-1999
		Bill Hughes
*/

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdarg.h>
#include <sys/types.h>
#include "glocale.h"
#include "gis.h"

#define MSG  0
#define WARN 1
#define ERR  2

/* static int (*error)() = 0; */
static int (*ext_error)() = 0; /* Roger Bivand 17 June 2000 */
static int no_warn = 0;
static int no_sleep = 1;
static int message_id = 1;

extern char *getenv();
static int print_word ( FILE *,char **,int *,int);
static void print_sentence ( FILE *, int type, char *);
static int print_error(char *,int);
static int mail_msg (char *,int);
static int write_error(char *, int,char *,long,char *);
static int log_error (char *,int);

/*!
 * \brief Print a message to stderr
 *
 * The output format depends on enviroment variable GRASS_MESSAGE_FORMAT
*/
void G_message (char *msg,...)
{
    char buffer[2000];  /* G_asprintf does not work */
    va_list ap;

    va_start(ap, msg);
    vsprintf(buffer,msg,ap);
    va_end(ap);

    print_error (buffer,MSG);
}

int G_fatal_error ( char *msg,...)
{
    char buffer[2000];  /* No novels to the error logs, OK? */
    va_list ap;

    va_start(ap,msg);
    vsprintf(buffer,msg,ap);
    va_end(ap);

    print_error (buffer,ERR);

    if ( ext_error ) return 0; /* do not exit error routine is specified */
    
    exit (1);
}

int G_warning ( char *msg, ...)
{
    char buffer[2000];
    va_list ap;

    if (no_warn) return 0;

    va_start(ap,msg);
    vsprintf(buffer,msg,ap);
    va_end(ap);
    print_error (buffer,WARN);

    return 0;
}

int G_suppress_warnings (int flag)
{
    int prev;

    prev = no_warn;
    no_warn = flag;
    return prev;
}

int G_sleep_on_error (int flag)
{
    int prev;

    prev = !no_sleep;
    no_sleep = !flag;
    return prev;
}

int G_set_error_routine ( int (*error_routine)())
{
    ext_error = error_routine; /* Roger Bivand 17 June 2000 */
    return 0;
}

int G_unset_error_routine ()
{
    ext_error = 0; /* Roger Bivand 17 June 2000 */

    return 0;
}

/* Print info to stderr and optionaly to log file and optionaly send mail */
static int print_error(char *msg,int type)
{
    static char *prefix_std[3];
    int fatal, format;
    
    if ( !prefix_std[0] ) { /* First time: set prefixes  */
        prefix_std[0] = "";
	prefix_std[1] = _("WARNING: ");
	prefix_std[2] = _("ERROR: ");
    }
    
    if ( type == ERR )
	fatal = 1;
    else /* WARN */
	fatal = 0;

    if ( (type == WARN || type == ERR) && ext_error) { /* Function defined by application */
	ext_error (msg, fatal);
    } else {
	char *w;
	int len, lead;

        format = G_info_format();

	if ( format == G_INFO_FORMAT_STANDARD ) {
	    if ( type == WARN || type == ERR ) { 
		log_error (msg, fatal);
	    }

	    fprintf(stderr,"%s", prefix_std[type] );
	    len = lead = strlen ( prefix_std[type] );
	    w = msg;

	    while (print_word(stderr,&w,&len,lead))
		    ;

	    if ( (type != MSG) && isatty(fileno(stderr))) { /* Bell */
		fprintf(stderr,"\7");
		fflush (stderr);
		if (!no_sleep)
		    sleep (5);
	    } else if ( (type == WARN || type == ERR) && getenv("GRASS_ERROR_MAIL")) { /* Mail */
		mail_msg (msg, fatal);
	    }
	} else { /* GUI */
	    print_sentence ( stderr, type, msg );
	}
    }

    return 0;
}

static int log_error ( char *msg,int fatal)
{
    FILE *pwd;
    char cwd[1024];
    long clock;
    char *home;
    char *gisbase;

/* get time */
    clock = time(0);

/* get current working directory */
    sprintf(cwd,"?");
    if ( (pwd = G_popen ("pwd","r")) )
    {
	if (fgets(cwd, sizeof cwd, pwd))
	{
	    char *c;

	    for (c = cwd; *c; c++)
		if (*c == '\n')
		    *c = 0;
	}
	G_pclose (pwd);
    }

/* write the 2 possible error log files */
    if( (gisbase = G_gisbase ()) )
	write_error (msg, fatal, gisbase, clock, cwd);
    home = G__home();
    if (home && gisbase && strcmp (home, gisbase))
	write_error (msg, fatal, home, clock, cwd);


    return 0;
}

static int write_error ( char *msg, int fatal, char *dir, long clock, char *cwd)
{
    char logfile[1000];
    FILE *log;
    char *ctime();

    if (dir == 0 || *dir == 0)
	return 1;
    sprintf (logfile, "%s/GIS_ERROR_LOG", dir) ;

/* logfile must exist and be writeable */
    if (access (logfile, 0) != 0)
	return 1;

    log = fopen (logfile,"a");
    if (!log)
	return 1;

    fprintf(log,"-------------------------------------\n");
    fprintf(log,"%-10s %s\n", "program:", G_program_name());
    fprintf(log,"%-10s %s\n", "user:", G_whoami());
    fprintf(log,"%-10s %s\n", "cwd:", cwd);
    fprintf(log,"%-10s %s\n", "date:", ctime(&clock));
    fprintf(log,"%-10s %s\n", fatal?"error:":"warning:", msg);
    fprintf(log,"-------------------------------------\n");

    fclose (log);

    return 0;
}

static int mail_msg ( char *msg,int fatal)
{
    FILE *mail;
    char command[64];
    char *user;

    user = G_whoami();
    if (user == 0 || *user == 0)
	return 1;

    sprintf (command, "mail '%s'", G_whoami());
    if ( (mail = G_popen (command, "w")) )
    {
	fprintf(mail,"GIS %s: %s\n",fatal?"ERROR":"WARNING",msg);
	G_pclose (mail);
    }

    return 0;
}

/* Print one word, new line if necessary */
static int print_word ( FILE *fd, char **word, int *len, int lead)
{
    int  wlen, start, totlen;
    int  nl;
    char *w,*b;

    start = *len;
    w = *word;

    nl = 0;
    while (*w == ' ' || *w == '\t' || *w == '\n')
	if(*w++ == '\n')
	    nl++;

    wlen = 0;
    for (b = w; *b != 0 && *b != ' ' && *b != '\t' && *b != '\n'; b++)
	wlen++;

    if (wlen == 0)
    {
	fprintf (fd, "\n");
	return 0;
    }

    if ( start > lead ) { /* add space */
	totlen = start + wlen + 1;
    } else {
	totlen = start + wlen; 
    }
    
    if ( nl != 0 || totlen > 75)
    {
	while (--nl > 0)
	    fprintf (fd, "\n");
	fprintf (fd, "\n%*s",lead,"");
	start = lead;
    }

    if ( start > lead ) {
      fprintf (fd, " ");
      start++;
    }

    *len = start + wlen;

    while (wlen-- > 0)
	fprintf (fd, "%c", *w++);

    *word = w;
    return 1;
}

/* Print one message, prefix inserted before each new line */
static void print_sentence ( FILE *fd, int type, char *msg )
{
    char *start;
    static char prefix[100];

    switch ( type ) {
	case MSG: 
    	    sprintf ( prefix, "GRASS_INFO_MESSAGE(%d,%d): ", (int)getpid(), message_id ); 
	    break;
	case WARN:
    	    sprintf ( prefix, "GRASS_INFO_WARNING(%d,%d): ", (int)getpid(), message_id ); 
	    break;
	case ERR:
    	    sprintf ( prefix, "GRASS_INFO_ERROR(%d,%d): ", (int)getpid(), message_id ); 
	    break;
    }

    start = msg;

    fprintf(stderr, "\n" );
    while ( *start != '\0' ) {
	fprintf ( fd, "%s", prefix);

	while ( *start != '\0' ) {
	    fprintf (fd, "%c", *start++);
		
	    if ( *start == '\n' ) {
	        *start++;
		break;
	    }
	}
	
	fprintf (fd, "\n" );
    }
    fprintf(stderr, "GRASS_INFO_END(%d,%d)\n", (int)getpid(), message_id );
    message_id++;
}
    
int G_info_format ( void ) 
{
    static int grass_info_format = -1;
    char    *fstr;
    
    if ( grass_info_format < 0) {
        fstr = getenv( "GRASS_MESSAGE_FORMAT" );

        if ( fstr && G_strcasecmp(fstr,"gui") == 0 )
	    grass_info_format = G_INFO_FORMAT_GUI;
        else
	    grass_info_format = G_INFO_FORMAT_STANDARD;
    }

    return grass_info_format;
}