log.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <syslog.h>
  6. #include <time.h>
  7. #include "log.h"
  8. /*
  9. * The minimum log level; set to one of L_* constants from log.h
  10. */
  11. int min_log_level = 666;
  12. /*
  13. * 0: send output to stderr
  14. * 1: send output to syslog LOG_LOCAL1 facility
  15. */
  16. int use_syslog = 0;
  17. /*
  18. * 0: don't display toxcore TRACE messages
  19. * 1: display all toxcore messages
  20. */
  21. int log_tox_trace = 0;
  22. /* Turn log level number to a printable string */
  23. char *log_printable_level(int level)
  24. {
  25. switch(level)
  26. {
  27. case L_ERROR:
  28. return "ERROR";
  29. case L_WARNING:
  30. return "WARNING";
  31. case L_NOTICE:
  32. return "NOTICE";
  33. case L_INFO:
  34. return "INFO";
  35. case L_DEBUG:
  36. return "DEBUG";
  37. case L_DEBUG2:
  38. return "DEBUG2";
  39. }
  40. return "UNKNOWN";
  41. }
  42. void log_init(void)
  43. {
  44. if(use_syslog)
  45. {
  46. openlog("tuntox", LOG_PID, LOG_LOCAL1);
  47. }
  48. }
  49. void log_close(void)
  50. {
  51. if(use_syslog)
  52. {
  53. closelog();
  54. }
  55. }
  56. /* Output the log to the console */
  57. void log_printf(int level, const char *fmt, ...)
  58. {
  59. va_list args;
  60. char logfmt[2048];
  61. char logtime[100];
  62. char *level_str;
  63. time_t rawtime;
  64. struct tm *timeinfo;
  65. if(level > min_log_level)
  66. {
  67. return;
  68. }
  69. if(!use_syslog)
  70. {
  71. time(&rawtime);
  72. timeinfo = localtime(&rawtime);
  73. strftime(logtime, 100, "%F %X", timeinfo);
  74. level_str = log_printable_level(level);
  75. if(fmt[strlen(fmt)-1] == '\n')
  76. {
  77. snprintf(logfmt, 2048, "%s: [%s]\t%s", logtime, level_str, fmt);
  78. }
  79. else
  80. {
  81. snprintf(logfmt, 2048, "%s: [%s]\t%s\n", logtime, level_str, fmt);
  82. }
  83. va_start(args, fmt);
  84. vfprintf(stderr, logfmt, args);
  85. va_end(args);
  86. }
  87. else
  88. {
  89. va_start(args, fmt);
  90. vsyslog(LOG_MAKEPRI(LOG_LOCAL1, level), fmt, args);
  91. va_end(args);
  92. }
  93. }
  94. void log_test(void)
  95. {
  96. int i = 112;
  97. char *x = "test";
  98. log_printf(L_WARNING, "Testing");
  99. log_printf(L_ERROR, "Number stodwadziesciatrzy: %d", 123);
  100. d(beenthere);
  101. dd(i);
  102. dp(&i);
  103. ds(x);
  104. }
  105. static const char *tox_log_level_name(TOX_LOG_LEVEL level)
  106. {
  107. switch (level) {
  108. case TOX_LOG_LEVEL_TRACE:
  109. return "TRACE";
  110. case TOX_LOG_LEVEL_DEBUG:
  111. return "DEBUG";
  112. case TOX_LOG_LEVEL_INFO:
  113. return "INFO";
  114. case TOX_LOG_LEVEL_WARNING:
  115. return "WARNING";
  116. case TOX_LOG_LEVEL_ERROR:
  117. return "ERROR";
  118. }
  119. return "UNKNOWN";
  120. }
  121. void on_tox_log(Tox *tox, TOX_LOG_LEVEL level, const char *path, uint32_t line, const char *func,
  122. const char *message, void *user_data)
  123. {
  124. uint32_t index = user_data ? *(uint32_t *)user_data : 0;
  125. const char *file = strrchr(path, '/');
  126. if(level == TOX_LOG_LEVEL_TRACE && !log_tox_trace)
  127. {
  128. return;
  129. }
  130. file = file ? file + 1 : path;
  131. log_printf(L_DEBUG2, "[#%d] %s %s:%d\t%s:\t%s\n", index, tox_log_level_name(level), file, line, func, message);
  132. }