123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
- /* See LICENSE for licensing information */
- /* $Id$ */
- #include "../or/or.h"
- struct logfile_t;
- typedef struct logfile_t {
- struct logfile_t *next;
- const char *filename;
- FILE *file;
- int needs_close;
- int loglevel;
- int max_loglevel;
- } logfile_t;
- static INLINE const char *sev_to_string(int severity) {
- switch(severity) {
- case LOG_DEBUG: return "debug";
- case LOG_INFO: return "info";
- case LOG_WARN: return "warn";
- case LOG_ERR: return "err";
- default: assert(0); return "UNKNOWN";
- }
- }
- static int loglevel = LOG_DEBUG;
- static logfile_t *logfiles = NULL;
- /* Format a log message into a fixed-sized buffer. (This is factored out
- * of 'logv' so that we never format a message more than once.
- */
- static INLINE void format_msg(char *buf, size_t buf_len,
- int severity, const char *funcname,
- const char *format, va_list ap)
- {
- time_t t;
- struct timeval now;
- int n;
- buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
-
- tor_gettimeofday(&now);
- t = (time_t)now.tv_sec;
-
- n = strftime(buf, buf_len, "%b %d %H:%M:%S", localtime(&t));
- n += snprintf(buf+n, buf_len-n,
- ".%.3ld [%s] ",
- (long)now.tv_usec / 1000, sev_to_string(severity));
- if(n > buf_len)
- n = buf_len-1; /* the *nprintf funcs return how many bytes they
- * _would_ print, if the output is truncated.
- * Subtract one because the count doesn't include the \0 */
- if (funcname) {
- n += snprintf(buf+n, buf_len-n, "%s(): ", funcname);
- if(n > buf_len)
- n = buf_len-1;
- }
- n += vsnprintf(buf+n,buf_len-n,format,ap);
- if(n > buf_len)
- n = buf_len-1;
- buf[n]='\n';
- buf[n+1]='\0';
- }
- static void
- logv(int severity, const char *funcname, const char *format, va_list ap)
- {
- char buf[10024];
- int formatted = 0;
- logfile_t *lf;
-
- assert(format);
- if (severity < loglevel)
- return;
- for (lf = logfiles; lf; lf = lf->next) {
- if (severity < lf->loglevel || severity > lf->max_loglevel)
- continue;
- if (!lf->file)
- continue;
- if (!formatted) {
- format_msg(buf, 10024, severity, funcname, format, ap);
- formatted = 1;
- }
- fputs(buf, lf->file);
- fflush(lf->file);
- /* XXX check for EOF */
- }
- }
- void
- log_set_severity(int severity)
- {
- loglevel = severity;
- }
- /* Outputs a message to stdout */
- void _log(int severity, const char *format, ...)
- {
- va_list ap;
- va_start(ap,format);
- logv(severity, NULL, format, ap);
- va_end(ap);
- }
- void _log_fn(int severity, const char *fn, const char *format, ...)
- {
- va_list ap;
- va_start(ap,format);
- logv(severity, fn, format, ap);
- va_end(ap);
- }
- void close_logs()
- {
- logfile_t *victim;
- while(logfiles) {
- victim = logfiles;
- logfiles = logfiles->next;
- if (victim->needs_close)
- fclose(victim->file);
- free(victim);
- }
- }
- void reset_logs()
- {
- logfile_t *lf;
- for (lf = logfiles; lf; lf = lf->next) {
- if (lf->needs_close) {
- fclose(lf->file);
- lf->file = fopen(lf->filename, "a");
- }
- }
- }
- void add_stream_log(int loglevel, const char *name, FILE *stream)
- {
- logfile_t *lf;
- lf = tor_malloc(sizeof(logfile_t));
- lf->filename = name;
- lf->needs_close = 0;
- lf->loglevel = loglevel;
- lf->max_loglevel = LOG_ERR;
- lf->file = stream;
- lf->next = logfiles;
- logfiles = lf;
- }
- /*
- * If opening the logfile fails, -1 is returned and
- * errno is set appropriately (by fopen)
- */
- int add_file_log(int loglevel, const char *filename)
- {
- FILE *f;
- f = fopen(filename, "a");
- if (!f) return -1;
- add_stream_log(loglevel, filename, f);
- logfiles->needs_close = 1;
- return 0;
- }
|