log.c 3.5 KB

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