logging.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_log_output = false;
  14. /* per default priorities higher than LOG_NOTICE are logged */
  15. int opt_log_level = LOG_NOTICE;
  16. static void my_log_curses(__maybe_unused int prio, char *f, va_list ap)
  17. {
  18. if (opt_quiet && prio != LOG_ERR)
  19. return;
  20. #ifdef HAVE_CURSES
  21. extern bool use_curses;
  22. if (use_curses && log_curses_only(prio, f, ap))
  23. ;
  24. else
  25. #endif
  26. {
  27. int len = strlen(f);
  28. int cancelstate;
  29. bool scs;
  30. strcpy(f + len - 1, " \n");
  31. scs = !pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
  32. mutex_lock(&console_lock);
  33. vprintf(f, ap);
  34. mutex_unlock(&console_lock);
  35. if (scs)
  36. pthread_setcancelstate(cancelstate, &cancelstate);
  37. }
  38. }
  39. static void log_generic(int prio, const char *fmt, va_list ap);
  40. void vapplog(int prio, const char *fmt, va_list ap)
  41. {
  42. if (!opt_debug && prio == LOG_DEBUG)
  43. return;
  44. if (use_syslog || opt_log_output || prio <= LOG_NOTICE)
  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. char *f;
  70. int len;
  71. struct timeval tv = {0, 0};
  72. struct tm *tm;
  73. gettimeofday(&tv, NULL);
  74. tm = localtime(&tv.tv_sec);
  75. len = 40 + strlen(fmt) + 22;
  76. f = alloca(len);
  77. sprintf(f, " [%d-%02d-%02d %02d:%02d:%02d] %s\n",
  78. tm->tm_year + 1900,
  79. tm->tm_mon + 1,
  80. tm->tm_mday,
  81. tm->tm_hour,
  82. tm->tm_min,
  83. tm->tm_sec,
  84. fmt);
  85. /* Only output to stderr if it's not going to the screen as well */
  86. if (!isatty(fileno((FILE *)stderr))) {
  87. va_list apc;
  88. va_copy(apc, ap);
  89. vfprintf(stderr, f, apc); /* atomic write to stderr */
  90. fflush(stderr);
  91. }
  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. }