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");
336 sprintf(buf,
"%s/GIS_ERROR_LOG",
G__home());
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)
375 sprintf(command,
"mail '%s'",
G_whoami());
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;