log.c 3.7 KB

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