logging.c 2.8 KB

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