logging.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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)
  19. {
  20. if (opt_quiet && prio != LOG_ERR)
  21. return;
  22. #ifdef HAVE_CURSES
  23. extern bool use_curses;
  24. if (use_curses && log_curses_only(prio, datetime, str))
  25. ;
  26. else
  27. #endif
  28. {
  29. mutex_lock(&console_lock);
  30. printf("%s%s%s", datetime, str, " \n");
  31. mutex_unlock(&console_lock);
  32. }
  33. }
  34. /* high-level logging function, based on global opt_log_level */
  35. /*
  36. * log function
  37. */
  38. void _applog(int prio, const char *str)
  39. {
  40. #ifdef HAVE_SYSLOG_H
  41. if (use_syslog) {
  42. syslog(prio, "%s", str);
  43. }
  44. #else
  45. if (0) {}
  46. #endif
  47. else {
  48. char datetime[64];
  49. struct timeval tv = {0, 0};
  50. struct tm *tm;
  51. cgtime(&tv);
  52. const time_t tmp_time = tv.tv_sec;
  53. tm = localtime(&tmp_time);
  54. snprintf(datetime, sizeof(datetime), " [%d-%02d-%02d %02d:%02d:%02d] ",
  55. tm->tm_year + 1900,
  56. tm->tm_mon + 1,
  57. tm->tm_mday,
  58. tm->tm_hour,
  59. tm->tm_min,
  60. tm->tm_sec);
  61. /* Only output to stderr if it's not going to the screen as well */
  62. if (!isatty(fileno((FILE *)stderr))) {
  63. fprintf(stderr, "%s%s\n", datetime, str); /* atomic write to stderr */
  64. fflush(stderr);
  65. }
  66. my_log_curses(prio, datetime, str);
  67. }
  68. }