logging.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_real(&tv);
  58. const time_t tmp_time = tv.tv_sec;
  59. int ms = (int)(tv.tv_usec / 1000);
  60. tm = localtime(&tmp_time);
  61. snprintf(datetime, sizeof(datetime), " [%d-%02d-%02d %02d:%02d:%02d.%03d] ",
  62. tm->tm_year + 1900,
  63. tm->tm_mon + 1,
  64. tm->tm_mday,
  65. tm->tm_hour,
  66. tm->tm_min,
  67. tm->tm_sec, ms);
  68. /* Only output to stderr if it's not going to the screen as well */
  69. if (!isatty(fileno((FILE *)stderr))) {
  70. fprintf(stderr, "%s%s\n", datetime, str); /* atomic write to stderr */
  71. fflush(stderr);
  72. }
  73. my_log_curses(prio, datetime, str, force);
  74. }
  75. }
  76. void _simplelog(int prio, const char *str, bool force)
  77. {
  78. #ifdef HAVE_SYSLOG_H
  79. if (use_syslog) {
  80. syslog(LOG_LOCAL0 | prio, "%s", str);
  81. }
  82. #else
  83. if (0) {}
  84. #endif
  85. else {
  86. /* Only output to stderr if it's not going to the screen as well */
  87. if (!isatty(fileno((FILE *)stderr))) {
  88. fprintf(stderr, "%s\n", str); /* atomic write to stderr */
  89. fflush(stderr);
  90. }
  91. my_log_curses(prio, "", str, force);
  92. }
  93. }