log.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "../or/or.h"
  5. #include "util.h"
  6. struct logfile_t;
  7. typedef struct logfile_t {
  8. struct logfile_t *next;
  9. const char *filename;
  10. FILE *file;
  11. int needs_close;
  12. int loglevel;
  13. int max_loglevel;
  14. } logfile_t;
  15. static INLINE const char *sev_to_string(int severity) {
  16. switch(severity) {
  17. case LOG_DEBUG: return "debug";
  18. case LOG_INFO: return "info";
  19. case LOG_WARNING: return "warn";
  20. case LOG_ERR: return "err";
  21. default: assert(0); return "UNKNOWN";
  22. }
  23. }
  24. static int loglevel = LOG_DEBUG;
  25. static logfile_t *logfiles = NULL;
  26. /* Format a log message into a fixed-sized buffer. (This is factored out
  27. * of 'logv' so that we never format a message more than once.
  28. */
  29. static INLINE void format_msg(char *buf, size_t buf_len,
  30. int severity, const char *funcname,
  31. const char *format, va_list ap)
  32. {
  33. time_t t;
  34. struct timeval now;
  35. int n;
  36. buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
  37. my_gettimeofday(&now);
  38. t = (time_t)now.tv_sec;
  39. n = strftime(buf, buf_len, "%b %d %H:%M:%S", localtime(&t));
  40. n += snprintf(buf+n, buf_len-n,
  41. ".%.3ld [%s] ",
  42. (long)now.tv_usec / 1000, sev_to_string(severity));
  43. if(n > buf_len)
  44. n = buf_len-1; /* the *nprintf funcs return how many bytes they
  45. * _would_ print, if the output is truncated.
  46. * Subtract one because the count doesn't include the \0 */
  47. if (funcname) {
  48. n += snprintf(buf+n, buf_len-n, "%s(): ", funcname);
  49. if(n > buf_len)
  50. n = buf_len-1;
  51. }
  52. n += vsnprintf(buf+n,buf_len-n,format,ap);
  53. if(n > buf_len)
  54. n = buf_len-1;
  55. buf[n]='\n';
  56. buf[n+1]='\0';
  57. }
  58. static void
  59. logv(int severity, const char *funcname, const char *format, va_list ap)
  60. {
  61. char buf[1024];
  62. int formatted = 0;
  63. logfile_t *lf;
  64. assert(format);
  65. if (severity < loglevel)
  66. return;
  67. if (!logfiles) {
  68. /* XXX This is a temporary measure until we get configuration support
  69. for logfiles. */
  70. add_stream_log(loglevel, "<stdout>", stdout);
  71. }
  72. for (lf = logfiles; lf; lf = lf->next) {
  73. if (severity < lf->loglevel || severity > lf->max_loglevel)
  74. continue;
  75. if (!lf->file)
  76. continue;
  77. if (!formatted) {
  78. format_msg(buf, 1024, severity, funcname, format, ap);
  79. formatted = 1;
  80. }
  81. fputs(buf, lf->file);
  82. /* XXX check for EOF */
  83. }
  84. }
  85. void
  86. log_set_severity(int severity)
  87. {
  88. loglevel = severity;
  89. }
  90. /* Outputs a message to stdout */
  91. void log(int severity, const char *format, ...)
  92. {
  93. va_list ap;
  94. va_start(ap,format);
  95. logv(severity, NULL, format, ap);
  96. va_end(ap);
  97. }
  98. void _log_fn(int severity, const char *fn, const char *format, ...)
  99. {
  100. va_list ap;
  101. va_start(ap,format);
  102. logv(severity, fn, format, ap);
  103. va_end(ap);
  104. }
  105. void close_logs()
  106. {
  107. logfile_t *next, *lf;
  108. for (lf = logfiles; lf; lf = lf->next) {
  109. if (lf->needs_close)
  110. fclose(lf->file);
  111. next = lf->next;
  112. free(lf);
  113. }
  114. logfiles = NULL;
  115. }
  116. void reset_logs()
  117. {
  118. logfile_t *lf;
  119. for (lf = logfiles; lf; lf = lf->next) {
  120. if (lf->needs_close) {
  121. fclose(lf->file);
  122. lf->file = fopen(lf->filename, "a");
  123. }
  124. }
  125. }
  126. void add_stream_log(int loglevel, const char *name, FILE *stream)
  127. {
  128. logfile_t *lf;
  129. lf = tor_malloc(sizeof(logfile_t));
  130. lf->filename = name;
  131. lf->needs_close = 0;
  132. lf->loglevel = loglevel;
  133. lf->max_loglevel = LOG_EMERG;
  134. lf->file = stream;
  135. lf->next = logfiles;
  136. logfiles = lf;
  137. }
  138. void add_file_log(int loglevel, const char *filename)
  139. {
  140. FILE *f;
  141. f = fopen(filename, "a");
  142. if (!f) return;
  143. add_stream_log(loglevel, filename, f);
  144. logfiles->needs_close = 1;
  145. }