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. bool opt_debug = false;
  16. bool opt_debug_console = false; // Only used if opt_debug is also enabled
  17. bool opt_log_output = false;
  18. /* per default priorities higher than LOG_NOTICE are logged */
  19. int opt_log_level = LOG_NOTICE;
  20. static void my_log_curses(int prio, char *f, va_list ap) FORMAT_SYNTAX_CHECK(printf, 2, 0);
  21. static void my_log_curses(__maybe_unused int prio, char *f, va_list ap)
  22. {
  23. if (opt_quiet && prio != LOG_ERR)
  24. return;
  25. #ifdef HAVE_CURSES
  26. extern bool use_curses;
  27. if (use_curses && log_curses_only(prio, f, ap))
  28. ;
  29. else
  30. #endif
  31. {
  32. int len = strlen(f);
  33. int cancelstate;
  34. bool scs;
  35. strcpy(f + len - 1, " \n");
  36. scs = !pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
  37. mutex_lock(&console_lock);
  38. vprintf(f, ap);
  39. mutex_unlock(&console_lock);
  40. if (scs)
  41. pthread_setcancelstate(cancelstate, &cancelstate);
  42. }
  43. }
  44. static void log_generic(int prio, const char *fmt, va_list ap) FORMAT_SYNTAX_CHECK(printf, 2, 0);
  45. void vapplog(int prio, const char *fmt, va_list ap)
  46. {
  47. if (!opt_debug && prio == LOG_DEBUG)
  48. return;
  49. log_generic(prio, fmt, ap);
  50. }
  51. void applog(int prio, const char *fmt, ...)
  52. {
  53. va_list ap;
  54. va_start(ap, fmt);
  55. vapplog(prio, fmt, ap);
  56. va_end(ap);
  57. }
  58. /* high-level logging functions, based on global opt_log_level */
  59. /*
  60. * generic log function used by priority specific ones
  61. * equals vapplog() without additional priority checks
  62. */
  63. static void log_generic(int prio, const char *fmt, va_list ap)
  64. {
  65. #ifdef HAVE_SYSLOG_H
  66. if (use_syslog) {
  67. vsyslog(prio, fmt, ap);
  68. }
  69. #else
  70. if (0) {}
  71. #endif
  72. else {
  73. bool writetocon = opt_debug_console || (opt_log_output && prio != LOG_DEBUG) || prio <= LOG_NOTICE;
  74. bool writetofile = !isatty(fileno((FILE *)stderr));
  75. if (!(writetocon || writetofile))
  76. return;
  77. char *f;
  78. int len;
  79. struct timeval tv = {0, 0};
  80. struct tm _tm;
  81. struct tm *tm = &_tm;
  82. gettimeofday(&tv, NULL);
  83. localtime_r(&tv.tv_sec, tm);
  84. len = 40 + strlen(fmt) + 22;
  85. f = alloca(len);
  86. sprintf(f, " [%d-%02d-%02d %02d:%02d:%02d] %s\n",
  87. tm->tm_year + 1900,
  88. tm->tm_mon + 1,
  89. tm->tm_mday,
  90. tm->tm_hour,
  91. tm->tm_min,
  92. tm->tm_sec,
  93. fmt);
  94. /* Only output to stderr if it's not going to the screen as well */
  95. if (writetofile) {
  96. va_list apc;
  97. va_copy(apc, ap);
  98. vfprintf(stderr, f, apc); /* atomic write to stderr */
  99. va_end(apc);
  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. }