logging.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #ifdef HAVE_CURSES
  19. extern bool use_curses;
  20. if (use_curses)
  21. log_curses(prio, f, ap);
  22. else
  23. #endif
  24. {
  25. int len = strlen(f);
  26. strcpy(f + len - 1, " \n");
  27. #ifdef HAVE_CURSES
  28. log_curses(prio, f, ap);
  29. #else
  30. vprintf(f, ap);
  31. #endif
  32. }
  33. }
  34. static void log_generic(int prio, const char *fmt, va_list ap);
  35. void vapplog(int prio, const char *fmt, va_list ap)
  36. {
  37. if (!opt_debug && prio == LOG_DEBUG)
  38. return;
  39. if (use_syslog || opt_log_output || prio <= LOG_NOTICE)
  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. char *f;
  65. int len;
  66. struct timeval tv = {0, 0};
  67. struct tm *tm;
  68. gettimeofday(&tv, NULL);
  69. tm = localtime(&tv.tv_sec);
  70. len = 40 + strlen(fmt) + 22;
  71. f = alloca(len);
  72. sprintf(f, " [%d-%02d-%02d %02d:%02d:%02d] %s\n",
  73. tm->tm_year + 1900,
  74. tm->tm_mon + 1,
  75. tm->tm_mday,
  76. tm->tm_hour,
  77. tm->tm_min,
  78. tm->tm_sec,
  79. fmt);
  80. /* Only output to stderr if it's not going to the screen as well */
  81. if (!isatty(fileno((FILE *)stderr))) {
  82. va_list apc;
  83. va_copy(apc, ap);
  84. vfprintf(stderr, f, apc); /* atomic write to stderr */
  85. fflush(stderr);
  86. }
  87. my_log_curses(prio, f, ap);
  88. }
  89. }
  90. /* we can not generalize variable argument list */
  91. #define LOG_TEMPLATE(PRIO) \
  92. if (PRIO <= opt_log_level) { \
  93. va_list ap; \
  94. va_start(ap, fmt); \
  95. vapplog(PRIO, fmt, ap); \
  96. va_end(ap); \
  97. }
  98. void log_error(const char *fmt, ...)
  99. {
  100. LOG_TEMPLATE(LOG_ERR);
  101. }
  102. void log_warning(const char *fmt, ...)
  103. {
  104. LOG_TEMPLATE(LOG_WARNING);
  105. }
  106. void log_notice(const char *fmt, ...)
  107. {
  108. LOG_TEMPLATE(LOG_NOTICE);
  109. }
  110. void log_info(const char *fmt, ...)
  111. {
  112. LOG_TEMPLATE(LOG_INFO);
  113. }
  114. void log_debug(const char *fmt, ...)
  115. {
  116. LOG_TEMPLATE(LOG_DEBUG);
  117. }