error.c 11 KB

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