22#include <grass/glocale.h>
25#include "gis_local_proto.h"
47static int (*ext_error)(
const char *, int);
48static int no_warn =
FALSE;
49static int no_sleep =
TRUE;
51static int grass_info_format;
53static char *prefix_std[3];
54static struct Counter message_id;
56static int print_word(FILE *,
char **,
int *,
const int);
57static void print_sentence(FILE *,
const int,
const char *);
58static void print_error(
const char *,
const int);
59static void mail_msg(
const char *,
int);
60static int write_error(
const char *,
int, time_t,
const char *);
61static void log_error(
const char *,
int);
63static int fatal_longjmp;
64static jmp_buf fatal_jmp_buf;
68 fatal_longjmp = enable;
69 return &fatal_jmp_buf;
72static 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;
280static 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);
300 if (format == G_INFO_FORMAT_SILENT)
303 if (format != G_INFO_FORMAT_GUI) {
304 if (format != G_INFO_FORMAT_PLAIN) {
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);
337static 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");
373 sprintf(buf,
"%s/GIS_ERROR_LOG",
G__home());
377 fstr = getenv(
"GRASS_MESSAGE_FORMAT");
380 grass_info_format = G_INFO_FORMAT_GUI;
382 grass_info_format = G_INFO_FORMAT_SILENT;
384 grass_info_format = G_INFO_FORMAT_PLAIN;
386 grass_info_format = G_INFO_FORMAT_STANDARD;
392static 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");
423static void mail_msg(
const char *msg,
int fatal)
429 fprintf(fp,
"GIS %s: %s\n", fatal ?
"ERROR" :
"WARNING", msg);
435static 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);
488static 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 *buf)
Free allocated memory.
int G_vasprintf(char **out, const char *fmt, va_list ap)
Safe replacement for asprintf().
void G_initialize_done(int *p)
void G_init_counter(struct Counter *c, int v)
int G_is_initialized(int *p)
int G_counter_next(struct Counter *c)
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.
jmp_buf * G_fatal_longjmp(int enable)
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)
const char * G_gisbase(void)
Get full path name of the top level module directory.
void G__call_error_handlers(void)
Call available error handlers (internal use only)
const char * G__home(void)
Get user's home directory (internal use only)
const char * G_program_name(void)
Return module name.
void G_sleep(unsigned int seconds)
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
char * G_store(const char *s)
Copy string to allocated memory.
int G_verbose(void)
Get current verbosity level.
int G_verbose_min(void)
Get min verbosity level.
int G_verbose_std(void)
Get standard verbosity level.
const char * G_whoami(void)
Gets user's name.