logging.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2011-2012 Con Kolivas
  3. * Copyright 2012-2013 Luke Dashjr
  4. * Copyright 2013 Andrew Smith
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #include <unistd.h>
  13. #include "compat.h"
  14. #include "logging.h"
  15. #include "miner.h"
  16. bool opt_debug = false;
  17. bool opt_debug_console = false; // Only used if opt_debug is also enabled
  18. bool opt_log_output = false;
  19. bool opt_log_microseconds;
  20. /* per default priorities higher than LOG_NOTICE are logged */
  21. int opt_log_level = LOG_NOTICE;
  22. static void my_log_curses(int prio, const char *datetime, const char *str)
  23. {
  24. if (opt_quiet && prio != LOG_ERR)
  25. return;
  26. #ifdef HAVE_CURSES
  27. extern bool use_curses;
  28. if (use_curses && log_curses_only(prio, datetime, str))
  29. ;
  30. else
  31. #endif
  32. {
  33. int cancelstate;
  34. bool scs;
  35. scs = !pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelstate);
  36. mutex_lock(&console_lock);
  37. printf("%s%s%s", datetime, str, " \n");
  38. mutex_unlock(&console_lock);
  39. if (scs)
  40. pthread_setcancelstate(cancelstate, &cancelstate);
  41. }
  42. }
  43. /* high-level logging function, based on global opt_log_level */
  44. /*
  45. * log function
  46. */
  47. void _applog(int prio, const char *str)
  48. {
  49. #ifdef HAVE_SYSLOG_H
  50. if (use_syslog) {
  51. syslog(prio, "%s", str);
  52. }
  53. #else
  54. if (0) {}
  55. #endif
  56. else {
  57. bool writetocon = opt_debug_console || (opt_log_output && prio != LOG_DEBUG) || prio <= LOG_NOTICE;
  58. bool writetofile = !isatty(fileno((FILE *)stderr));
  59. if (!(writetocon || writetofile))
  60. return;
  61. char datetime[64];
  62. struct timeval tv = {0, 0};
  63. struct tm _tm;
  64. struct tm *tm = &_tm;
  65. cgtime(&tv);
  66. localtime_r(&tv.tv_sec, tm);
  67. if (opt_log_microseconds)
  68. sprintf(datetime, " [%d-%02d-%02d %02d:%02d:%02d.%06ld] ",
  69. tm->tm_year + 1900,
  70. tm->tm_mon + 1,
  71. tm->tm_mday,
  72. tm->tm_hour,
  73. tm->tm_min,
  74. tm->tm_sec,
  75. (long)tv.tv_usec);
  76. else
  77. sprintf(datetime, " [%d-%02d-%02d %02d:%02d:%02d] ",
  78. tm->tm_year + 1900,
  79. tm->tm_mon + 1,
  80. tm->tm_mday,
  81. tm->tm_hour,
  82. tm->tm_min,
  83. tm->tm_sec);
  84. /* Only output to stderr if it's not going to the screen as well */
  85. if (writetofile) {
  86. fprintf(stderr, "%s%s\n", datetime, str); /* atomic write to stderr */
  87. fflush(stderr);
  88. }
  89. if (writetocon)
  90. my_log_curses(prio, datetime, str);
  91. }
  92. }