logging.c 3.3 KB

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