2002-11-24 09:45:13 +01:00
|
|
|
/* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2002-11-24 09:45:13 +01:00
|
|
|
#include "../or/or.h"
|
2002-06-27 00:45:49 +02:00
|
|
|
#include "log.h"
|
|
|
|
|
2002-09-04 02:39:33 +02:00
|
|
|
/* FIXME this whole thing is hacked together. feel free to make it clean. */
|
|
|
|
size_t sev_to_string(char *buf, int max, int severity) {
|
|
|
|
assert(max > 20);
|
|
|
|
|
|
|
|
switch(severity) {
|
|
|
|
case LOG_DEBUG:
|
|
|
|
strcpy(buf,"debug");
|
|
|
|
break;
|
|
|
|
case LOG_INFO:
|
|
|
|
strcpy(buf,"info");
|
|
|
|
break;
|
|
|
|
case LOG_NOTICE:
|
|
|
|
strcpy(buf,"notice");
|
|
|
|
break;
|
|
|
|
case LOG_WARNING:
|
|
|
|
strcpy(buf,"warn");
|
|
|
|
break;
|
|
|
|
case LOG_ERR:
|
|
|
|
strcpy(buf,"err");
|
|
|
|
break;
|
|
|
|
case LOG_CRIT:
|
|
|
|
strcpy(buf,"crit");
|
|
|
|
break;
|
|
|
|
case LOG_ALERT:
|
|
|
|
strcpy(buf,"alert");
|
|
|
|
break;
|
|
|
|
case LOG_EMERG:
|
|
|
|
strcpy(buf,"emerg");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
strcpy(buf,"UNKNOWN");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strlen(buf)+1;
|
|
|
|
}
|
|
|
|
|
2003-06-21 21:03:22 +02:00
|
|
|
static int loglevel = LOG_DEBUG;
|
|
|
|
|
2003-06-17 23:36:44 +02:00
|
|
|
static void
|
|
|
|
logv(int severity, const char *funcname, const char *format, va_list ap)
|
2002-06-27 00:45:49 +02:00
|
|
|
{
|
2002-09-04 02:39:33 +02:00
|
|
|
char buf[201];
|
|
|
|
time_t t;
|
2002-11-24 09:45:13 +01:00
|
|
|
struct timeval now;
|
2003-06-17 23:36:44 +02:00
|
|
|
|
2003-06-21 21:03:22 +02:00
|
|
|
assert(format);
|
|
|
|
if (severity > loglevel)
|
|
|
|
return;
|
|
|
|
if (gettimeofday(&now,NULL) < 0)
|
|
|
|
return;
|
2002-07-12 20:14:17 +02:00
|
|
|
|
2003-06-21 21:03:22 +02:00
|
|
|
t = time(NULL);
|
|
|
|
strftime(buf, 200, "%b %d %H:%M:%S", localtime(&t));
|
|
|
|
printf("%s.%.3ld ", buf, (long)now.tv_usec / 1000);
|
|
|
|
sev_to_string(buf, 200, severity);
|
|
|
|
printf("[%s] ", buf);
|
|
|
|
if (funcname)
|
|
|
|
printf("%s(): ", funcname);
|
|
|
|
vprintf(format,ap);
|
|
|
|
printf("\n");
|
|
|
|
}
|
2003-06-17 23:36:44 +02:00
|
|
|
|
2003-06-21 21:03:22 +02:00
|
|
|
void
|
|
|
|
log_set_severity(int severity)
|
|
|
|
{
|
|
|
|
loglevel = severity;
|
2003-06-17 23:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
2002-09-04 02:39:33 +02:00
|
|
|
|
2003-06-17 23:36:44 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|