Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
92 lines (69 sloc) 1.58 KB
#ifndef com_sleepless_io_log_cpp
#define com_sleepless_io_log_cpp
// Copyright 3000 Sleepless Software Inc. All Rights Reserved
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include "mutex.cpp"
#define LOG Log::log
#define DTLOG Log::dtlog
#define SLOG Log::slog
#define TRACE if(logTrace) Log::log
#define HERE() TRACE("@ %s, %d\n", __FILE__, __LINE__)
#define TRACEON() Log::trace(true)
#define TRACEOFF() Log::trace(false)
static bool logTrace = false;
static Mutex logMutex;
static FILE *logstream = stdout;
struct Log
{
static FILE *to(const char *f, const char *mode)
{
logMutex.lock();
FILE *fp = fopen(f, mode);
if(fp)
logstream = fp;
logMutex.unlock();
return logstream;
}
static void log(const char *fmt, ...)
{
logMutex.lock();
va_list marker;
va_start(marker, fmt);
vfprintf(logstream, fmt, marker);
fflush(logstream);
va_end(marker);
logMutex.unlock();
}
static void dtlog(const char *fmt, ...)
{
logMutex.lock();
char buf[80];
time_t tt = time(0);
strftime(buf, sizeof(buf), "[%Y-%m-%d %H:%M:%S] ", localtime(&tt));
fprintf(logstream, "%s", buf);
va_list marker;
va_start(marker, fmt);
vfprintf(logstream, fmt, marker);
fflush(logstream);
va_end(marker);
logMutex.unlock();
}
static void slog(char *fmt, ...)
{
logMutex.lock();
fprintf(logstream, "[%ld] ", time(0));
va_list marker;
va_start(marker, fmt);
vfprintf(logstream, fmt, marker);
fflush(logstream);
va_end(marker);
logMutex.unlock();
}
static void trace(bool tf)
{
logTrace = tf;
}
};
#endif // com_sleepless_io_log_cpp