logging.c 3.2 KB

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