error.c 11 KB

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