GRASS GIS 7 Programmer's Manual
7.8.4(2020)-exported
|
Go to the documentation of this file.
21 #include <sys/types.h>
22 #include <grass/glocale.h>
23 #include <grass/gis.h>
25 #include "gis_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)
79 print_error(buffer, type);
96 vfprint_error(
MSG, msg, ap);
115 vfprint_error(
MSG, msg, ap);
137 vfprint_error(
MSG, msg, ap);
171 vfprint_error(
ERR, msg, ap);
177 longjmp(fatal_jmp_buf, 1);
186 if (getenv(
"GRASS_ABORT_ON_ERROR"))
212 vfprint_error(
WARN, msg, ap);
263 ext_error = error_routine;
280 static void print_error(
const char *msg,
const int type)
289 if ((type ==
MSG || type ==
WARN || type ==
ERR) && ext_error) {
290 ext_error(msg, fatal);
296 if (type ==
WARN || type ==
ERR)
297 log_error(msg, fatal);
299 if (format == G_INFO_FORMAT_SILENT)
302 if (format != G_INFO_FORMAT_GUI) {
303 if (format != G_INFO_FORMAT_PLAIN) {
307 fprintf(stderr,
"%s", prefix_std[type]);
308 len = lead = strlen(prefix_std[type]);
311 while (print_word(stderr, &w, &len, lead)) ;
314 fprintf(stderr,
"%s%s\n", prefix_std[type], msg);
317 if ((type !=
MSG) && isatty(fileno(stderr))
319 fprintf(stderr,
"\7");
324 else if ((type ==
WARN || type ==
ERR) && getenv(
"GRASS_ERROR_MAIL")) {
325 mail_msg(msg, fatal);
329 print_sentence(stderr, type, msg);
334 static void log_error(
const char *msg,
int fatal)
344 getcwd(cwd,
sizeof(cwd));
348 write_error(msg, fatal, clock, cwd);
353 static int initialized;
362 prefix_std[1] = _(
"WARNING: ");
363 prefix_std[2] = _(
"ERROR: ");
365 logfile = getenv(
"GIS_ERROR_LOG");
368 sprintf(buf,
"%s/GIS_ERROR_LOG",
G__home());
372 fstr = getenv(
"GRASS_MESSAGE_FORMAT");
375 grass_info_format = G_INFO_FORMAT_GUI;
377 grass_info_format = G_INFO_FORMAT_SILENT;
379 grass_info_format = G_INFO_FORMAT_PLAIN;
381 grass_info_format = G_INFO_FORMAT_STANDARD;
387 static int write_error(
const char *msg,
int fatal,
388 time_t clock,
const char *cwd)
394 log = fopen(logfile,
"r");
399 log = freopen(logfile,
"a", log);
404 fprintf(log,
"-------------------------------------\n");
406 fprintf(log,
"%-10s %s\n",
"user:",
G_whoami());
407 fprintf(log,
"%-10s %s\n",
"cwd:", cwd);
408 fprintf(log,
"%-10s %s\n",
"date:", ctime(&clock));
409 fprintf(log,
"%-10s %s\n", fatal ?
"error:" :
"warning:", msg);
410 fprintf(log,
"-------------------------------------\n");
418 static void mail_msg(
const char *msg,
int fatal)
424 fprintf(fp,
"GIS %s: %s\n", fatal ?
"ERROR" :
"WARNING", msg);
430 static int print_word(FILE * fd,
char **word,
int *len,
const int lead)
432 int wlen, start, totlen;
440 while (*w ==
' ' || *w ==
'\t' || *w ==
'\n')
445 for (
b = w; *
b != 0 && *
b !=
' ' && *
b !=
'\t' && *
b !=
'\n';
b++)
454 totlen = start + wlen + 1;
457 totlen = start + wlen;
460 if (nl != 0 || totlen > 75) {
463 fprintf(fd,
"\n%*s", lead,
"");
474 fwrite(w, 1, wlen, fd);
483 static void print_sentence(FILE * fd,
const int type,
const char *msg)
491 sprintf(prefix,
"GRASS_INFO_MESSAGE(%d,%d): ", getpid(),
id);
494 sprintf(prefix,
"GRASS_INFO_WARNING(%d,%d): ", getpid(),
id);
497 sprintf(prefix,
"GRASS_INFO_ERROR(%d,%d): ", getpid(),
id);
503 fprintf(stderr,
"\n");
504 while (*start !=
'\0') {
505 const char *next = start;
507 fprintf(fd,
"%s", prefix);
509 while (*next !=
'\0') {
518 fwrite(start, 1, next - start, fd);
522 fprintf(stderr,
"GRASS_INFO_END(%d,%d)\n", getpid(),
id);
536 return grass_info_format;
void G_init_counter(struct Counter *c, int v)
const char * G_program_name(void)
Return module name.
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
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.
void G_unset_error_routine(void)
After this call subsequent error messages will be handled in the default method.
int G_is_initialized(int *p)
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
const char * G__home(void)
Get user's home directory (internal use only)
const char * G_whoami(void)
Gets user's name.
jmp_buf * G_fatal_longjmp(int enable)
int G_vasprintf(char **out, const char *fmt, va_list ap)
Safe replacement for asprintf().
void G_initialize_done(int *p)
void G_init_logging(void)
void G_message(const char *msg,...)
Print a message to stderr.
void G_free(void *buf)
Free allocated memory.
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.
int G_verbose(void)
Get current verbosity level.
int G_verbose_std(void)
Get standard verbosity level.
void G_important_message(const char *msg,...)
Print a message to stderr even in brief mode (verbosity=1)
#define WARN
A warning message.
#define ERR
A fatal error message.
void G__call_error_handlers(void)
Call available error handlers (internal use only)
void G_sleep(unsigned int seconds)
int G_verbose_min(void)
Get min verbosity level.
const char * G_gisbase(void)
Get full path name of the top level module directory.
int G_suppress_warnings(int flag)
Suppress printing a warning message to stderr.
int G_counter_next(struct Counter *c)
int G_sleep_on_error(int flag)
Turn on/off no_sleep flag.
int G_info_format(void)
Get current message format.
void G_warning(const char *msg,...)
Print a warning message to stderr.