20 #include <sys/types.h>
21 #include <grass/glocale.h>
22 #include <grass/gis.h>
45 static int (*ext_error) (
const char *,
int);
46 static int no_warn = 0;
47 static int no_sleep = 1;
48 static int message_id = 1;
50 static int print_word(FILE *,
char **,
int *,
const int);
51 static void print_sentence(FILE *,
const int,
const char *);
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);
57 static int vfprint_error(
int type,
const char *
template, va_list ap)
61 vsprintf(buffer,
template, ap);
80 vfprint_error(
MSG, msg, ap);
101 vfprint_error(
MSG, msg, ap);
125 vfprint_error(
MSG, msg, ap);
160 vfprint_error(
ERR, msg, ap);
186 vfprint_error(
WARN, msg, ap);
235 ext_error = error_routine;
257 static char *prefix_std[3];
260 if (!prefix_std[0]) {
262 prefix_std[1] = _(
"WARNING: ");
263 prefix_std[2] = _(
"ERROR: ");
271 if ((type ==
MSG || type ==
WARN || type ==
ERR) && ext_error) {
272 ext_error(msg, fatal);
277 if (type ==
WARN || type ==
ERR)
278 log_error(msg, fatal);
280 if (format == G_INFO_FORMAT_SILENT)
283 if (format != G_INFO_FORMAT_GUI) {
284 if (format != G_INFO_FORMAT_PLAIN) {
288 fprintf(stderr,
"%s", prefix_std[type]);
289 len = lead = strlen(prefix_std[type]);
292 while (print_word(stderr, &w, &len, lead)) ;
295 fprintf(stderr,
"%s\n", msg);
298 if ((type !=
MSG) && isatty(fileno(stderr))
300 fprintf(stderr,
"\7");
305 else if ((type ==
WARN || type ==
ERR) &&
getenv(
"GRASS_ERROR_MAIL")) {
306 mail_msg(msg, fatal);
310 print_sentence(stderr, type, msg);
317 static int log_error(
const char *msg,
int fatal)
327 getcwd(cwd,
sizeof(cwd));
331 write_error(msg, fatal, clock, cwd);
337 static int write_error(
const char *msg,
int fatal,
338 time_t clock,
const char *cwd)
340 static char *logfile;
344 logfile =
getenv(
"GIS_ERROR_LOG");
353 log = fopen(logfile,
"r");
358 log = freopen(logfile,
"a", log);
363 fprintf(log,
"-------------------------------------\n");
365 fprintf(log,
"%-10s %s\n",
"user:",
G_whoami());
366 fprintf(log,
"%-10s %s\n",
"cwd:", cwd);
367 fprintf(log,
"%-10s %s\n",
"date:", ctime(&clock));
368 fprintf(log,
"%-10s %s\n", fatal ?
"error:" :
"warning:", msg);
369 fprintf(log,
"-------------------------------------\n");
377 static int mail_msg(
const char *msg,
int fatal)
384 if (user == 0 || *user == 0)
388 if ((mail = popen(command,
"w"))) {
389 fprintf(mail,
"GIS %s: %s\n", fatal ?
"ERROR" :
"WARNING", msg);
397 static int print_word(FILE *
fd,
char **word,
int *len,
const int lead)
399 int wlen, start, totlen;
407 while (*w ==
' ' || *w ==
'\t' || *w ==
'\n')
412 for (b = w; *b != 0 && *b !=
' ' && *b !=
'\t' && *b !=
'\n'; b++)
421 totlen = start + wlen + 1;
424 totlen = start + wlen;
427 if (nl != 0 || totlen > 75) {
430 fprintf(fd,
"\n%*s", lead,
"");
441 fwrite(w, 1, wlen, fd);
450 static void print_sentence(FILE * fd,
const int type,
const char *msg)
457 sprintf(prefix,
"GRASS_INFO_MESSAGE(%d,%d): ", getpid(), message_id);
460 sprintf(prefix,
"GRASS_INFO_WARNING(%d,%d): ", getpid(), message_id);
463 sprintf(prefix,
"GRASS_INFO_ERROR(%d,%d): ", getpid(), message_id);
469 fprintf(stderr,
"\n");
470 while (*start !=
'\0') {
471 const char *
next = start;
473 fprintf(fd,
"%s", prefix);
475 while (*next !=
'\0') {
484 fwrite(start, 1, next - start, fd);
488 fprintf(stderr,
"GRASS_INFO_END(%d,%d)\n", getpid(), message_id);
501 static int grass_info_format = -1;
504 if (grass_info_format < 0) {
505 fstr =
getenv(
"GRASS_MESSAGE_FORMAT");
508 grass_info_format = G_INFO_FORMAT_GUI;
510 grass_info_format = G_INFO_FORMAT_SILENT;
512 grass_info_format = G_INFO_FORMAT_PLAIN;
514 grass_info_format = G_INFO_FORMAT_STANDARD;
517 return grass_info_format;
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
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.
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,...)
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...
#define ERR
A fatal error message.
def fatal
Display an error message using g.message -e, then abort.
void G_verbose_message(const char *msg,...)
Print a message to stderr but only if module is in verbose mode.
void G_message(const char *msg,...)
Print a message to stderr.
#define WARN
A warning message.
int G_info_format(void)
Get current message format.
void G_sleep(unsigned int seconds)
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.
char * G_whoami(void)
Gets user's name.
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
char * G_gisbase(void)
top level module directory
int G_verbose_min(void)
Get min verbosity level.
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
int G_sleep_on_error(int flag)
Turn on/off no_sleep flag.