logging.c 3.1 KB

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