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);
155 vfprint_error(
ERR, msg, ap);
181 vfprint_error(
WARN, msg, ap);
230 ext_error = error_routine;
252 static char *prefix_std[3];
255 if (!prefix_std[0]) {
257 prefix_std[1] = _(
"WARNING: ");
258 prefix_std[2] = _(
"ERROR: ");
266 if ((type ==
WARN || type ==
ERR) && ext_error) {
267 ext_error(msg, fatal);
275 if (format != G_INFO_FORMAT_GUI) {
276 if (type ==
WARN || type ==
ERR) {
277 log_error(msg, fatal);
280 fprintf(stderr,
"%s", prefix_std[type]);
281 len = lead = strlen(prefix_std[type]);
284 while (print_word(stderr, &w, &len, lead)) ;
286 if ((type !=
MSG) &&
isatty(fileno(stderr))
288 fprintf(stderr,
"\7");
293 else if ((type ==
WARN || type ==
ERR) &&
getenv(
"GRASS_ERROR_MAIL")) {
294 mail_msg(msg, fatal);
298 print_sentence(stderr, type, msg);
305 static int log_error(
const char *msg,
int fatal)
315 getcwd(cwd,
sizeof(cwd));
319 write_error(msg, fatal, clock, cwd);
325 static int write_error(
const char *msg,
int fatal,
326 time_t clock,
const char *cwd)
328 static char *logfile;
332 logfile =
getenv(
"GIS_ERROR_LOG");
341 log = fopen(logfile,
"r");
346 log = freopen(logfile,
"a", log);
351 fprintf(log,
"-------------------------------------\n");
353 fprintf(log,
"%-10s %s\n",
"user:",
G_whoami());
354 fprintf(log,
"%-10s %s\n",
"cwd:", cwd);
355 fprintf(log,
"%-10s %s\n",
"date:", ctime(&clock));
356 fprintf(log,
"%-10s %s\n", fatal ?
"error:" :
"warning:", msg);
357 fprintf(log,
"-------------------------------------\n");
365 static int mail_msg(
const char *msg,
int fatal)
372 if (user == 0 || *user == 0)
376 if ((mail = popen(command,
"w"))) {
377 fprintf(mail,
"GIS %s: %s\n", fatal ?
"ERROR" :
"WARNING", msg);
385 static int print_word(FILE *
fd,
char **word,
int *len,
const int lead)
387 int wlen, start, totlen;
395 while (*w ==
' ' || *w ==
'\t' || *w ==
'\n')
400 for (b = w; *b != 0 && *b !=
' ' && *b !=
'\t' && *b !=
'\n'; b++)
409 totlen = start + wlen + 1;
412 totlen = start + wlen;
415 if (nl != 0 || totlen > 75) {
418 fprintf(fd,
"\n%*s", lead,
"");
429 fwrite(w, 1, wlen, fd);
438 static void print_sentence(FILE * fd,
const int type,
const char *msg)
445 sprintf(prefix,
"GRASS_INFO_MESSAGE(%d,%d): ", getpid(), message_id);
448 sprintf(prefix,
"GRASS_INFO_WARNING(%d,%d): ", getpid(), message_id);
451 sprintf(prefix,
"GRASS_INFO_ERROR(%d,%d): ", getpid(), message_id);
457 fprintf(stderr,
"\n");
458 while (*start !=
'\0') {
459 const char *
next = start;
461 fprintf(fd,
"%s", prefix);
463 while (*next !=
'\0') {
472 fwrite(start, 1, next - start, fd);
476 fprintf(stderr,
"GRASS_INFO_END(%d,%d)\n", getpid(), message_id);
489 static int grass_info_format = -1;
492 if (grass_info_format < 0) {
493 fstr =
getenv(
"GRASS_MESSAGE_FORMAT");
496 grass_info_format = G_INFO_FORMAT_GUI;
498 grass_info_format = G_INFO_FORMAT_SILENT;
500 grass_info_format = G_INFO_FORMAT_PLAIN;
502 grass_info_format = G_INFO_FORMAT_STANDARD;
505 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.