logging.c 2.8 KB

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