PageRenderTime 87ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/error/error.c

https://github.com/agustingianni/StreamingProject
C | 121 lines | 77 code | 27 blank | 17 comment | 2 complexity | 2bd056aedd6ec9753b52ed6e125a8596 MD5 | raw file
  1. /*
  2. * error.c
  3. *
  4. * Created on: Jul 16, 2009
  5. * Author: gr00vy
  6. */
  7. #include <stdarg.h> /* ANSI C header file */
  8. #include <syslog.h> /* for syslog() */
  9. /* si el proceso es un daemon mandar todo a syslog */
  10. int daemon_proc = 0;
  11. static void error_doit(int, int, const char *, va_list);
  12. /* Nonfatal error related to system call */
  13. void
  14. error_ret(const char *fmt, ...)
  15. {
  16. va_list ap;
  17. va_start(ap, fmt);
  18. error_doit(1, LOG_INFO, fmt, ap);
  19. va_end(ap);
  20. return;
  21. }
  22. /* Fatal error related to system call */
  23. void
  24. error_sys(const char *fmt, ...)
  25. {
  26. va_list ap;
  27. va_start(ap, fmt);
  28. error_doit(1, LOG_ERR, fmt, ap);
  29. va_end(ap);
  30. exit(1);
  31. }
  32. /* Fatal error related to system call
  33. * Print message, dump core, and terminate */
  34. void
  35. error_dump(const char *fmt, ...)
  36. {
  37. va_list ap;
  38. va_start(ap, fmt);
  39. error_doit(1, LOG_ERR, fmt, ap);
  40. va_end(ap);
  41. abort(); /* dump core and terminate */
  42. exit(1); /* shouldn't get here */
  43. }
  44. /* Nonfatal error unrelated to system call
  45. * Print message and return */
  46. void
  47. error_msg(const char *fmt, ...)
  48. {
  49. va_list ap;
  50. va_start(ap, fmt);
  51. error_doit(0, LOG_INFO, fmt, ap);
  52. va_end(ap);
  53. return;
  54. }
  55. /* Fatal error unrelated to system call
  56. * Print message and terminate */
  57. void
  58. error_quit(const char *fmt, ...)
  59. {
  60. va_list ap;
  61. va_start(ap, fmt);
  62. error_doit(0, LOG_ERR, fmt, ap);
  63. va_end(ap);
  64. exit(1);
  65. }
  66. /* Print message and return to caller
  67. * Caller specifies "errnoflag" and "level" */
  68. static void
  69. error_doit(int errnoflag, int level, const char *fmt, va_list ap)
  70. {
  71. int errno_save, n;
  72. char buf[MAXLINE + 1];
  73. errno_save = errno; /* value caller might want printed */
  74. #ifdef HAVE_VSNPRINTF
  75. vsnprintf(buf, MAXLINE, fmt, ap); * safe */
  76. #else
  77. vsprintf(buf, fmt, ap); /* not safe */
  78. #endif
  79. n = strlen(buf);
  80. if (errnoflag)
  81. snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save));
  82. strcat(buf, "\n");
  83. if (daemon_proc)
  84. {
  85. syslog(level, buf);
  86. }
  87. else
  88. {
  89. fflush(stdout); /* in case stdout and stderr are the same */
  90. fputs(buf, stderr);
  91. fflush(stderr);
  92. }
  93. return;
  94. }