logging.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <unistd.h>
  2. #include "logging.h"
  3. #include "miner.h"
  4. bool opt_debug = false;
  5. bool opt_log_output = false;
  6. /* per default priorities higher than LOG_NOTICE are logged */
  7. int opt_log_level = LOG_NOTICE;
  8. void vapplog(int prio, const char *fmt, va_list ap)
  9. {
  10. extern bool use_curses;
  11. if (!opt_debug && prio == LOG_DEBUG)
  12. return;
  13. #ifdef HAVE_SYSLOG_H
  14. if (use_syslog) {
  15. vsyslog(prio, fmt, ap);
  16. }
  17. #else
  18. if (0) {}
  19. #endif
  20. else if (opt_log_output || prio <= LOG_NOTICE) {
  21. char *f;
  22. int len;
  23. struct timeval tv = {0, 0};
  24. struct tm *tm;
  25. gettimeofday(&tv, NULL);
  26. tm = localtime(&tv.tv_sec);
  27. len = 40 + strlen(fmt) + 22;
  28. f = alloca(len);
  29. sprintf(f, "[%d-%02d-%02d %02d:%02d:%02d] %s\n",
  30. tm->tm_year + 1900,
  31. tm->tm_mon + 1,
  32. tm->tm_mday,
  33. tm->tm_hour,
  34. tm->tm_min,
  35. tm->tm_sec,
  36. fmt);
  37. /* Only output to stderr if it's not going to the screen as well */
  38. if (!isatty(fileno((FILE *)stderr))) {
  39. va_list apc;
  40. va_copy(apc, ap);
  41. vfprintf(stderr, f, apc); /* atomic write to stderr */
  42. fflush(stderr);
  43. }
  44. if (use_curses)
  45. log_curses(prio, f, ap);
  46. else {
  47. int len = strlen(f);
  48. strcpy(f + len - 1, " \n");
  49. log_curses(prio, f, ap);
  50. }
  51. }
  52. }
  53. void applog(int prio, const char *fmt, ...)
  54. {
  55. va_list ap;
  56. va_start(ap, fmt);
  57. vapplog(prio, fmt, ap);
  58. va_end(ap);
  59. }
  60. /* high-level logging functions, based on global opt_log_level */
  61. /*
  62. * generic log function used by priority specific ones
  63. * equals vapplog() without additional priority checks
  64. */
  65. static void __maybe_unused log_generic(int prio, const char *fmt, va_list ap)
  66. {
  67. extern bool use_curses;
  68. #ifdef HAVE_SYSLOG_H
  69. if (use_syslog) {
  70. vsyslog(prio, fmt, ap);
  71. }
  72. #else
  73. if (0) {}
  74. #endif
  75. else {
  76. char *f;
  77. int len;
  78. struct timeval tv = {0, 0};
  79. struct tm *tm;
  80. gettimeofday(&tv, NULL);
  81. tm = localtime(&tv.tv_sec);
  82. len = 40 + strlen(fmt) + 22;
  83. f = alloca(len);
  84. sprintf(f, "[%d-%02d-%02d %02d:%02d:%02d] %s\n",
  85. tm->tm_year + 1900,
  86. tm->tm_mon + 1,
  87. tm->tm_mday,
  88. tm->tm_hour,
  89. tm->tm_min,
  90. tm->tm_sec,
  91. fmt);
  92. /* Only output to stderr if it's not going to the screen as well */
  93. if (!isatty(fileno((FILE *)stderr))) {
  94. va_list apc;
  95. va_copy(apc, ap);
  96. vfprintf(stderr, f, apc); /* atomic write to stderr */
  97. fflush(stderr);
  98. }
  99. if (use_curses)
  100. log_curses(prio, f, ap);
  101. else {
  102. int len = strlen(f);
  103. strcpy(f + len - 1, " \n");
  104. log_curses(prio, f, ap);
  105. }
  106. }
  107. }
  108. /* we can not generalize variable argument list */
  109. #define LOG_TEMPLATE(PRIO) \
  110. if (PRIO <= opt_log_level) { \
  111. va_list ap; \
  112. va_start(ap, fmt); \
  113. vapplog(PRIO, fmt, ap); \
  114. va_end(ap); \
  115. }
  116. void log_error(const char *fmt, ...)
  117. {
  118. LOG_TEMPLATE(LOG_ERR);
  119. }
  120. void log_warning(const char *fmt, ...)
  121. {
  122. LOG_TEMPLATE(LOG_WARNING);
  123. }
  124. void log_notice(const char *fmt, ...)
  125. {
  126. LOG_TEMPLATE(LOG_NOTICE);
  127. }
  128. void log_info(const char *fmt, ...)
  129. {
  130. LOG_TEMPLATE(LOG_INFO);
  131. }
  132. void log_debug(const char *fmt, ...)
  133. {
  134. LOG_TEMPLATE(LOG_DEBUG);
  135. }