logging.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2011-2012 Con Kolivas
  3. * Copyright 2012-2013 Luke Dashjr
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <unistd.h>
  12. #include "compat.h"
  13. #include "logging.h"
  14. #include "miner.h"
  15. // #include "util.h"
  16. bool opt_debug = false;
  17. bool opt_debug_console = false; // Only used if opt_debug is also enabled
  18. bool opt_log_output = false;
  19. bool opt_log_microseconds;
  20. /* per default priorities higher than LOG_NOTICE are logged */
  21. int opt_log_level = LOG_NOTICE;
  22. static void my_log_curses(int prio, char *f, va_list ap) FORMAT_SYNTAX_CHECK(printf, 2, 0);
  23. static void my_log_curses(__maybe_unused int prio, char *f, va_list ap)
  24. {
  25. if (opt_quiet && prio != LOG_ERR)
  26. return;
  27. #ifdef HAVE_CURSES
  28. extern bool use_curses;
  29. if (use_curses && log_curses_only(prio, f, ap))
  30. ;
  31. else
  32. #endif
  33. {
  34. int len = strlen(f);
  35. int cancelstate;
  36. bool scs;
  37. strcpy(f + len - 1, " \n");
  38. scs = !pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
  39. mutex_lock(&console_lock);
  40. vprintf(f, ap);
  41. mutex_unlock(&console_lock);
  42. if (scs)
  43. pthread_setcancelstate(cancelstate, &cancelstate);
  44. }
  45. }
  46. static void log_generic(int prio, const char *fmt, va_list ap) FORMAT_SYNTAX_CHECK(printf, 2, 0);
  47. void vapplog(int prio, const char *fmt, va_list ap)
  48. {
  49. if (!opt_debug && prio == LOG_DEBUG)
  50. return;
  51. log_generic(prio, fmt, ap);
  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 log_generic(int prio, const char *fmt, va_list ap)
  66. {
  67. #ifdef HAVE_SYSLOG_H
  68. if (use_syslog) {
  69. vsyslog(prio, fmt, ap);
  70. }
  71. #else
  72. if (0) {}
  73. #endif
  74. else {
  75. bool writetocon = opt_debug_console || (opt_log_output && prio != LOG_DEBUG) || prio <= LOG_NOTICE;
  76. bool writetofile = !isatty(fileno((FILE *)stderr));
  77. if (!(writetocon || writetofile))
  78. return;
  79. char *f;
  80. int len;
  81. struct timeval tv = {0, 0};
  82. struct tm _tm;
  83. struct tm *tm = &_tm;
  84. cgtime(&tv);
  85. localtime_r(&tv.tv_sec, tm);
  86. // NOTE: my_log_curses appends 20 spaces
  87. len = 40 + strlen(fmt) + 22 + 7 /* microseconds */;
  88. f = alloca(len);
  89. if (opt_log_microseconds)
  90. sprintf(f, " [%d-%02d-%02d %02d:%02d:%02d.%06ld] %s\n",
  91. tm->tm_year + 1900,
  92. tm->tm_mon + 1,
  93. tm->tm_mday,
  94. tm->tm_hour,
  95. tm->tm_min,
  96. tm->tm_sec,
  97. (long)tv.tv_usec,
  98. fmt);
  99. else
  100. sprintf(f, " [%d-%02d-%02d %02d:%02d:%02d] %s\n",
  101. tm->tm_year + 1900,
  102. tm->tm_mon + 1,
  103. tm->tm_mday,
  104. tm->tm_hour,
  105. tm->tm_min,
  106. tm->tm_sec,
  107. fmt);
  108. /* Only output to stderr if it's not going to the screen as well */
  109. if (writetofile) {
  110. va_list apc;
  111. va_copy(apc, ap);
  112. vfprintf(stderr, f, apc); /* atomic write to stderr */
  113. va_end(apc);
  114. fflush(stderr);
  115. }
  116. if (writetocon)
  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. }