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; return strlen(buf)+1;
} }
/* Outputs a message to stdout */ static void
void log(int severity, const char *format, ...) logv(int severity, const char *funcname, const char *format, va_list ap)
{ {
static int loglevel = LOG_DEBUG; static int loglevel = LOG_DEBUG;
char buf[201]; char buf[201];
time_t t; time_t t;
va_list ap;
struct timeval now; struct timeval now;
if (format) { if (format) {
if (severity > loglevel)
return;
if (gettimeofday(&now,NULL) < 0) if (gettimeofday(&now,NULL) < 0)
return; return;
va_start(ap,format);
if (severity <= loglevel)
{
t = time(NULL); t = time(NULL);
strftime(buf, 200, "%b %d %H:%M:%S", localtime(&t)); strftime(buf, 200, "%b %d %H:%M:%S", localtime(&t));
printf("%s.%.3ld ", buf, (long)now.tv_usec / 1000); printf("%s.%.3ld ", buf, (long)now.tv_usec / 1000);
sev_to_string(buf, 200, severity); sev_to_string(buf, 200, severity);
printf("[%s] ", buf); printf("[%s] ", buf);
if (funcname)
printf("%s(): ", funcname);
vprintf(format,ap); vprintf(format,ap);
printf("\n"); 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); va_end(ap);
} }
else
loglevel = severity; 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, ...); void log(int severity, const char *format, ...);
#ifdef __GNUC__ #ifdef __GNUC__
#ifdef __APPLE_CPP__ void _log_fn(int severity, const char *funcname, const char *format, ...);
#define log_fn(severity, args...) \ #define log_fn(severity, args...) \
log((severity), __PRETTY_FUNCTION__ "(): " args) _log_fn(severity, __PRETTY_FUNCTION__, args)
#else
#define log_fn(severity, format, args...) \
log((severity), "%s(): " format, __PRETTY_FUNCTION__ , ##args)
#endif
#else #else
#define log_fn log #define log_fn log
#define log_fnf log
#endif #endif
# define __LOG_H # define __LOG_H