log.c 4.0 KB

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