error.c 11 KB

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