logging.c 2.6 KB

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