21 #include <sys/types.h>
25 #include "gis_local_proto.h"
47 static int (*ext_error)(
const char *, int);
48 static int no_warn =
FALSE;
49 static int no_sleep =
TRUE;
51 static int grass_info_format;
53 static char *prefix_std[3];
54 static struct Counter message_id;
56 static int print_word(FILE *,
char **,
int *,
const int);
57 static void print_sentence(FILE *,
const int,
const char *);
58 static void print_error(
const char *,
const int);
59 static void mail_msg(
const char *,
int);
60 static int write_error(
const char *,
int, time_t,
const char *);
61 static void log_error(
const char *,
int);
63 static int fatal_longjmp;
64 static jmp_buf fatal_jmp_buf;
68 fatal_longjmp = enable;
69 return &fatal_jmp_buf;
72 static void vfprint_error(
int type,
const char *
template, va_list ap)
78 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;
280 static void print_error(
const char *msg,
const int type)
289 if ((type ==
MSG || type ==
WARN || type ==
ERR) &&
291 ext_error(msg, fatal);
297 if (type ==
WARN || type ==
ERR)
298 log_error(msg, fatal);
308 fprintf(stderr,
"%s", prefix_std[type]);
309 len = lead = strlen(prefix_std[type]);
312 while (print_word(stderr, &w, &len, lead))
316 fprintf(stderr,
"%s%s\n", prefix_std[type], msg);
319 if ((type !=
MSG) && isatty(fileno(stderr)) &&
321 fprintf(stderr,
"\7");
326 else if ((type ==
WARN || type ==
ERR) &&
327 getenv(
"GRASS_ERROR_MAIL")) {
328 mail_msg(msg, fatal);
332 print_sentence(stderr, type, msg);
337 static void log_error(
const char *msg,
int fatal)
347 if (getcwd(cwd,
sizeof(cwd)) ==
NULL)
348 sprintf(cwd,
"%s",
_(
"unknown"));
352 write_error(msg, fatal, clock, cwd);
357 static int initialized;
366 prefix_std[1] =
_(
"WARNING: ");
367 prefix_std[2] =
_(
"ERROR: ");
369 logfile = getenv(
"GIS_ERROR_LOG");
377 fstr = getenv(
"GRASS_MESSAGE_FORMAT");
392 static int write_error(
const char *msg,
int fatal, time_t clock,
399 log = fopen(logfile,
"r");
404 log = freopen(logfile,
"a", log);
409 fprintf(log,
"-------------------------------------\n");
411 fprintf(log,
"%-10s %s\n",
"user:",
G_whoami());
412 fprintf(log,
"%-10s %s\n",
"cwd:", cwd);
413 fprintf(log,
"%-10s %s\n",
"date:", ctime(&clock));
414 fprintf(log,
"%-10s %s\n", fatal ?
"error:" :
"warning:", msg);
415 fprintf(log,
"-------------------------------------\n");
423 static void mail_msg(
const char *msg,
int fatal)
429 fprintf(
fp,
"GIS %s: %s\n", fatal ?
"ERROR" :
"WARNING", msg);
435 static int print_word(FILE *fd,
char **word,
int *len,
const int lead)
437 int wlen, start, totlen;
445 while (*w ==
' ' || *w ==
'\t' || *w ==
'\n')
450 for (
b = w; *
b != 0 && *
b !=
' ' && *
b !=
'\t' && *
b !=
'\n';
b++)
459 totlen = start + wlen + 1;
462 totlen = start + wlen;
465 if (nl != 0 || totlen > 75) {
468 fprintf(fd,
"\n%*s", lead,
"");
479 fwrite(w, 1, wlen, fd);
488 static void print_sentence(FILE *fd,
const int type,
const char *msg)
496 sprintf(prefix,
"GRASS_INFO_MESSAGE(%d,%d): ", getpid(),
id);
499 sprintf(prefix,
"GRASS_INFO_WARNING(%d,%d): ", getpid(),
id);
502 sprintf(prefix,
"GRASS_INFO_ERROR(%d,%d): ", getpid(),
id);
508 fprintf(stderr,
"\n");
509 while (*start !=
'\0') {
510 const char *next = start;
512 fprintf(fd,
"%s", prefix);
514 while (*next !=
'\0') {
523 fwrite(start, 1, next - start, fd);
527 fprintf(stderr,
"GRASS_INFO_END(%d,%d)\n", getpid(),
id);
541 return grass_info_format;
void G_free(void *)
Free allocated memory.
FILE * G_open_mail(struct Popen *)
const char * G_whoami(void)
Gets user's name.
void G_close_mail(struct Popen *)
int G_verbose(void)
Get current verbosity level.
int G_verbose_min(void)
Get min verbosity level.
void G_sleep(unsigned int)
int G_is_initialized(int *)
void G_initialize_done(int *)
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
int G_counter_next(struct Counter *)
const char * G_gisbase(void)
Get full path name of the top level module directory.
const char * G_program_name(void)
Return module name.
void G_init_counter(struct Counter *, int)
int G_verbose_std(void)
Get standard verbosity level.
int G_vasprintf(char **, const char *, va_list)
Safe replacement for asprintf().
char * G_store(const char *)
Copy string to allocated memory.
jmp_buf * G_fatal_longjmp(int enable)
void G_verbose_message(const char *msg,...)
Print a message to stderr but only if module is in verbose mode.
int G_sleep_on_error(int flag)
Turn on/off no_sleep flag.
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.
#define WARN
A warning message.
void G_important_message(const char *msg,...)
Print a message to stderr even in brief mode (verbosity=1)
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
#define ERR
A fatal error message.
void G_message(const char *msg,...)
Print a message to stderr.
int G_suppress_warnings(int flag)
Suppress printing a warning message to stderr.
void G_unset_error_routine(void)
After this call subsequent error messages will be handled in the default method.
void G_warning(const char *msg,...)
Print a warning message to stderr.
int G_info_format(void)
Get current message format.
void G_init_logging(void)
void G__call_error_handlers(void)
Call available error handlers (internal use only)
#define G_INFO_FORMAT_GUI
#define G_INFO_FORMAT_PLAIN
#define G_INFO_FORMAT_STANDARD
#define G_INFO_FORMAT_SILENT
const char * G__home(void)
Get user's home directory (internal use only)