21 #include <sys/types.h>
22 #include <grass/glocale.h>
23 #include <grass/gis.h>
25 #include "local_proto.h"
48 static int (*ext_error) (
const char *, int);
49 static int no_warn =
FALSE;
50 static int no_sleep =
TRUE;
52 static int grass_info_format;
54 static char *prefix_std[3];
55 static struct Counter message_id;
57 static int print_word(FILE *,
char **,
int *,
const int);
58 static void print_sentence(FILE *,
const int,
const char *);
59 static void print_error(
const char *,
const int);
60 static void mail_msg(
const char *,
int);
61 static int write_error(
const char *,
int, time_t,
const char *);
62 static void log_error(
const char *,
int);
64 static int fatal_longjmp;
65 static jmp_buf fatal_jmp_buf;
69 fatal_longjmp = enable;
70 return &fatal_jmp_buf;
73 static void vfprint_error(
int type,
const char *
template, va_list ap)
77 vsprintf(buffer,
template, ap);
79 print_error(buffer, type);
95 vfprint_error(
MSG, msg, ap);
114 vfprint_error(
MSG, msg, ap);
136 vfprint_error(
MSG, msg, ap);
170 vfprint_error(
ERR, msg, ap);
176 longjmp(fatal_jmp_buf, 1);
185 if (getenv(
"GRASS_ABORT_ON_ERROR"))
211 vfprint_error(
WARN, msg, ap);
262 ext_error = error_routine;
279 static void print_error(
const char *msg,
const int type)
288 if ((type ==
MSG || type ==
WARN || type ==
ERR) && ext_error) {
289 ext_error(msg, fatal);
295 if (type ==
WARN || type ==
ERR)
296 log_error(msg, fatal);
298 if (format == G_INFO_FORMAT_SILENT)
301 if (format != G_INFO_FORMAT_GUI) {
302 if (format != G_INFO_FORMAT_PLAIN) {
306 fprintf(stderr,
"%s", prefix_std[type]);
307 len = lead = strlen(prefix_std[type]);
310 while (print_word(stderr, &w, &len, lead)) ;
313 fprintf(stderr,
"%s%s\n", prefix_std[type], msg);
316 if ((type !=
MSG) && isatty(fileno(stderr))
318 fprintf(stderr,
"\7");
323 else if ((type ==
WARN || type ==
ERR) && getenv(
"GRASS_ERROR_MAIL")) {
324 mail_msg(msg, fatal);
328 print_sentence(stderr, type, msg);
333 static void log_error(
const char *msg,
int fatal)
343 getcwd(cwd,
sizeof(cwd));
347 write_error(msg, fatal, clock, cwd);
352 static int initialized;
361 prefix_std[1] = _(
"WARNING: ");
362 prefix_std[2] = _(
"ERROR: ");
364 logfile = getenv(
"GIS_ERROR_LOG");
367 sprintf(buf,
"%s/GIS_ERROR_LOG",
G__home());
371 fstr = getenv(
"GRASS_MESSAGE_FORMAT");
374 grass_info_format = G_INFO_FORMAT_GUI;
376 grass_info_format = G_INFO_FORMAT_SILENT;
378 grass_info_format = G_INFO_FORMAT_PLAIN;
380 grass_info_format = G_INFO_FORMAT_STANDARD;
386 static int write_error(
const char *msg,
int fatal,
387 time_t clock,
const char *cwd)
393 log = fopen(logfile,
"r");
398 log = freopen(logfile,
"a", log);
403 fprintf(log,
"-------------------------------------\n");
405 fprintf(log,
"%-10s %s\n",
"user:",
G_whoami());
406 fprintf(log,
"%-10s %s\n",
"cwd:", cwd);
407 fprintf(log,
"%-10s %s\n",
"date:", ctime(&clock));
408 fprintf(log,
"%-10s %s\n", fatal ?
"error:" :
"warning:", msg);
409 fprintf(log,
"-------------------------------------\n");
417 static void mail_msg(
const char *msg,
int fatal)
423 fprintf(fp,
"GIS %s: %s\n", fatal ?
"ERROR" :
"WARNING", msg);
429 static int print_word(FILE * fd,
char **word,
int *len,
const int lead)
431 int wlen, start, totlen;
439 while (*w ==
' ' || *w ==
'\t' || *w ==
'\n')
444 for (b = w; *b != 0 && *b !=
' ' && *b !=
'\t' && *b !=
'\n'; b++)
453 totlen = start + wlen + 1;
456 totlen = start + wlen;
459 if (nl != 0 || totlen > 75) {
462 fprintf(fd,
"\n%*s", lead,
"");
473 fwrite(w, 1, wlen, fd);
482 static void print_sentence(FILE * fd,
const int type,
const char *msg)
490 sprintf(prefix,
"GRASS_INFO_MESSAGE(%d,%d): ", getpid(),
id);
493 sprintf(prefix,
"GRASS_INFO_WARNING(%d,%d): ", getpid(),
id);
496 sprintf(prefix,
"GRASS_INFO_ERROR(%d,%d): ", getpid(),
id);
502 fprintf(stderr,
"\n");
503 while (*start !=
'\0') {
504 const char *next = start;
506 fprintf(fd,
"%s", prefix);
508 while (*next !=
'\0') {
517 fwrite(start, 1, next - start, fd);
521 fprintf(stderr,
"GRASS_INFO_END(%d,%d)\n", getpid(),
id);
535 return grass_info_format;
int G_info_format(void)
Get current message format.
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
const char * G__home(void)
Get user's home directory (internal use only)
void G_important_message(const char *msg,...)
Print a message to stderr even in brief mode (verbosity=1)
int G_sleep_on_error(int flag)
Turn on/off no_sleep flag.
void G_init_counter(struct Counter *c, int v)
#define WARN
A warning message.
void G_verbose_message(const char *msg,...)
Print a message to stderr but only if module is in verbose mode.
char * G_store(const char *s)
Copy string to allocated memory.
int G_is_initialized(int *p)
void G_initialize_done(int *p)
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int G_suppress_warnings(int flag)
Suppress printing a warning message to stderr.
void G_sleep(unsigned int seconds)
#define ERR
A fatal error message.
void 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...
const char * G_program_name(void)
Return module name.
int G_verbose_std(void)
Get standard verbosity level.
int G_verbose(void)
Get current verbosity level.
jmp_buf * G_fatal_longjmp(int enable)
void G_init_logging(void)
const char * G_whoami(void)
Gets user's name.
void G_message(const char *msg,...)
Print a message to stderr.
void G__call_error_handlers(void)
Call available error handlers (internal use only)
void G_unset_error_routine(void)
After this call subsequent error messages will be handled in the default method.
int G_verbose_min(void)
Get min verbosity level.
int G_counter_next(struct Counter *c)
const char * G_gisbase(void)
Get full path name of the top level module directory.
void G_warning(const char *msg,...)
Print a warning message to stderr.