error.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*!
  2. * \file error.c
  3. */
  4. /*
  5. **********************************************************************
  6. *
  7. * G_fatal_error (msg)
  8. * char *msg One line error message
  9. *
  10. * G_warning (msg)
  11. * char *msg
  12. *
  13. * Gives the message, msg, to the user. Also print a message to
  14. * $GISBASE/GIS_ERROR_LOG if the file exists and is writeable;
  15. * and to the file $HOME/GIS_ERROR_LOG in the user's home directory
  16. * if $HOME is set and the file exists.
  17. * G_warning() returns, which G_fatal_error() exits.
  18. *
  19. * note: By default, the message is handled by an internal routine
  20. * which prints the message to the screen. Using G_set_error_routine()
  21. * the programmer can have the message handled by another routine.
  22. * This is especially useful if the message should go to a particular
  23. * location on the screen when using curses or to a location on
  24. * a graphics device (monitor).
  25. *
  26. **********************************************************************
  27. * G_set_error_routine (error_routine)
  28. * int (*error_routine)()
  29. *
  30. * Establishes error_routine as the routine that will handle
  31. * the printing of subsequent error messages. error_routine
  32. * will be called like this:
  33. * error_routine(msg, fatal)
  34. * char *msg ;
  35. **********************************************************************
  36. * G_unset_error_routine ()
  37. *
  38. * After this call subsequent error messages will be handled in the
  39. * default method. Error messages are printed directly to the
  40. * screen:
  41. * ERROR: message
  42. * -or- WARNING: message
  43. *
  44. **********************************************************************/
  45. /*
  46. Throughout the code the references to these routines attempt to
  47. send format strings and arguments. It seems that we expect
  48. these to handle varargs, so now they do. 7-Mar-1999
  49. Bill Hughes
  50. */
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <unistd.h>
  54. #include <time.h>
  55. #include <stdarg.h>
  56. #include <sys/types.h>
  57. #include <grass/glocale.h>
  58. #include <grass/gis.h>
  59. /*!
  60. * \def MSG
  61. *
  62. * \brief A message
  63. */
  64. #define MSG 0
  65. /*!
  66. * \def WARN
  67. *
  68. * \brief A warning message
  69. */
  70. #define WARN 1
  71. /*!
  72. * \def ERR
  73. *
  74. * \brief A fatal error message
  75. */
  76. #define ERR 2
  77. /* static int (*error)() = 0; */
  78. static int (*ext_error)(const char *, int); /* Roger Bivand 17 June 2000 */
  79. static int no_warn = 0;
  80. static int no_sleep = 1;
  81. static int message_id = 1;
  82. static int print_word (FILE *, char **, int *, const int);
  83. static void print_sentence (FILE *, const int, const char *);
  84. static int print_error (const char *, const int);
  85. static int mail_msg (const char *, int);
  86. static int write_error (const char *, int, const char *,
  87. time_t, const char *);
  88. static int log_error (const char *, int);
  89. static int
  90. vfprint_error (int type, const char *template, va_list ap)
  91. {
  92. char buffer[2000]; /* G_asprintf does not work */
  93. vsprintf (buffer, template, ap);
  94. /* . */
  95. return print_error (buffer, type);
  96. }
  97. /*!
  98. * \fn void G_message (const char *msg,...)
  99. *
  100. * \brief Print a message to stderr
  101. *
  102. * The output format depends on enviroment variable GRASS_MESSAGE_FORMAT
  103. *
  104. * \param[in] msg string. Cannot be NULL.
  105. */
  106. void G_message (const char *msg,...)
  107. {
  108. if( G_verbose() >= G_verbose_std() ) {
  109. va_list ap;
  110. va_start(ap, msg);
  111. vfprint_error (MSG, msg, ap);
  112. va_end(ap);
  113. }
  114. }
  115. /*!
  116. * \fn void G_verbose_message (const char *msg,...)
  117. *
  118. * \brief Print a message to stderr but only if module is in verbose mode
  119. *
  120. * The output format depends on enviroment variables
  121. * GRASS_MESSAGE_FORMAT and GRASS_VERBOSE
  122. *
  123. * \param[in] msg string. Cannot be NULL.
  124. */
  125. void G_verbose_message (const char *msg, ...)
  126. {
  127. if( G_verbose() > G_verbose_std() ) {
  128. va_list ap;
  129. va_start(ap, msg);
  130. vfprint_error (MSG, msg, ap);
  131. va_end(ap);
  132. }
  133. }
  134. /*!
  135. * \fn void G_important_message (const char *msg,...)
  136. *
  137. * \brief Print a message to stderr even in brief mode (verbosity=1)
  138. *
  139. * Ususally just G_percent()/G_clicker() would be shown at this level.
  140. * This allows important non-error/warning messages to display as well.
  141. *
  142. * The output format depends on enviroment variables
  143. * GRASS_MESSAGE_FORMAT and GRASS_VERBOSE
  144. *
  145. * \param[in] msg string. Cannot be NULL.
  146. */
  147. void G_important_message(const char *msg, ...)
  148. {
  149. if( G_verbose() > G_verbose_min() ) {
  150. va_list ap;
  151. va_start(ap, msg);
  152. vfprint_error (MSG, msg, ap);
  153. va_end(ap);
  154. }
  155. }
  156. /*!
  157. * \fn int G_fatal_error (const char *msg,...)
  158. *
  159. * \brief Print a fatal error message to stderr
  160. *
  161. * The output format depends on enviroment variable
  162. * GRASS_MESSAGE_FORMAT
  163. *
  164. * By default, the message is handled by an internal routine which
  165. * prints the message to the screen. Using G_set_error_routine() the
  166. * programmer can have the message handled by another routine. This is
  167. * especially useful if the message should go to a particular location
  168. * on the screen when using curses or to a location on a graphics
  169. * device (monitor).
  170. *
  171. * \return Terminates with an exit status of EXIT_FAILURE if no external
  172. * routine is specified by G_set_error_routine()
  173. *
  174. * \param[in] msg string. Cannot be NULL.
  175. */
  176. int G_fatal_error (const char *msg,...)
  177. {
  178. va_list ap;
  179. va_start(ap,msg);
  180. vfprint_error (ERR, msg, ap);
  181. va_end(ap);
  182. exit (EXIT_FAILURE);
  183. }
  184. /*!
  185. *
  186. * \fn int G_warning (const char *msg, ...)
  187. *
  188. * \brief Print a warning message to stderr
  189. *
  190. * The output format depends on enviroment variable
  191. * GRASS_MESSAGE_FORMAT
  192. *
  193. * A warning message can be suppressed by G_suppress_warnings()
  194. *
  195. * \param[in] msg string. Cannot be NULL.
  196. */
  197. int G_warning (const char *msg, ...)
  198. {
  199. va_list ap;
  200. if (no_warn) return 0;
  201. va_start(ap,msg);
  202. vfprint_error (WARN, msg, ap);
  203. va_end(ap);
  204. return 0;
  205. }
  206. /*!
  207. * \fn int G_suppress_warnings (int flag)
  208. *
  209. * \brief Suppress printing a warning message to stderr
  210. *
  211. * \param[in] flag a warning message will be suppressed if non-zero value is given
  212. * \return previous flag
  213. */
  214. int G_suppress_warnings (int flag)
  215. {
  216. int prev;
  217. prev = no_warn;
  218. no_warn = flag;
  219. return prev;
  220. }
  221. /*!
  222. * \fn int G_sleep_on_error (int flag)
  223. *
  224. * \brief Turn on/off no_sleep flag
  225. *
  226. * \param flag if non-zero/zero value is given G_sleep() will be activated/deactivated
  227. *
  228. * \param[in] flag error message will be suppressed if non-zero value is given
  229. * \return previous flag
  230. */
  231. int G_sleep_on_error (int flag)
  232. {
  233. int prev;
  234. prev = !no_sleep;
  235. no_sleep = !flag;
  236. return prev;
  237. }
  238. /*!
  239. * \fn int G_set_error_routine ( int (*error_routine)(char *, int))
  240. *
  241. * \brief Establishes error_routine as the routine that will handle
  242. * the printing of subsequent error messages.
  243. *
  244. * \param[in] error_routine routine will be called like this: error_routine(msg,
  245. * fatal)
  246. *
  247. * \return always null
  248. */
  249. int G_set_error_routine(int (*error_routine)(const char *, int))
  250. {
  251. ext_error = error_routine; /* Roger Bivand 17 June 2000 */
  252. return 0;
  253. }
  254. /*!
  255. * \fn int G_unset_error_routine (void)
  256. *
  257. * \brief After this call subsequent error messages will be handled in the
  258. * default method.
  259. *
  260. * Error messages are printed directly to the screen: ERROR: message or WARNING: message
  261. *
  262. * \return always returns 0.
  263. */
  264. int G_unset_error_routine(void)
  265. {
  266. ext_error = 0; /* Roger Bivand 17 June 2000 */
  267. return 0;
  268. }
  269. /* Print info to stderr and optionaly to log file and optionaly send mail */
  270. static int print_error(const char *msg, const int type)
  271. {
  272. static char *prefix_std[3];
  273. int fatal, format;
  274. if ( !prefix_std[0] ) { /* First time: set prefixes */
  275. prefix_std[0] = "";
  276. prefix_std[1] = _("WARNING: ");
  277. prefix_std[2] = _("ERROR: ");
  278. }
  279. if ( type == ERR )
  280. fatal = TRUE;
  281. else /* WARN */
  282. fatal = FALSE;
  283. if ( (type == MSG || type == WARN || type == ERR) && ext_error) { /* Function defined by application */
  284. ext_error (msg, fatal);
  285. } else {
  286. char *w;
  287. int len, lead;
  288. format = G_info_format();
  289. if ( format != G_INFO_FORMAT_GUI ) {
  290. if ( type == WARN || type == ERR ) {
  291. log_error (msg, fatal);
  292. }
  293. fprintf(stderr,"%s", prefix_std[type] );
  294. len = lead = strlen ( prefix_std[type] );
  295. w = (char *)msg;
  296. while (print_word(stderr,&w,&len,lead))
  297. ;
  298. if ( (type != MSG) && isatty(fileno(stderr))
  299. && (G_info_format() == G_INFO_FORMAT_STANDARD) ) { /* Bell */
  300. fprintf(stderr,"\7");
  301. fflush (stderr);
  302. if (!no_sleep)
  303. G_sleep (5);
  304. } else if ( (type == WARN || type == ERR) && getenv("GRASS_ERROR_MAIL")) { /* Mail */
  305. mail_msg (msg, fatal);
  306. }
  307. } else { /* GUI */
  308. print_sentence ( stderr, type, msg );
  309. }
  310. }
  311. return 0;
  312. }
  313. static int log_error (const char *msg, int fatal)
  314. {
  315. FILE *pwd;
  316. char cwd[1024];
  317. time_t clock;
  318. char *home;
  319. char *gisbase;
  320. /* get time */
  321. clock = time(NULL);
  322. /* get current working directory */
  323. sprintf(cwd,"?");
  324. if ( (pwd = G_popen ("pwd","r")) )
  325. {
  326. if (fgets(cwd, sizeof(cwd), pwd))
  327. {
  328. char *c;
  329. for (c = cwd; *c; c++)
  330. if (*c == '\n')
  331. *c = 0;
  332. }
  333. G_pclose (pwd);
  334. }
  335. /* write the 2 possible error log files */
  336. if ((gisbase = G_gisbase ()))
  337. write_error (msg, fatal, gisbase, clock, cwd);
  338. home = G__home();
  339. if (home && gisbase && strcmp (home, gisbase))
  340. write_error (msg, fatal, home, clock, cwd);
  341. return 0;
  342. }
  343. /* Write a message to the log file */
  344. static int write_error (const char *msg, int fatal,
  345. const char *dir, time_t clock,
  346. const char *cwd)
  347. {
  348. char logfile[GNAME_MAX];
  349. FILE *log;
  350. if (dir == 0 || *dir == 0)
  351. return 1;
  352. sprintf (logfile, "%s/GIS_ERROR_LOG", dir) ;
  353. log = fopen (logfile,"r");
  354. if (!log)
  355. /* GIS_ERROR_LOG file is not readable or does not exist */
  356. return 1;
  357. log = freopen(logfile, "a", log);
  358. if (!log)
  359. /* the user doesn't have write permission */
  360. return 1;
  361. fprintf(log,"-------------------------------------\n");
  362. fprintf(log,"%-10s %s\n", "program:", G_program_name());
  363. fprintf(log,"%-10s %s\n", "user:", G_whoami());
  364. fprintf(log,"%-10s %s\n", "cwd:", cwd);
  365. fprintf(log,"%-10s %s\n", "date:", ctime(&clock));
  366. fprintf(log,"%-10s %s\n", fatal?"error:":"warning:", msg);
  367. fprintf(log,"-------------------------------------\n");
  368. fclose (log);
  369. return 0;
  370. }
  371. /* Mail a message */
  372. static int mail_msg (const char *msg, int fatal)
  373. {
  374. FILE *mail;
  375. char command[64];
  376. char *user;
  377. user = G_whoami();
  378. if (user == 0 || *user == 0)
  379. return 1;
  380. sprintf (command, "mail '%s'", G_whoami());
  381. if ( (mail = G_popen (command, "w")) )
  382. {
  383. fprintf(mail,"GIS %s: %s\n",fatal?"ERROR":"WARNING",msg);
  384. G_pclose (mail);
  385. }
  386. return 0;
  387. }
  388. /* Print one word, new line if necessary */
  389. static int print_word (FILE *fd, char **word, int *len, const int lead)
  390. {
  391. int wlen, start, totlen;
  392. int nl;
  393. char *w,*b;
  394. start = *len;
  395. w = *word;
  396. nl = 0;
  397. while (*w == ' ' || *w == '\t' || *w == '\n')
  398. if(*w++ == '\n')
  399. nl++;
  400. wlen = 0;
  401. for (b = w; *b != 0 && *b != ' ' && *b != '\t' && *b != '\n'; b++)
  402. wlen++;
  403. if (wlen == 0)
  404. {
  405. fprintf (fd, "\n");
  406. return 0;
  407. }
  408. if ( start > lead ) { /* add space */
  409. totlen = start + wlen + 1;
  410. } else {
  411. totlen = start + wlen;
  412. }
  413. if ( nl != 0 || totlen > 75)
  414. {
  415. while (--nl > 0)
  416. fprintf (fd, "\n");
  417. fprintf (fd, "\n%*s",lead,"");
  418. start = lead;
  419. }
  420. if ( start > lead ) {
  421. fprintf (fd, " ");
  422. start++;
  423. }
  424. *len = start + wlen;
  425. fwrite(w, 1, wlen, fd);
  426. w += wlen;
  427. *word = w;
  428. return 1;
  429. }
  430. /* Print one message, prefix inserted before each new line */
  431. static void print_sentence (FILE *fd, const int type, const char *msg)
  432. {
  433. char prefix[100];
  434. const char *start;
  435. switch ( type ) {
  436. case MSG:
  437. sprintf (prefix, "GRASS_INFO_MESSAGE(%d,%d): ", getpid(), message_id);
  438. break;
  439. case WARN:
  440. sprintf (prefix, "GRASS_INFO_WARNING(%d,%d): ", getpid(), message_id);
  441. break;
  442. case ERR:
  443. sprintf (prefix, "GRASS_INFO_ERROR(%d,%d): ", getpid(), message_id);
  444. break;
  445. }
  446. start = msg;
  447. fprintf(stderr, "\n" );
  448. while ( *start != '\0' ) {
  449. const char *next = start;
  450. fprintf ( fd, "%s", prefix);
  451. while ( *next != '\0' ) {
  452. next++;
  453. if ( *next == '\n' ) {
  454. next++;
  455. break;
  456. }
  457. }
  458. fwrite (start, 1, next - start, fd);
  459. fprintf (fd, "\n" );
  460. start = next;
  461. }
  462. fprintf(stderr, "GRASS_INFO_END(%d,%d)\n", getpid(), message_id );
  463. message_id++;
  464. }
  465. /*!
  466. * \fn int G_info_format ( void )
  467. *
  468. * \brief Get current message format
  469. *
  470. * Maybe set to either "standard" or "gui" (normally GRASS takes care)
  471. *
  472. * \return grass_info_format value
  473. */
  474. int G_info_format ( void )
  475. {
  476. static int grass_info_format = -1;
  477. char *fstr;
  478. if ( grass_info_format < 0) {
  479. fstr = getenv( "GRASS_MESSAGE_FORMAT" );
  480. if ( fstr && G_strcasecmp(fstr,"gui") == 0 )
  481. grass_info_format = G_INFO_FORMAT_GUI;
  482. else if ( fstr && G_strcasecmp(fstr,"silent") == 0 )
  483. grass_info_format = G_INFO_FORMAT_SILENT;
  484. else
  485. grass_info_format = G_INFO_FORMAT_STANDARD;
  486. }
  487. return grass_info_format;
  488. }