log.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include <stdarg.h>
  5. #include <assert.h>
  6. #include <stdlib.h>
  7. #include "orconfig.h"
  8. #include "./util.h"
  9. #include "./log.h"
  10. #ifdef MS_WINDOWS
  11. #define vsnprintf _vsnprintf
  12. #endif
  13. struct logfile_t;
  14. typedef struct logfile_t {
  15. struct logfile_t *next;
  16. const char *filename;
  17. FILE *file;
  18. int needs_close;
  19. int loglevel;
  20. int max_loglevel;
  21. } logfile_t;
  22. static INLINE const char *sev_to_string(int severity) {
  23. switch(severity) {
  24. case LOG_DEBUG: return "debug";
  25. case LOG_INFO: return "info";
  26. case LOG_NOTICE: return "notice";
  27. case LOG_WARN: return "warn";
  28. case LOG_ERR: return "err";
  29. default: assert(0); return "UNKNOWN";
  30. }
  31. }
  32. static logfile_t *logfiles = NULL;
  33. /* Format a log message into a fixed-sized buffer. (This is factored out
  34. * of 'logv' so that we never format a message more than once.
  35. */
  36. static INLINE void format_msg(char *buf, size_t buf_len,
  37. int severity, const char *funcname,
  38. const char *format, va_list ap)
  39. {
  40. time_t t;
  41. struct timeval now;
  42. size_t n;
  43. buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
  44. tor_gettimeofday(&now);
  45. t = (time_t)now.tv_sec;
  46. n = strftime(buf, buf_len, "%b %d %H:%M:%S", localtime(&t));
  47. n += snprintf(buf+n, buf_len-n,
  48. ".%.3ld [%s] ",
  49. (long)now.tv_usec / 1000, sev_to_string(severity));
  50. if(n > buf_len)
  51. n = buf_len-1; /* the *nprintf funcs return how many bytes they
  52. * _would_ print, if the output is truncated.
  53. * Subtract one because the count doesn't include the \0 */
  54. if (funcname) {
  55. n += snprintf(buf+n, buf_len-n, "%s(): ", funcname);
  56. if(n > buf_len)
  57. n = buf_len-1;
  58. }
  59. n += vsnprintf(buf+n,buf_len-n,format,ap);
  60. if(n > buf_len)
  61. n = buf_len-1;
  62. buf[n]='\n';
  63. buf[n+1]='\0';
  64. }
  65. static void
  66. logv(int severity, const char *funcname, const char *format, va_list ap)
  67. {
  68. char buf[10024];
  69. int formatted = 0;
  70. logfile_t *lf;
  71. assert(format);
  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, 10024, severity, funcname, format, ap);
  79. formatted = 1;
  80. }
  81. if(fputs(buf, lf->file) == EOF) { /* error */
  82. assert(0); /* XXX */
  83. }
  84. if(fflush(lf->file) == EOF) { /* error */
  85. /* don't log the error! */
  86. assert(0); /* XXX fail for now. what's better to do? */
  87. }
  88. }
  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 *victim;
  108. while(logfiles) {
  109. victim = logfiles;
  110. logfiles = logfiles->next;
  111. if (victim->needs_close)
  112. fclose(victim->file);
  113. free(victim);
  114. }
  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_ERR;
  134. lf->file = stream;
  135. lf->next = logfiles;
  136. logfiles = lf;
  137. }
  138. /*
  139. * If opening the logfile fails, -1 is returned and
  140. * errno is set appropriately (by fopen)
  141. */
  142. int add_file_log(int loglevel, const char *filename)
  143. {
  144. FILE *f;
  145. f = fopen(filename, "a");
  146. if (!f) return -1;
  147. add_stream_log(loglevel, filename, f);
  148. logfiles->needs_close = 1;
  149. return 0;
  150. }
  151. /*
  152. Local Variables:
  153. mode:c
  154. indent-tabs-mode:nil
  155. c-basic-offset:2
  156. End:
  157. */