logging.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifdef HAVE_CURSES
  25. extern bool use_curses;
  26. if (use_curses && _log_curses_only(prio, datetime, str))
  27. ;
  28. else
  29. #endif
  30. printf(" %s %s%s", datetime, str, " \n");
  31. }
  32. /* high-level logging function, based on global opt_log_level */
  33. /*
  34. * log function
  35. */
  36. void _applog(int prio, const char *str)
  37. {
  38. #ifdef HAVE_SYSLOG_H
  39. if (use_syslog) {
  40. syslog(prio, "%s", str);
  41. }
  42. #else
  43. if (0) {}
  44. #endif
  45. else {
  46. bool writetocon =
  47. (opt_debug_console || (opt_log_output && prio != LOG_DEBUG) || prio <= LOG_NOTICE)
  48. && !(opt_quiet && prio != LOG_ERR);
  49. bool writetofile = !isatty(fileno((FILE *)stderr));
  50. if (!(writetocon || writetofile))
  51. return;
  52. char datetime[64];
  53. if (opt_log_microseconds)
  54. {
  55. struct timeval tv;
  56. bfg_init_time();
  57. bfg_gettimeofday(&tv);
  58. format_timestamp(datetime, BTF_DATE | BTF_HRTIME | BTF_BRACKETS, &tv);
  59. }
  60. else
  61. get_now_datestamp(datetime);
  62. if (writetofile || writetocon)
  63. {
  64. bfg_console_lock();
  65. /* Only output to stderr if it's not going to the screen as well */
  66. if (writetofile) {
  67. fprintf(stderr, " %s %s\n", datetime, str); /* atomic write to stderr */
  68. fflush(stderr);
  69. }
  70. if (writetocon)
  71. _my_log_curses(prio, datetime, str);
  72. bfg_console_unlock();
  73. }
  74. }
  75. }