-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
error.c
542 lines (454 loc) · 12.1 KB
/
error.c
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*!
* \file lib/gis/error.c
*
* \brief GIS Library - Error messages functions
*
* (C) 1999-2011 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
* \author USACERL and many others
*/
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <unistd.h>
#include <time.h>
#include <stdarg.h>
#include <sys/types.h>
#include <grass/glocale.h>
#include <grass/gis.h>
#include "gis_local_proto.h"
/*!
* \def MSG
*
* \brief A message
*/
#define MSG 0
/*!
* \def WARN
*
* \brief A warning message
*/
#define WARN 1
/*!
* \def ERR
*
* \brief A fatal error message
*/
#define ERR 2
/* static int (*error)() = 0; */
static int (*ext_error)(const char *, int); /* Roger Bivand 17 June 2000 */
static int no_warn = FALSE;
static int no_sleep = TRUE;
static int grass_info_format;
static char *logfile;
static char *prefix_std[3];
static struct Counter message_id;
static int print_word(FILE *, char **, int *, const int);
static void print_sentence(FILE *, const int, const char *);
static void print_error(const char *, const int);
static void mail_msg(const char *, int);
static int write_error(const char *, int, time_t, const char *);
static void log_error(const char *, int);
static int fatal_longjmp;
static jmp_buf fatal_jmp_buf;
jmp_buf *G_fatal_longjmp(int enable)
{
fatal_longjmp = enable;
return &fatal_jmp_buf;
}
static void vfprint_error(int type, const char *template, va_list ap)
{
char *buffer = NULL;
G_vasprintf(&buffer, template, ap);
print_error(buffer, type);
G_free(buffer);
}
/*!
* \brief Print a message to stderr
*
* The output format depends on environment variable GRASS_MESSAGE_FORMAT
*
* \param msg string (cannot be NULL)
*/
void G_message(const char *msg, ...)
{
if (G_verbose() >= G_verbose_std()) {
va_list ap;
va_start(ap, msg);
vfprint_error(MSG, msg, ap);
va_end(ap);
}
}
/*!
* \brief Print a message to stderr but only if module is in verbose mode
*
* The output format depends on environment variables
* GRASS_MESSAGE_FORMAT and GRASS_VERBOSE
*
* \param msg string (cannot be NULL)
*/
void G_verbose_message(const char *msg, ...)
{
if (G_verbose() > G_verbose_std()) {
va_list ap;
va_start(ap, msg);
vfprint_error(MSG, msg, ap);
va_end(ap);
}
}
/*!
* \brief Print a message to stderr even in brief mode (verbosity=1)
*
* Usually just G_percent()/G_clicker() would be shown at this level.
* This allows important non-error/warning messages to display as well.
*
* The output format depends on environment variables
* GRASS_MESSAGE_FORMAT and GRASS_VERBOSE
*
* \param msg string (cannot be NULL)
*/
void G_important_message(const char *msg, ...)
{
if (G_verbose() > G_verbose_min()) {
va_list ap;
va_start(ap, msg);
vfprint_error(MSG, msg, ap);
va_end(ap);
}
}
/*!
* \brief Print a fatal error message to stderr
*
* The output format depends on environment variable
* GRASS_MESSAGE_FORMAT
*
* 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).
*
* \param msg string (cannot be NULL)
* \return Terminates with an exit status of EXIT_FAILURE if no external
* routine is specified by G_set_error_routine()
*/
void G_fatal_error(const char *msg, ...)
{
static int busy;
va_list ap;
if (busy)
exit(EXIT_FAILURE);
busy = 1;
if (G_verbose() > -1) {
va_start(ap, msg);
vfprint_error(ERR, msg, ap);
va_end(ap);
}
if (fatal_longjmp) {
busy = 0;
longjmp(fatal_jmp_buf, 1);
}
G__call_error_handlers();
/* Raise SIGABRT, useful for debugging only.
* Type "export GRASS_ABORT_ON_ERROR=1"
* to enable this feature using bash.
*/
if (getenv("GRASS_ABORT_ON_ERROR"))
abort();
exit(EXIT_FAILURE);
}
/*!
* \brief Print a warning message to stderr
*
* The output format depends on environment variable
* GRASS_MESSAGE_FORMAT
*
* A warning message can be suppressed by G_suppress_warnings()
*
* \param msg string (cannot be NULL)
*
* \return
*/
void G_warning(const char *msg, ...)
{
va_list ap;
if (no_warn || G_verbose() < 0)
return;
va_start(ap, msg);
vfprint_error(WARN, msg, ap);
va_end(ap);
}
/*!
* \brief Suppress printing a warning message to stderr
*
* \param flag a warning message will be suppressed if non-zero value is given
*
* \return previous flag
*/
int G_suppress_warnings(int flag)
{
int prev;
prev = no_warn;
no_warn = flag;
return prev;
}
/*!
* \brief Turn on/off no_sleep flag
*
* If <em>flag</em> is 0, then no pause will occur after printing an
* error or warning message. Otherwise the pause will occur.
*
* \param flag if non-zero/zero value is given G_sleep() will be
* activated/deactivated
*
* \return previous no_sleep value
*/
int G_sleep_on_error(int flag)
{
int prev;
prev = !no_sleep;
no_sleep = !flag;
return prev;
}
/*!
* \brief Establishes error_routine as the routine that will handle
* the printing of subsequent error messages.
*
* \param error_routine routine will be called like this: error_routine(msg,
* fatal)
*
* \return
*/
void G_set_error_routine(int (*error_routine)(const char *, int))
{
ext_error = error_routine; /* Roger Bivand 17 June 2000 */
}
/*!
* \brief 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
*
* \return 0
*/
void G_unset_error_routine(void)
{
ext_error = 0; /* Roger Bivand 17 June 2000 */
}
/* Print info to stderr and optionally to log file and optionally send mail */
static void print_error(const char *msg, const int type)
{
int fatal, format;
if (type == ERR)
fatal = TRUE;
else /* WARN */
fatal = FALSE;
if ((type == MSG || type == WARN || type == ERR) &&
ext_error) { /* Function defined by application */
ext_error(msg, fatal);
}
else {
G_init_logging();
format = G_info_format();
if (type == WARN || type == ERR)
log_error(msg, fatal);
if (format == G_INFO_FORMAT_SILENT)
return;
if (format != G_INFO_FORMAT_GUI) {
if (format != G_INFO_FORMAT_PLAIN) {
char *w;
int len, lead;
fprintf(stderr, "%s", prefix_std[type]);
len = lead = strlen(prefix_std[type]);
w = (char *)msg;
while (print_word(stderr, &w, &len, lead))
;
}
else {
fprintf(stderr, "%s%s\n", prefix_std[type], msg);
}
if ((type != MSG) && isatty(fileno(stderr)) &&
(G_info_format() == G_INFO_FORMAT_STANDARD)) { /* Bell */
fprintf(stderr, "\7");
fflush(stderr);
if (!no_sleep)
G_sleep(5);
}
else if ((type == WARN || type == ERR) &&
getenv("GRASS_ERROR_MAIL")) { /* Mail */
mail_msg(msg, fatal);
}
}
else { /* GUI */
print_sentence(stderr, type, msg);
}
}
}
static void log_error(const char *msg, int fatal)
{
char cwd[GPATH_MAX];
time_t clock;
const char *gisbase;
/* get time */
clock = time(NULL);
/* get current working directory */
if (getcwd(cwd, sizeof(cwd)) == NULL)
sprintf(cwd, "%s", _("unknown"));
/* write the error log file */
if ((gisbase = G_gisbase()))
write_error(msg, fatal, clock, cwd);
}
void G_init_logging(void)
{
static int initialized;
char *fstr;
if (G_is_initialized(&initialized))
return;
G_init_counter(&message_id, 1);
prefix_std[0] = "";
prefix_std[1] = _("WARNING: ");
prefix_std[2] = _("ERROR: ");
logfile = getenv("GIS_ERROR_LOG");
if (!logfile) {
char buf[GPATH_MAX];
snprintf(buf, GPATH_MAX, "%s/GIS_ERROR_LOG", G__home());
logfile = G_store(buf);
}
fstr = getenv("GRASS_MESSAGE_FORMAT");
if (fstr && G_strcasecmp(fstr, "gui") == 0)
grass_info_format = G_INFO_FORMAT_GUI;
else if (fstr && G_strcasecmp(fstr, "silent") == 0)
grass_info_format = G_INFO_FORMAT_SILENT;
else if (fstr && G_strcasecmp(fstr, "plain") == 0)
grass_info_format = G_INFO_FORMAT_PLAIN;
else
grass_info_format = G_INFO_FORMAT_STANDARD;
G_initialize_done(&initialized);
}
/* Write a message to the log file */
static int write_error(const char *msg, int fatal, time_t clock,
const char *cwd)
{
FILE *log;
G_init_logging();
log = fopen(logfile, "r");
if (!log)
/* GIS_ERROR_LOG file is not readable or does not exist */
return 1;
log = freopen(logfile, "a", log);
if (!log)
/* the user doesn't have write permission */
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;
}
/* Mail a message */
static void mail_msg(const char *msg, int fatal)
{
struct Popen mail;
FILE *fp = G_open_mail(&mail);
if (fp)
fprintf(fp, "GIS %s: %s\n", fatal ? "ERROR" : "WARNING", msg);
G_close_mail(&mail);
}
/* Print one word, new line if necessary */
static int print_word(FILE *fd, char **word, int *len, const 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;
fwrite(w, 1, wlen, fd);
w += wlen;
*word = w;
return 1;
}
/* Print one message, prefix inserted before each new line */
static void print_sentence(FILE *fd, const int type, const char *msg)
{
char prefix[100];
const char *start;
int id = G_counter_next(&message_id);
switch (type) {
case MSG:
sprintf(prefix, "GRASS_INFO_MESSAGE(%d,%d): ", getpid(), id);
break;
case WARN:
sprintf(prefix, "GRASS_INFO_WARNING(%d,%d): ", getpid(), id);
break;
case ERR:
sprintf(prefix, "GRASS_INFO_ERROR(%d,%d): ", getpid(), id);
break;
}
start = msg;
fprintf(stderr, "\n");
while (*start != '\0') {
const char *next = start;
fprintf(fd, "%s", prefix);
while (*next != '\0') {
next++;
if (*next == '\n') {
next++;
break;
}
}
fwrite(start, 1, next - start, fd);
fprintf(fd, "\n");
start = next;
}
fprintf(stderr, "GRASS_INFO_END(%d,%d)\n", getpid(), id);
}
/*!
* \brief Get current message format
*
* Maybe set to either "standard" or "gui" (normally GRASS takes care)
*
* \return grass_info_format value
*/
int G_info_format(void)
{
G_init_logging();
return grass_info_format;
}