GRASS Programmer's Manual  6.4.4(2014)-r
lib/gis/error.c
Go to the documentation of this file.
1 
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <time.h>
19 #include <stdarg.h>
20 #include <sys/types.h>
21 #include <grass/glocale.h>
22 #include <grass/gis.h>
23 
29 #define MSG 0
30 
35 #define WARN 1
36 
41 #define ERR 2
42 
43 
44 /* static int (*error)() = 0; */
45 static int (*ext_error) (const char *, int); /* Roger Bivand 17 June 2000 */
46 static int no_warn = 0;
47 static int no_sleep = 1;
48 static int message_id = 1;
49 
50 static int print_word(FILE *, char **, int *, const int);
51 static void print_sentence(FILE *, const int, const char *);
52 static int print_error(const char *, const int);
53 static int mail_msg(const char *, int);
54 static int write_error(const char *, int, time_t, const char *);
55 static int log_error(const char *, int);
56 
57 static int vfprint_error(int type, const char *template, va_list ap)
58 {
59  char buffer[2000]; /* G_asprintf does not work */
60 
61  vsprintf(buffer, template, ap);
62 
63  /* . */
64  return print_error(buffer, type);
65 }
66 
74 void G_message(const char *msg, ...)
75 {
76  if (G_verbose() >= G_verbose_std()) {
77  va_list ap;
78 
79  va_start(ap, msg);
80  vfprint_error(MSG, msg, ap);
81  va_end(ap);
82  }
83 
84  return;
85 }
86 
95 void G_verbose_message(const char *msg, ...)
96 {
97  if (G_verbose() > G_verbose_std()) {
98  va_list ap;
99 
100  va_start(ap, msg);
101  vfprint_error(MSG, msg, ap);
102  va_end(ap);
103  }
104 
105  return;
106 }
107 
119 void G_important_message(const char *msg, ...)
120 {
121  if (G_verbose() > G_verbose_min()) {
122  va_list ap;
123 
124  va_start(ap, msg);
125  vfprint_error(MSG, msg, ap);
126  va_end(ap);
127  }
128 
129  return;
130 }
131 
150 int G_fatal_error(const char *msg, ...)
151 {
152  static int busy;
153  va_list ap;
154 
155  if (busy)
156  exit(EXIT_FAILURE);
157  busy = 1;
158 
159  va_start(ap, msg);
160  vfprint_error(ERR, msg, ap);
161  va_end(ap);
162 
163  exit(EXIT_FAILURE);
164 }
165 
178 int G_warning(const char *msg, ...)
179 {
180  va_list ap;
181 
182  if (no_warn)
183  return 0;
184 
185  va_start(ap, msg);
186  vfprint_error(WARN, msg, ap);
187  va_end(ap);
188 
189  return 0;
190 }
191 
200 {
201  int prev;
202 
203  prev = no_warn;
204  no_warn = flag;
205  return prev;
206 }
207 
216 {
217  int prev;
218 
219  prev = !no_sleep;
220  no_sleep = !flag;
221  return prev;
222 }
223 
233 int G_set_error_routine(int (*error_routine) (const char *, int))
234 {
235  ext_error = error_routine; /* Roger Bivand 17 June 2000 */
236  return 0;
237 }
238 
248 {
249  ext_error = 0; /* Roger Bivand 17 June 2000 */
250 
251  return 0;
252 }
253 
254 /* Print info to stderr and optionally to log file and optionally send mail */
255 static int print_error(const char *msg, const int type)
256 {
257  static char *prefix_std[3];
258  int fatal, format;
259 
260  if (!prefix_std[0]) { /* First time: set prefixes */
261  prefix_std[0] = "";
262  prefix_std[1] = _("WARNING: ");
263  prefix_std[2] = _("ERROR: ");
264  }
265 
266  if (type == ERR)
267  fatal = TRUE;
268  else /* WARN */
269  fatal = FALSE;
270 
271  if ((type == WARN || type == ERR) && ext_error) { /* Function defined by application */
272  ext_error(msg, fatal);
273  }
274  else {
275  char *w;
276  int len, lead;
277 
278  format = G_info_format();
279 
280  if (format != G_INFO_FORMAT_GUI) {
281  if (type == WARN || type == ERR) {
282  log_error(msg, fatal);
283  }
284 
285  fprintf(stderr, "%s", prefix_std[type]);
286  len = lead = strlen(prefix_std[type]);
287  w = (char *)msg;
288 
289  while (print_word(stderr, &w, &len, lead)) ;
290 
291  if ((type != MSG) && isatty(fileno(stderr))
292  && (G_info_format() == G_INFO_FORMAT_STANDARD)) { /* Bell */
293  fprintf(stderr, "\7");
294  fflush(stderr);
295  if (!no_sleep)
296  G_sleep(5);
297  }
298  else if ((type == WARN || type == ERR) && getenv("GRASS_ERROR_MAIL")) { /* Mail */
299  mail_msg(msg, fatal);
300  }
301  }
302  else { /* GUI */
303  print_sentence(stderr, type, msg);
304  }
305  }
306 
307  return 0;
308 }
309 
310 static int log_error(const char *msg, int fatal)
311 {
312  char cwd[GPATH_MAX];
313  time_t clock;
314  char *gisbase;
315 
316  /* get time */
317  clock = time(NULL);
318 
319  /* get current working directory */
320  getcwd(cwd, sizeof(cwd));
321 
322  /* write the error log file */
323  if ((gisbase = G_gisbase()))
324  write_error(msg, fatal, clock, cwd);
325 
326  return 0;
327 }
328 
329 /* Write a message to the log file */
330 static int write_error(const char *msg, int fatal,
331  time_t clock, const char *cwd)
332 {
333  static char *logfile;
334  FILE *log;
335 
336  if (!logfile) {
337  logfile = getenv("GIS_ERROR_LOG");
338  if (!logfile) {
339  char buf[GPATH_MAX];
340 
341  sprintf(buf, "%s/GIS_ERROR_LOG", G__home());
342  logfile = G_store(buf);
343  }
344  }
345 
346  log = fopen(logfile, "r");
347  if (!log)
348  /* GIS_ERROR_LOG file is not readable or does not exist */
349  return 1;
350 
351  log = freopen(logfile, "a", log);
352  if (!log)
353  /* the user doesn't have write permission */
354  return 1;
355 
356  fprintf(log, "-------------------------------------\n");
357  fprintf(log, "%-10s %s\n", "program:", G_program_name());
358  fprintf(log, "%-10s %s\n", "user:", G_whoami());
359  fprintf(log, "%-10s %s\n", "cwd:", cwd);
360  fprintf(log, "%-10s %s\n", "date:", ctime(&clock));
361  fprintf(log, "%-10s %s\n", fatal ? "error:" : "warning:", msg);
362  fprintf(log, "-------------------------------------\n");
363 
364  fclose(log);
365 
366  return 0;
367 }
368 
369 /* Mail a message */
370 static int mail_msg(const char *msg, int fatal)
371 {
372  FILE *mail;
373  char command[64];
374  char *user;
375 
376  user = G_whoami();
377  if (user == 0 || *user == 0)
378  return 1;
379 
380  sprintf(command, "mail '%s'", G_whoami());
381  if ((mail = popen(command, "w"))) {
382  fprintf(mail, "GIS %s: %s\n", fatal ? "ERROR" : "WARNING", msg);
383  G_pclose(mail);
384  }
385 
386  return 0;
387 }
388 
389 /* Print one word, new line if necessary */
390 static int print_word(FILE * fd, char **word, int *len, const int lead)
391 {
392  int wlen, start, totlen;
393  int nl;
394  char *w, *b;
395 
396  start = *len;
397  w = *word;
398 
399  nl = 0;
400  while (*w == ' ' || *w == '\t' || *w == '\n')
401  if (*w++ == '\n')
402  nl++;
403 
404  wlen = 0;
405  for (b = w; *b != 0 && *b != ' ' && *b != '\t' && *b != '\n'; b++)
406  wlen++;
407 
408  if (wlen == 0) {
409  fprintf(fd, "\n");
410  return 0;
411  }
412 
413  if (start > lead) { /* add space */
414  totlen = start + wlen + 1;
415  }
416  else {
417  totlen = start + wlen;
418  }
419 
420  if (nl != 0 || totlen > 75) {
421  while (--nl > 0)
422  fprintf(fd, "\n");
423  fprintf(fd, "\n%*s", lead, "");
424  start = lead;
425  }
426 
427  if (start > lead) {
428  fprintf(fd, " ");
429  start++;
430  }
431 
432  *len = start + wlen;
433 
434  fwrite(w, 1, wlen, fd);
435  w += wlen;
436 
437  *word = w;
438 
439  return 1;
440 }
441 
442 /* Print one message, prefix inserted before each new line */
443 static void print_sentence(FILE * fd, const int type, const char *msg)
444 {
445  char prefix[100];
446  const char *start;
447 
448  switch (type) {
449  case MSG:
450  sprintf(prefix, "GRASS_INFO_MESSAGE(%d,%d): ", getpid(), message_id);
451  break;
452  case WARN:
453  sprintf(prefix, "GRASS_INFO_WARNING(%d,%d): ", getpid(), message_id);
454  break;
455  case ERR:
456  sprintf(prefix, "GRASS_INFO_ERROR(%d,%d): ", getpid(), message_id);
457  break;
458  }
459 
460  start = msg;
461 
462  fprintf(stderr, "\n");
463  while (*start != '\0') {
464  const char *next = start;
465 
466  fprintf(fd, "%s", prefix);
467 
468  while (*next != '\0') {
469  next++;
470 
471  if (*next == '\n') {
472  next++;
473  break;
474  }
475  }
476 
477  fwrite(start, 1, next - start, fd);
478  fprintf(fd, "\n");
479  start = next;
480  }
481  fprintf(stderr, "GRASS_INFO_END(%d,%d)\n", getpid(), message_id);
482  message_id++;
483 }
484 
492 int G_info_format(void)
493 {
494  static int grass_info_format = -1;
495  char *fstr;
496 
497  if (grass_info_format < 0) {
498  fstr = getenv("GRASS_MESSAGE_FORMAT");
499 
500  if (fstr && G_strcasecmp(fstr, "gui") == 0)
501  grass_info_format = G_INFO_FORMAT_GUI;
502  else if (fstr && G_strcasecmp(fstr, "silent") == 0)
503  grass_info_format = G_INFO_FORMAT_SILENT;
504  else if (fstr && G_strcasecmp(fstr, "plain") == 0)
505  grass_info_format = G_INFO_FORMAT_PLAIN;
506  else
507  grass_info_format = G_INFO_FORMAT_STANDARD;
508  }
509 
510  return grass_info_format;
511 }
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:192
float b
Definition: named_colr.c:8
#define NULL
Definition: strings.c:26
int G_unset_error_routine(void)
After this call subsequent error messages will be handled in the default method.
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
#define FALSE
Definition: dbfopen.c:117
void G_important_message(const char *msg,...)
Print a message to stderr even in brief mode (verbosity=1)
int G_suppress_warnings(int flag)
Suppress printing a warning message to stderr.
void print_error(int err_code, char *msg,...)
Definition: gem/error.c:32
int G_set_error_routine(int(*error_routine)(const char *, int))
Establishes error_routine as the routine that will handle the printing of subsequent error messages...
list command
Definition: render.py:1305
tuple gisbase
Definition: forms.py:59
#define ERR
A fatal error message.
Definition: lib/gis/error.c:41
int G_warning(const char *msg,...)
Print a warning message to stderr.
char * getenv()
log
Definition: wxnviz.py:54
void G_verbose_message(const char *msg,...)
Print a message to stderr but only if module is in verbose mode.
Definition: lib/gis/error.c:95
void G_message(const char *msg,...)
Print a message to stderr.
Definition: lib/gis/error.c:74
#define WARN
A warning message.
Definition: lib/gis/error.c:35
#define TRUE
Definition: dbfopen.c:118
int G_info_format(void)
Get current message format.
void G_sleep(unsigned int seconds)
Definition: sleep.c:11
flag
Definition: tools.py:1403
#define MSG
A message.
Definition: lib/gis/error.c:29
const char * G_program_name(void)
return module name
Definition: progrm_nme.c:33
int G_verbose_std(void)
Get standard verbosity level.
Definition: verbose.c:80
int G_verbose(void)
Get current verbosity level.
Definition: verbose.c:45
char * G_whoami(void)
Gets user's name.
Definition: gis/whoami.c:40
struct ellps_list * next
tuple msg
Definition: wxnviz.py:32
def fatal(msg)
Display an error message using g.message -e, then abort.
Definition: core.py:383
char * G_gisbase(void)
top level module directory
Definition: gisbase.c:42
char * G__home(void)
Definition: home.c:43
int G_verbose_min(void)
Get min verbosity level.
Definition: verbose.c:92
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int G_pclose(FILE *ptr)
Definition: popen.c:9
int G_sleep_on_error(int flag)
Turn on/off no_sleep flag.