error.c 11 KB

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