error.c 11 KB

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