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 ==
WARN || type ==
ERR) && ext_error) {
272 ext_error(msg, fatal);
280 if (format != G_INFO_FORMAT_GUI) {
281 if (type ==
WARN || type ==
ERR) {
282 log_error(msg, fatal);
285 fprintf(stderr,
"%s", prefix_std[type]);
286 len = lead = strlen(prefix_std[type]);
289 while (print_word(stderr, &w, &len, lead)) ;
291 if ((type !=
MSG) && isatty(fileno(stderr))
293 fprintf(stderr,
"\7");
298 else if ((type ==
WARN || type ==
ERR) &&
getenv(
"GRASS_ERROR_MAIL")) {
299 mail_msg(msg, fatal);
303 print_sentence(stderr, type, msg);
310 static int log_error(
const char *msg,
int fatal)
320 getcwd(cwd,
sizeof(cwd));
324 write_error(msg, fatal, clock, cwd);
330 static int write_error(
const char *msg,
int fatal,
331 time_t clock,
const char *cwd)
333 static char *logfile;
337 logfile =
getenv(
"GIS_ERROR_LOG");
341 sprintf(buf,
"%s/GIS_ERROR_LOG",
G__home());
346 log = fopen(logfile,
"r");
351 log = freopen(logfile,
"a", log);
356 fprintf(log,
"-------------------------------------\n");
358 fprintf(log,
"%-10s %s\n",
"user:",
G_whoami());
359 fprintf(log,
"%-10s %s\n",
"cwd:", cwd);
360 fprintf(log,
"%-10s %s\n",
"date:", ctime(&clock));
361 fprintf(log,
"%-10s %s\n", fatal ?
"error:" :
"warning:", msg);
362 fprintf(log,
"-------------------------------------\n");
370 static int mail_msg(
const char *msg,
int fatal)
377 if (user == 0 || *user == 0)
380 sprintf(command,
"mail '%s'",
G_whoami());
381 if ((mail = popen(command,
"w"))) {
382 fprintf(mail,
"GIS %s: %s\n", fatal ?
"ERROR" :
"WARNING", msg);
390 static int print_word(FILE * fd,
char **word,
int *len,
const int lead)
392 int wlen, start, totlen;
400 while (*w ==
' ' || *w ==
'\t' || *w ==
'\n')
405 for (b = w; *b != 0 && *b !=
' ' && *b !=
'\t' && *b !=
'\n'; b++)
414 totlen = start + wlen + 1;
417 totlen = start + wlen;
420 if (nl != 0 || totlen > 75) {
423 fprintf(fd,
"\n%*s", lead,
"");
434 fwrite(w, 1, wlen, fd);
443 static void print_sentence(FILE * fd,
const int type,
const char *msg)
450 sprintf(prefix,
"GRASS_INFO_MESSAGE(%d,%d): ", getpid(), message_id);
453 sprintf(prefix,
"GRASS_INFO_WARNING(%d,%d): ", getpid(), message_id);
456 sprintf(prefix,
"GRASS_INFO_ERROR(%d,%d): ", getpid(), message_id);
462 fprintf(stderr,
"\n");
463 while (*start !=
'\0') {
464 const char *
next = start;
466 fprintf(fd,
"%s", prefix);
468 while (*next !=
'\0') {
477 fwrite(start, 1, next - start, fd);
481 fprintf(stderr,
"GRASS_INFO_END(%d,%d)\n", getpid(), message_id);
494 static int grass_info_format = -1;
497 if (grass_info_format < 0) {
498 fstr =
getenv(
"GRASS_MESSAGE_FORMAT");
501 grass_info_format = G_INFO_FORMAT_GUI;
503 grass_info_format = G_INFO_FORMAT_SILENT;
505 grass_info_format = G_INFO_FORMAT_PLAIN;
507 grass_info_format = G_INFO_FORMAT_STANDARD;
510 return grass_info_format;
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
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.
int G_warning(const char *msg,...)
Print a warning message to stderr.
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 * 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.