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"
|
2003-08-12 05:08:41 +02:00
|
|
|
#include "util.h"
|
2002-09-04 02:39:33 +02:00
|
|
|
|
2003-06-25 06:47:54 +02:00
|
|
|
static const char *sev_to_string(int severity) {
|
2002-09-04 02:39:33 +02:00
|
|
|
switch(severity) {
|
2003-06-25 06:47:54 +02:00
|
|
|
case LOG_DEBUG: return "debug";
|
|
|
|
case LOG_INFO: return "info";
|
|
|
|
case LOG_NOTICE: return "notice";
|
|
|
|
case LOG_WARNING: return "warn";
|
|
|
|
case LOG_ERR: return "err";
|
|
|
|
case LOG_CRIT: return "crit";
|
|
|
|
case LOG_ALERT: return "alert";
|
|
|
|
case LOG_EMERG: return "emerg";
|
|
|
|
default: return "UNKNOWN";
|
2002-09-04 02:39:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2003-09-12 00:13:13 +02:00
|
|
|
if (severity < loglevel)
|
2003-06-21 21:03:22 +02:00
|
|
|
return;
|
2003-08-12 05:08:41 +02:00
|
|
|
my_gettimeofday(&now);
|
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));
|
2003-06-25 06:47:54 +02:00
|
|
|
printf("%s.%.3ld [%s] ", buf, (long)now.tv_usec / 1000, sev_to_string(severity));
|
2003-06-21 21:03:22 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|