logging.c 3.0 KB

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