mirror of
https://github.com/debauchee/barrier.git
synced 2026-05-05 21:02:39 +08:00
checkpoint. merging win32 code. server on X is currently broken
and client probably is.
This commit is contained in:
@@ -1,14 +1,37 @@
|
||||
#include "CLog.h"
|
||||
#include "BasicTypes.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#if defined(CONFIG_PLATFORM_WIN32)
|
||||
#include <windows.h>
|
||||
#define vsnprintf _vsnprintf
|
||||
#endif
|
||||
|
||||
static int g_maxPriority = -1;
|
||||
static const char* g_priority[] = {
|
||||
"FATAL",
|
||||
"ERROR",
|
||||
"WARNING",
|
||||
"NOTE",
|
||||
"INFO",
|
||||
"DEBUG",
|
||||
};
|
||||
static const int g_numPriority = (int)(sizeof(g_priority) /
|
||||
sizeof(g_priority[0]));
|
||||
static const int g_maxPriorityLength = 7; // length of longest string
|
||||
static const int g_prioritySuffixLength = 2;
|
||||
static const int g_priorityPad = g_maxPriorityLength +
|
||||
g_prioritySuffixLength;
|
||||
static const int g_newlineLength = 2;
|
||||
|
||||
//
|
||||
// CLog
|
||||
//
|
||||
|
||||
static int g_maxPriority = -1;
|
||||
CLog::Outputter CLog::s_outputter = NULL;
|
||||
|
||||
void CLog::print(const char* fmt, ...)
|
||||
{
|
||||
@@ -19,11 +42,14 @@ void CLog::print(const char* fmt, ...)
|
||||
fmt += 3;
|
||||
}
|
||||
|
||||
// compute prefix padding length
|
||||
int pad = g_priorityPad;
|
||||
|
||||
// print to buffer
|
||||
char stack[1024];
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
char* buffer = vsprint(0, stack,
|
||||
char* buffer = vsprint(pad, stack,
|
||||
sizeof(stack) / sizeof(stack[0]), fmt, args);
|
||||
va_end(args);
|
||||
|
||||
@@ -48,17 +74,19 @@ void CLog::printt(const char* file, int line,
|
||||
// compute prefix padding length
|
||||
char stack[1024];
|
||||
sprintf(stack, "%d", line);
|
||||
int pad = strlen(file) + 1 + strlen(stack) + 1 + 1;
|
||||
int pad = strlen(file) + 1 /* comma */ +
|
||||
strlen(stack) + 1 /* colon */ + 1 /* space */ +
|
||||
g_priorityPad;
|
||||
|
||||
// print to buffer
|
||||
// print to buffer, leaving space for a newline at the end
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
char* buffer = vsprint(pad, stack,
|
||||
sizeof(stack) / sizeof(stack[0]), fmt, args);
|
||||
va_end(args);
|
||||
|
||||
// print the prefix to the buffer
|
||||
sprintf(buffer, "%s,%d:", file, line);
|
||||
// print the prefix to the buffer. leave space for priority label.
|
||||
sprintf(buffer + g_priorityPad, "%s,%d:", file, line);
|
||||
buffer[pad - 1] = ' ';
|
||||
|
||||
// output buffer
|
||||
@@ -69,35 +97,51 @@ void CLog::printt(const char* file, int line,
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
void CLog::output(int priority, const char* msg)
|
||||
void CLog::setOutputter(Outputter outputter)
|
||||
{
|
||||
static const char* s_priority[] = {
|
||||
"FATAL",
|
||||
"ERROR",
|
||||
"WARNING",
|
||||
"NOTE",
|
||||
"INFO",
|
||||
"DEBUG",
|
||||
};
|
||||
static const int s_numPriority = (int)(sizeof(s_priority) /
|
||||
sizeof(s_priority[0]));
|
||||
assert(priority >= 0 && priority < s_numPriority);
|
||||
s_outputter = outputter;
|
||||
}
|
||||
|
||||
void CLog::output(int priority, char* msg)
|
||||
{
|
||||
assert(priority >= 0 && priority < g_numPriority);
|
||||
assert(msg != 0);
|
||||
|
||||
if (g_maxPriority == -1) {
|
||||
g_maxPriority = s_numPriority - 1;
|
||||
g_maxPriority = g_numPriority - 1;
|
||||
const char* priEnv = getenv("SYN_LOG_PRI");
|
||||
if (priEnv != NULL) {
|
||||
for (int i = 0; i < s_numPriority; ++i)
|
||||
if (strcmp(priEnv, s_priority[i]) == 0) {
|
||||
for (int i = 0; i < g_numPriority; ++i)
|
||||
if (strcmp(priEnv, g_priority[i]) == 0) {
|
||||
g_maxPriority = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (priority <= g_maxPriority)
|
||||
fprintf(stderr, "%s: %s\n", s_priority[priority], msg);
|
||||
if (priority <= g_maxPriority) {
|
||||
// insert priority label
|
||||
int n = strlen(g_priority[priority]);
|
||||
sprintf(msg + g_maxPriorityLength - n, "%s:", g_priority[priority]);
|
||||
msg[g_maxPriorityLength + 1] = ' ';
|
||||
|
||||
// put a newline at the end
|
||||
#if defined(CONFIG_PLATFORM_WIN32)
|
||||
strcat(msg + g_priorityPad, "\r\n");
|
||||
#else
|
||||
strcat(msg + g_priorityPad, "\n");
|
||||
#endif
|
||||
|
||||
// print it
|
||||
if (s_outputter)
|
||||
s_outputter(msg + g_maxPriorityLength - n);
|
||||
else
|
||||
#if defined(CONFIG_PLATFORM_WIN32)
|
||||
OutputDebugString(msg + g_maxPriorityLength - n);
|
||||
#else
|
||||
fprintf(stderr, "%s", msg + g_maxPriorityLength - n);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
char* CLog::vsprint(int pad, char* buffer, int len,
|
||||
@@ -109,7 +153,7 @@ char* CLog::vsprint(int pad, char* buffer, int len,
|
||||
int n;
|
||||
if (len >= pad) {
|
||||
n = vsnprintf(buffer + pad, len - pad, fmt, args);
|
||||
if (n != -1 && n <= len - pad)
|
||||
if (n != -1 && n <= len - pad + g_newlineLength)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
@@ -120,7 +164,7 @@ char* CLog::vsprint(int pad, char* buffer, int len,
|
||||
len *= 2;
|
||||
buffer = new char[len + pad];
|
||||
n = vsnprintf(buffer + pad, len - pad, fmt, args);
|
||||
} while (n == -1 || n > len - pad);
|
||||
} while (n == -1 || n > len - pad + g_newlineLength);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user