Come up with a less macro-happy, even more portable log_fn implementation

svn:r331
This commit is contained in:
Nick Mathewson 2003-06-17 21:36:44 +00:00
parent b9480620c6
commit 5fd033a8e8
2 changed files with 37 additions and 28 deletions

View File

@ -42,36 +42,50 @@ size_t sev_to_string(char *buf, int max, int severity) {
return strlen(buf)+1;
}
/* Outputs a message to stdout */
void log(int severity, const char *format, ...)
static void
logv(int severity, const char *funcname, const char *format, va_list ap)
{
static int loglevel = LOG_DEBUG;
char buf[201];
time_t t;
va_list ap;
struct timeval now;
if (format) {
if(gettimeofday(&now,NULL) < 0)
if (severity > loglevel)
return;
if (gettimeofday(&now,NULL) < 0)
return;
va_start(ap,format);
if (severity <= loglevel)
{
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);
vprintf(format,ap);
printf("\n");
}
va_end(ap);
}
else
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");
} else
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);
}

View File

@ -13,16 +13,11 @@
void log(int severity, const char *format, ...);
#ifdef __GNUC__
#ifdef __APPLE_CPP__
void _log_fn(int severity, const char *funcname, const char *format, ...);
#define log_fn(severity, args...) \
log((severity), __PRETTY_FUNCTION__ "(): " args)
#else
#define log_fn(severity, format, args...) \
log((severity), "%s(): " format, __PRETTY_FUNCTION__ , ##args)
#endif
_log_fn(severity, __PRETTY_FUNCTION__, args)
#else
#define log_fn log
#define log_fnf log
#endif
# define __LOG_H