logging.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2011-2012 Con Kolivas
  3. * Copyright 2013 Andrew Smith
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <unistd.h>
  12. #include "logging.h"
  13. #include "miner.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(int prio, const char *datetime, const char *str, bool force)
  19. {
  20. if (opt_quiet && prio != LOG_ERR)
  21. return;
  22. /* Mutex could be locked by dead thread on shutdown so forcelog will
  23. * invalidate any console lock status. */
  24. if (force) {
  25. mutex_trylock(&console_lock);
  26. mutex_unlock(&console_lock);
  27. }
  28. #ifdef HAVE_CURSES
  29. extern bool use_curses;
  30. if (use_curses && log_curses_only(prio, datetime, str))
  31. ;
  32. else
  33. #endif
  34. {
  35. mutex_lock(&console_lock);
  36. printf("%s%s%s", datetime, str, " \n");
  37. mutex_unlock(&console_lock);
  38. }
  39. }
  40. /* high-level logging function, based on global opt_log_level */
  41. /*
  42. * log function
  43. */
  44. void _applog(int prio, const char *str, bool force)
  45. {
  46. #ifdef HAVE_SYSLOG_H
  47. if (use_syslog) {
  48. syslog(LOG_LOCAL0 | prio, "%s", str);
  49. }
  50. #else
  51. if (0) {}
  52. #endif
  53. else {
  54. char datetime[64];
  55. struct timeval tv = {0, 0};
  56. struct tm *tm;
  57. cgtime(&tv);
  58. const time_t tmp_time = tv.tv_sec;
  59. tm = localtime(&tmp_time);
  60. snprintf(datetime, sizeof(datetime), " [%d-%02d-%02d %02d:%02d:%02d] ",
  61. tm->tm_year + 1900,
  62. tm->tm_mon + 1,
  63. tm->tm_mday,
  64. tm->tm_hour,
  65. tm->tm_min,
  66. tm->tm_sec);
  67. /* Only output to stderr if it's not going to the screen as well */
  68. if (!isatty(fileno((FILE *)stderr))) {
  69. fprintf(stderr, "%s%s\n", datetime, str); /* atomic write to stderr */
  70. fflush(stderr);
  71. }
  72. my_log_curses(prio, datetime, str, force);
  73. }
  74. }
  75. void _simplelog(int prio, const char *str, bool force)
  76. {
  77. #ifdef HAVE_SYSLOG_H
  78. if (use_syslog) {
  79. syslog(LOG_LOCAL0 | prio, "%s", str);
  80. }
  81. #else
  82. if (0) {}
  83. #endif
  84. else {
  85. /* Only output to stderr if it's not going to the screen as well */
  86. if (!isatty(fileno((FILE *)stderr))) {
  87. fprintf(stderr, "%s\n", str); /* atomic write to stderr */
  88. fflush(stderr);
  89. }
  90. my_log_curses(prio, "", str, force);
  91. }
  92. }