logging.c 3.3 KB

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