error.c 11 KB

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