logging.c 3.4 KB

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