2001-10-14 18:38:45 +04:00
|
|
|
#include "CLog.h"
|
2002-07-25 21:23:35 +04:00
|
|
|
#include "CString.h"
|
2002-06-11 02:06:45 +04:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2001-10-14 18:38:45 +04:00
|
|
|
|
2002-06-19 15:23:49 +04:00
|
|
|
#if WINDOWS_LIKE
|
2002-06-10 20:49:46 +04:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2001-11-19 03:33:36 +03:00
|
|
|
#include <windows.h>
|
|
|
|
#define vsnprintf _vsnprintf
|
|
|
|
#endif
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
// names of priorities
|
2001-11-19 03:33:36 +03:00
|
|
|
static const char* g_priority[] = {
|
|
|
|
"FATAL",
|
|
|
|
"ERROR",
|
|
|
|
"WARNING",
|
|
|
|
"NOTE",
|
|
|
|
"INFO",
|
|
|
|
"DEBUG",
|
2002-04-27 22:49:03 +04:00
|
|
|
"DEBUG1",
|
|
|
|
"DEBUG2"
|
2001-11-19 03:33:36 +03:00
|
|
|
};
|
2002-06-10 20:49:46 +04:00
|
|
|
|
|
|
|
// number of priorities
|
2001-11-19 03:33:36 +03:00
|
|
|
static const int g_numPriority = (int)(sizeof(g_priority) /
|
|
|
|
sizeof(g_priority[0]));
|
2002-06-10 20:49:46 +04:00
|
|
|
|
|
|
|
// the default priority
|
2002-06-01 23:26:11 +04:00
|
|
|
#if defined(NDEBUG)
|
|
|
|
static const int g_defaultMaxPriority = 4;
|
|
|
|
#else
|
|
|
|
static const int g_defaultMaxPriority = 5;
|
|
|
|
#endif
|
2002-06-10 20:49:46 +04:00
|
|
|
|
|
|
|
// length of longest string in g_priority
|
|
|
|
static const int g_maxPriorityLength = 7;
|
|
|
|
|
|
|
|
// length of suffix string (": ")
|
2001-11-19 03:33:36 +03:00
|
|
|
static const int g_prioritySuffixLength = 2;
|
2002-06-10 20:49:46 +04:00
|
|
|
|
|
|
|
// amount of padded required to fill in the priority prefix
|
2001-11-19 03:33:36 +03:00
|
|
|
static const int g_priorityPad = g_maxPriorityLength +
|
|
|
|
g_prioritySuffixLength;
|
2002-06-10 20:49:46 +04:00
|
|
|
|
2002-06-19 15:23:49 +04:00
|
|
|
// platform newline sequence
|
|
|
|
#if WINDOWS_LIKE
|
|
|
|
static const char* g_newline = "\r\n";
|
|
|
|
#else
|
|
|
|
static const char* g_newline = "\n";
|
|
|
|
#endif
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
// minimum length of a newline sequence
|
2001-11-19 03:33:36 +03:00
|
|
|
static const int g_newlineLength = 2;
|
|
|
|
|
2001-10-14 18:38:45 +04:00
|
|
|
//
|
|
|
|
// CLog
|
|
|
|
//
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
CLog::Outputter CLog::s_outputter = NULL;
|
2002-05-31 18:25:26 +04:00
|
|
|
CLog::Lock CLog::s_lock = &CLog::dummyLock;
|
|
|
|
int CLog::s_maxPriority = -1;
|
2001-10-24 01:23:29 +04:00
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CLog::print(const char* fmt, ...)
|
2001-10-14 18:38:45 +04:00
|
|
|
{
|
|
|
|
// check if fmt begins with a priority argument
|
|
|
|
int priority = 4;
|
|
|
|
if (fmt[0] == '%' && fmt[1] == 'z') {
|
|
|
|
priority = fmt[2] - '\060';
|
|
|
|
fmt += 3;
|
|
|
|
}
|
|
|
|
|
2002-06-14 22:08:20 +04:00
|
|
|
// done if below priority threshold
|
|
|
|
if (priority > getMaxPriority()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
// compute prefix padding length
|
|
|
|
int pad = g_priorityPad;
|
|
|
|
|
2001-10-14 18:38:45 +04:00
|
|
|
// print to buffer
|
|
|
|
char stack[1024];
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2002-07-25 21:23:35 +04:00
|
|
|
char* buffer = CStringUtil::vsprint(stack,
|
|
|
|
sizeof(stack) / sizeof(stack[0]),
|
|
|
|
pad, g_newlineLength, fmt, args);
|
2001-10-14 18:38:45 +04:00
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
// output buffer
|
|
|
|
output(priority, buffer);
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
if (buffer != stack)
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CLog::printt(const char* file, int line, const char* fmt, ...)
|
2001-10-14 18:38:45 +04:00
|
|
|
{
|
|
|
|
// check if fmt begins with a priority argument
|
|
|
|
int priority = 4;
|
|
|
|
if (fmt[0] == '%' && fmt[1] == 'z') {
|
|
|
|
priority = fmt[2] - '\060';
|
|
|
|
fmt += 3;
|
|
|
|
}
|
|
|
|
|
2002-06-14 22:08:20 +04:00
|
|
|
// done if below priority threshold
|
|
|
|
if (priority > getMaxPriority()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-10-14 18:38:45 +04:00
|
|
|
// compute prefix padding length
|
|
|
|
char stack[1024];
|
|
|
|
sprintf(stack, "%d", line);
|
2001-11-19 03:33:36 +03:00
|
|
|
int pad = strlen(file) + 1 /* comma */ +
|
|
|
|
strlen(stack) + 1 /* colon */ + 1 /* space */ +
|
|
|
|
g_priorityPad;
|
2001-10-14 18:38:45 +04:00
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
// print to buffer, leaving space for a newline at the end
|
2001-10-14 18:38:45 +04:00
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2002-07-25 21:23:35 +04:00
|
|
|
char* buffer = CStringUtil::vsprint(stack,
|
|
|
|
sizeof(stack) / sizeof(stack[0]),
|
|
|
|
pad, g_newlineLength, fmt, args);
|
2001-10-14 18:38:45 +04:00
|
|
|
va_end(args);
|
|
|
|
|
2001-11-19 03:33:36 +03:00
|
|
|
// print the prefix to the buffer. leave space for priority label.
|
|
|
|
sprintf(buffer + g_priorityPad, "%s,%d:", file, line);
|
2001-10-14 18:38:45 +04:00
|
|
|
buffer[pad - 1] = ' ';
|
|
|
|
|
2002-06-03 20:36:45 +04:00
|
|
|
// discard file and line if priority < 0
|
2002-06-04 15:03:34 +04:00
|
|
|
char* message = buffer;
|
2002-06-03 20:36:45 +04:00
|
|
|
if (priority < 0) {
|
2002-06-04 15:03:34 +04:00
|
|
|
message += pad - g_priorityPad;
|
2002-06-03 20:36:45 +04:00
|
|
|
}
|
|
|
|
|
2001-10-14 18:38:45 +04:00
|
|
|
// output buffer
|
2002-06-04 15:03:34 +04:00
|
|
|
output(priority, message);
|
2001-10-14 18:38:45 +04:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
if (buffer != stack)
|
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CLog::setOutputter(Outputter outputter)
|
2001-10-14 18:38:45 +04:00
|
|
|
{
|
2002-05-31 18:25:26 +04:00
|
|
|
CHoldLock lock(s_lock);
|
2001-11-19 03:33:36 +03:00
|
|
|
s_outputter = outputter;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
CLog::Outputter
|
|
|
|
CLog::getOutputter()
|
2001-11-19 03:33:36 +03:00
|
|
|
{
|
2002-05-31 18:25:26 +04:00
|
|
|
CHoldLock lock(s_lock);
|
|
|
|
return s_outputter;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CLog::setLock(Lock newLock)
|
2002-05-31 18:25:26 +04:00
|
|
|
{
|
|
|
|
CHoldLock lock(s_lock);
|
|
|
|
s_lock = (newLock == NULL) ? dummyLock : newLock;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
CLog::Lock
|
|
|
|
CLog::getLock()
|
2002-05-31 18:25:26 +04:00
|
|
|
{
|
|
|
|
CHoldLock lock(s_lock);
|
|
|
|
return (s_lock == dummyLock) ? NULL : s_lock;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
bool
|
2002-06-17 17:31:21 +04:00
|
|
|
CLog::setFilter(const char* maxPriority)
|
2002-06-03 20:36:45 +04:00
|
|
|
{
|
|
|
|
if (maxPriority != NULL) {
|
|
|
|
for (int i = 0; i < g_numPriority; ++i) {
|
|
|
|
if (strcmp(maxPriority, g_priority[i]) == 0) {
|
|
|
|
setFilter(i);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CLog::setFilter(int maxPriority)
|
2002-05-31 18:25:26 +04:00
|
|
|
{
|
|
|
|
CHoldLock lock(s_lock);
|
|
|
|
s_maxPriority = maxPriority;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
int
|
|
|
|
CLog::getFilter()
|
2002-05-31 18:25:26 +04:00
|
|
|
{
|
|
|
|
CHoldLock lock(s_lock);
|
|
|
|
return getMaxPriority();
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CLog::dummyLock(bool)
|
2002-05-31 18:25:26 +04:00
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
int
|
|
|
|
CLog::getMaxPriority()
|
2002-05-31 18:25:26 +04:00
|
|
|
{
|
|
|
|
CHoldLock lock(s_lock);
|
2001-10-14 18:38:45 +04:00
|
|
|
|
2002-05-31 18:25:26 +04:00
|
|
|
if (s_maxPriority == -1) {
|
2002-06-01 23:26:11 +04:00
|
|
|
s_maxPriority = g_defaultMaxPriority;
|
2002-06-03 20:36:45 +04:00
|
|
|
setFilter(getenv("SYN_LOG_PRI"));
|
2001-10-24 01:23:29 +04:00
|
|
|
}
|
|
|
|
|
2002-05-31 18:25:26 +04:00
|
|
|
return s_maxPriority;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
2002-06-17 17:31:21 +04:00
|
|
|
CLog::output(int priority, char* msg)
|
2002-05-31 18:25:26 +04:00
|
|
|
{
|
2002-06-03 20:36:45 +04:00
|
|
|
assert(priority >= -1 && priority < g_numPriority);
|
2002-06-14 22:08:20 +04:00
|
|
|
assert(msg != NULL);
|
|
|
|
|
|
|
|
// insert priority label
|
|
|
|
int n = -g_prioritySuffixLength;
|
|
|
|
if (priority >= 0) {
|
|
|
|
n = strlen(g_priority[priority]);
|
2002-07-25 21:23:35 +04:00
|
|
|
strcpy(msg + g_maxPriorityLength - n, g_priority[priority]);
|
|
|
|
msg[g_maxPriorityLength + 0] = ':';
|
|
|
|
msg[g_maxPriorityLength + 1] = ' ';
|
2002-06-14 22:08:20 +04:00
|
|
|
msg[g_maxPriorityLength + 1] = ' ';
|
|
|
|
}
|
2001-11-19 03:33:36 +03:00
|
|
|
|
2002-06-14 22:08:20 +04:00
|
|
|
// put a newline at the end
|
2002-06-19 15:23:49 +04:00
|
|
|
strcat(msg + g_priorityPad, g_newline);
|
2001-11-19 03:33:36 +03:00
|
|
|
|
2002-06-14 22:08:20 +04:00
|
|
|
// print it
|
|
|
|
CHoldLock lock(s_lock);
|
|
|
|
if (s_outputter == NULL ||
|
|
|
|
!s_outputter(priority, msg + g_maxPriorityLength - n)) {
|
2002-06-19 15:23:49 +04:00
|
|
|
#if WINDOWS_LIKE
|
2002-06-14 22:08:20 +04:00
|
|
|
openConsole();
|
2001-11-19 03:33:36 +03:00
|
|
|
#endif
|
2002-06-14 22:08:20 +04:00
|
|
|
fprintf(stderr, "%s", msg + g_maxPriorityLength - n);
|
2001-11-19 03:33:36 +03:00
|
|
|
}
|
2001-10-14 18:38:45 +04:00
|
|
|
}
|
|
|
|
|
2002-06-19 15:23:49 +04:00
|
|
|
#if WINDOWS_LIKE
|
2002-05-22 21:01:17 +04:00
|
|
|
|
|
|
|
static DWORD s_thread = 0;
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
static
|
|
|
|
BOOL WINAPI
|
|
|
|
CLogSignalHandler(DWORD)
|
2002-05-22 21:01:17 +04:00
|
|
|
{
|
|
|
|
// terminate cleanly and skip remaining handlers
|
|
|
|
PostThreadMessage(s_thread, WM_QUIT, 0, 0);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2002-06-10 20:49:46 +04:00
|
|
|
void
|
|
|
|
CLog::openConsole()
|
2002-05-22 21:01:17 +04:00
|
|
|
{
|
|
|
|
static bool s_hasConsole = false;
|
|
|
|
|
|
|
|
// ignore if already created
|
|
|
|
if (s_hasConsole)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// remember the current thread. when we get a ctrl+break or the
|
|
|
|
// console is closed we'll post WM_QUIT to this thread to shutdown
|
|
|
|
// cleanly.
|
|
|
|
// note -- win95/98/me are broken and will not receive a signal
|
|
|
|
// when the console is closed nor during logoff or shutdown,
|
|
|
|
// see microsoft articles Q130717 and Q134284. we could work
|
|
|
|
// around this in a painful way using hooks and hidden windows
|
|
|
|
// (as apache does) but it's not worth it. the app will still
|
|
|
|
// quit, just not cleanly. users in-the-know can use ctrl+c.
|
|
|
|
s_thread = GetCurrentThreadId();
|
|
|
|
|
|
|
|
// open a console
|
|
|
|
if (!AllocConsole())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// get the handle for error output
|
|
|
|
HANDLE herr = GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
|
|
|
|
// prep console. windows 95 and its ilk have braindead
|
|
|
|
// consoles that can't even resize independently of the
|
|
|
|
// buffer size. use a 25 line buffer for those systems.
|
|
|
|
OSVERSIONINFO osInfo;
|
|
|
|
COORD size = { 80, 1000 };
|
|
|
|
osInfo.dwOSVersionInfoSize = sizeof(osInfo);
|
|
|
|
if (GetVersionEx(&osInfo) &&
|
|
|
|
osInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
|
|
|
|
size.Y = 25;
|
|
|
|
SetConsoleScreenBufferSize(herr, size);
|
|
|
|
SetConsoleTextAttribute(herr,
|
|
|
|
FOREGROUND_RED |
|
|
|
|
FOREGROUND_GREEN |
|
|
|
|
FOREGROUND_BLUE);
|
|
|
|
SetConsoleCtrlHandler(CLogSignalHandler, TRUE);
|
|
|
|
|
|
|
|
// reopen stderr to point at console
|
|
|
|
freopen("con", "w", stderr);
|
|
|
|
s_hasConsole = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|