Added log time support to the logger.
This commit is contained in:
parent
a6ccf28d54
commit
361e403936
|
@ -20,274 +20,287 @@
|
||||||
#include "Version.h"
|
#include "Version.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
// names of priorities
|
// names of priorities
|
||||||
static const char* g_priority[] = {
|
static const char* g_priority[] = {
|
||||||
"FATAL",
|
"FATAL",
|
||||||
"ERROR",
|
"ERROR",
|
||||||
"WARNING",
|
"WARNING",
|
||||||
"NOTE",
|
"NOTE",
|
||||||
"INFO",
|
"INFO",
|
||||||
"DEBUG",
|
"DEBUG",
|
||||||
"DEBUG1",
|
"DEBUG1",
|
||||||
"DEBUG2"
|
"DEBUG2"
|
||||||
};
|
};
|
||||||
|
|
||||||
// number of priorities
|
// number of priorities
|
||||||
static const int g_numPriority = (int)(sizeof(g_priority) /
|
static const int g_numPriority = (int)(sizeof(g_priority) /
|
||||||
sizeof(g_priority[0]));
|
sizeof(g_priority[0]));
|
||||||
|
|
||||||
// the default priority
|
// the default priority
|
||||||
#if defined(NDEBUG)
|
#if defined(NDEBUG)
|
||||||
static const int g_defaultMaxPriority = 4;
|
static const int g_defaultMaxPriority = 4;
|
||||||
#else
|
#else
|
||||||
static const int g_defaultMaxPriority = 5;
|
static const int g_defaultMaxPriority = 5;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// length of longest string in g_priority
|
// length of longest string in g_priority
|
||||||
static const int g_maxPriorityLength = 7;
|
static const int g_maxPriorityLength = 7;
|
||||||
|
|
||||||
// length of suffix string (": ")
|
// length of suffix string (": ")
|
||||||
static const int g_prioritySuffixLength = 2;
|
static const int g_prioritySuffixLength = 2;
|
||||||
|
|
||||||
// amount of padded required to fill in the priority prefix
|
// amount of padded required to fill in the priority prefix
|
||||||
static const int g_priorityPad = g_maxPriorityLength +
|
static const int g_priorityPad = g_maxPriorityLength +
|
||||||
g_prioritySuffixLength;
|
g_prioritySuffixLength;
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// CLog
|
// CLog
|
||||||
//
|
//
|
||||||
|
|
||||||
CLog* CLog::s_log = NULL;
|
CLog* CLog::s_log = NULL;
|
||||||
|
|
||||||
CLog::CLog()
|
CLog::CLog()
|
||||||
{
|
{
|
||||||
assert(s_log == NULL);
|
assert(s_log == NULL);
|
||||||
|
|
||||||
// create mutex for multithread safe operation
|
// create mutex for multithread safe operation
|
||||||
m_mutex = ARCH->newMutex();
|
m_mutex = ARCH->newMutex();
|
||||||
|
|
||||||
// other initalization
|
// other initalization
|
||||||
m_maxPriority = g_defaultMaxPriority;
|
m_maxPriority = g_defaultMaxPriority;
|
||||||
m_maxNewlineLength = 0;
|
m_maxNewlineLength = 0;
|
||||||
insert(new CConsoleLogOutputter);
|
insert(new CConsoleLogOutputter);
|
||||||
}
|
}
|
||||||
|
|
||||||
CLog::~CLog()
|
CLog::~CLog()
|
||||||
{
|
{
|
||||||
// clean up
|
// clean up
|
||||||
for (COutputterList::iterator index = m_outputters.begin();
|
for (COutputterList::iterator index = m_outputters.begin();
|
||||||
index != m_outputters.end(); ++index) {
|
index != m_outputters.end(); ++index) {
|
||||||
delete *index;
|
delete *index;
|
||||||
}
|
}
|
||||||
for (COutputterList::iterator index = m_alwaysOutputters.begin();
|
for (COutputterList::iterator index = m_alwaysOutputters.begin();
|
||||||
index != m_alwaysOutputters.end(); ++index) {
|
index != m_alwaysOutputters.end(); ++index) {
|
||||||
delete *index;
|
delete *index;
|
||||||
}
|
}
|
||||||
ARCH->closeMutex(m_mutex);
|
ARCH->closeMutex(m_mutex);
|
||||||
s_log = NULL;
|
s_log = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
CLog*
|
CLog*
|
||||||
CLog::getInstance()
|
CLog::getInstance()
|
||||||
{
|
{
|
||||||
// note -- not thread safe; client must initialize log safely
|
// note -- not thread safe; client must initialize log safely
|
||||||
if (s_log == NULL) {
|
if (s_log == NULL) {
|
||||||
s_log = new CLog;
|
s_log = new CLog;
|
||||||
}
|
}
|
||||||
return s_log;
|
return s_log;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
CLog::print(const char* file, int line, const char* fmt, ...) const
|
CLog::print(const char* file, int line, const char* fmt, ...) const
|
||||||
{
|
{
|
||||||
// check if fmt begins with a priority argument
|
// check if fmt begins with a priority argument
|
||||||
int priority = 4;
|
int priority = 4;
|
||||||
if (fmt[0] == '%' && fmt[1] == 'z') {
|
if (fmt[0] == '%' && fmt[1] == 'z') {
|
||||||
priority = fmt[2] - '\060';
|
priority = fmt[2] - '\060';
|
||||||
fmt += 3;
|
fmt += 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
// done if below priority threshold
|
// done if below priority threshold
|
||||||
if (priority > getFilter()) {
|
if (priority > getFilter()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// compute prefix padding length
|
// compute prefix padding length
|
||||||
char stack[1024];
|
char stack[1024];
|
||||||
int pPad = g_priorityPad;
|
|
||||||
if (file != NULL) {
|
|
||||||
sprintf(stack, "%d", line);
|
|
||||||
pPad += strlen(file) + 1 /* comma */ +
|
|
||||||
strlen(stack) + 1 /* colon */ + 1 /* space */;
|
|
||||||
}
|
|
||||||
|
|
||||||
// compute suffix padding length
|
// compute suffix padding length
|
||||||
int sPad = m_maxNewlineLength;
|
int sPad = m_maxNewlineLength;
|
||||||
|
|
||||||
// print to buffer, leaving space for a newline at the end and prefix
|
// print to buffer, leaving space for a newline at the end and prefix
|
||||||
// at the beginning.
|
// at the beginning.
|
||||||
char* buffer = stack;
|
char* buffer = stack;
|
||||||
int len = (int)(sizeof(stack) / sizeof(stack[0]));
|
int len = (int)(sizeof(stack) / sizeof(stack[0]));
|
||||||
while (true) {
|
while (true) {
|
||||||
// try printing into the buffer
|
// try printing into the buffer
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
int n = ARCH->vsnprintf(buffer + pPad, len - pPad - sPad, fmt, args);
|
int n = ARCH->vsnprintf(buffer, len - sPad, fmt, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
// if the buffer wasn't big enough then make it bigger and try again
|
// if the buffer wasn't big enough then make it bigger and try again
|
||||||
if (n < 0 || n > (int)len) {
|
if (n < 0 || n > (int)len) {
|
||||||
if (buffer != stack) {
|
if (buffer != stack) {
|
||||||
delete[] buffer;
|
delete[] buffer;
|
||||||
}
|
}
|
||||||
len *= 2;
|
len *= 2;
|
||||||
buffer = new char[len];
|
buffer = new char[len];
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the buffer was big enough then continue
|
// if the buffer was big enough then continue
|
||||||
else {
|
else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// print the prefix to the buffer. leave space for priority label.
|
// print the prefix to the buffer. leave space for priority label.
|
||||||
char* message = buffer;
|
char message[2048];
|
||||||
if (file != NULL) {
|
if (file != NULL) {
|
||||||
sprintf(buffer + g_priorityPad, "%s,%d:", file, line);
|
|
||||||
buffer[pPad - 1] = ' ';
|
|
||||||
|
|
||||||
// discard file and line if priority < 0
|
struct tm *tm;
|
||||||
if (priority < 0) {
|
char tmp[220];
|
||||||
message += pPad - g_priorityPad;
|
time_t t;
|
||||||
}
|
time(&t);
|
||||||
}
|
tm = localtime(&t);
|
||||||
|
sprintf(tmp, "%04i-%02i-%02iT%02i:%02i:%02i", tm->tm_year + 1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
|
||||||
|
//strcpy(msg, tmp);
|
||||||
|
|
||||||
// output buffer
|
|
||||||
output(priority, message);
|
|
||||||
|
|
||||||
// clean up
|
sprintf(message, "%s %s: %s\n\t%s,%d", tmp, g_priority[priority], buffer, file, line);
|
||||||
if (buffer != stack) {
|
// buffer[pPad - 1] = ' ';
|
||||||
delete[] buffer;
|
|
||||||
}
|
// discard file and line if priority < 0
|
||||||
|
/*if (priority < 0) {
|
||||||
|
message += pPad - g_priorityPad;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
// output buffer
|
||||||
|
output(priority, message);
|
||||||
|
|
||||||
|
// clean up
|
||||||
|
if (buffer != stack) {
|
||||||
|
delete[] buffer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
CLog::insert(ILogOutputter* outputter, bool alwaysAtHead)
|
CLog::insert(ILogOutputter* outputter, bool alwaysAtHead)
|
||||||
{
|
{
|
||||||
assert(outputter != NULL);
|
assert(outputter != NULL);
|
||||||
assert(outputter->getNewline() != NULL);
|
assert(outputter->getNewline() != NULL);
|
||||||
|
|
||||||
CArchMutexLock lock(m_mutex);
|
CArchMutexLock lock(m_mutex);
|
||||||
if (alwaysAtHead) {
|
if (alwaysAtHead) {
|
||||||
m_alwaysOutputters.push_front(outputter);
|
m_alwaysOutputters.push_front(outputter);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
m_outputters.push_front(outputter);
|
m_outputters.push_front(outputter);
|
||||||
}
|
}
|
||||||
int newlineLength = strlen(outputter->getNewline());
|
int newlineLength = strlen(outputter->getNewline());
|
||||||
if (newlineLength > m_maxNewlineLength) {
|
if (newlineLength > m_maxNewlineLength) {
|
||||||
m_maxNewlineLength = newlineLength;
|
m_maxNewlineLength = newlineLength;
|
||||||
}
|
}
|
||||||
outputter->open(kAppVersion);
|
outputter->open(kAppVersion);
|
||||||
outputter->show(false);
|
outputter->show(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
CLog::remove(ILogOutputter* outputter)
|
CLog::remove(ILogOutputter* outputter)
|
||||||
{
|
{
|
||||||
CArchMutexLock lock(m_mutex);
|
CArchMutexLock lock(m_mutex);
|
||||||
m_outputters.remove(outputter);
|
m_outputters.remove(outputter);
|
||||||
m_alwaysOutputters.remove(outputter);
|
m_alwaysOutputters.remove(outputter);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
CLog::pop_front(bool alwaysAtHead)
|
CLog::pop_front(bool alwaysAtHead)
|
||||||
{
|
{
|
||||||
CArchMutexLock lock(m_mutex);
|
CArchMutexLock lock(m_mutex);
|
||||||
COutputterList* list = alwaysAtHead ? &m_alwaysOutputters : &m_outputters;
|
COutputterList* list = alwaysAtHead ? &m_alwaysOutputters : &m_outputters;
|
||||||
if (!list->empty()) {
|
if (!list->empty()) {
|
||||||
delete list->front();
|
delete list->front();
|
||||||
list->pop_front();
|
list->pop_front();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
CLog::setFilter(const char* maxPriority)
|
CLog::setFilter(const char* maxPriority)
|
||||||
{
|
{
|
||||||
if (maxPriority != NULL) {
|
if (maxPriority != NULL) {
|
||||||
for (int i = 0; i < g_numPriority; ++i) {
|
for (int i = 0; i < g_numPriority; ++i) {
|
||||||
if (strcmp(maxPriority, g_priority[i]) == 0) {
|
if (strcmp(maxPriority, g_priority[i]) == 0) {
|
||||||
setFilter(i);
|
setFilter(i);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
CLog::setFilter(int maxPriority)
|
CLog::setFilter(int maxPriority)
|
||||||
{
|
{
|
||||||
CArchMutexLock lock(m_mutex);
|
CArchMutexLock lock(m_mutex);
|
||||||
m_maxPriority = maxPriority;
|
m_maxPriority = maxPriority;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
CLog::getFilter() const
|
CLog::getFilter() const
|
||||||
{
|
{
|
||||||
CArchMutexLock lock(m_mutex);
|
CArchMutexLock lock(m_mutex);
|
||||||
return m_maxPriority;
|
return m_maxPriority;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
CLog::output(int priority, char* msg) const
|
CLog::output(int priority, char* msg) const
|
||||||
{
|
{
|
||||||
assert(priority >= -1 && priority < g_numPriority);
|
assert(priority >= -1 && priority < g_numPriority);
|
||||||
assert(msg != NULL);
|
assert(msg != NULL);
|
||||||
|
|
||||||
// insert priority label
|
// insert priority label
|
||||||
int n = -g_prioritySuffixLength;
|
//int n = -g_prioritySuffixLength;
|
||||||
if (priority >= 0) {
|
/*
|
||||||
n = strlen(g_priority[priority]);
|
if (priority >= 0) {
|
||||||
strcpy(msg + g_maxPriorityLength - n, g_priority[priority]);
|
|
||||||
msg[g_maxPriorityLength + 0] = ':';
|
|
||||||
msg[g_maxPriorityLength + 1] = ' ';
|
|
||||||
msg[g_maxPriorityLength + 1] = ' ';
|
|
||||||
}
|
|
||||||
|
|
||||||
// find end of message
|
|
||||||
char* end = msg + g_priorityPad + strlen(msg + g_priorityPad);
|
|
||||||
|
|
||||||
// write to each outputter
|
n = strlen(g_priority[priority]);
|
||||||
CArchMutexLock lock(m_mutex);
|
strcpy(msg + g_maxPriorityLength - n, g_priority[priority]);
|
||||||
for (COutputterList::const_iterator index = m_alwaysOutputters.begin();
|
msg[g_maxPriorityLength + 0] = ':';
|
||||||
index != m_alwaysOutputters.end();
|
msg[g_maxPriorityLength + 1] = ' ';
|
||||||
++index) {
|
msg[g_maxPriorityLength + 1] = ' ';
|
||||||
// get outputter
|
|
||||||
ILogOutputter* outputter = *index;
|
|
||||||
|
|
||||||
// put an appropriate newline at the end
|
|
||||||
strcpy(end, outputter->getNewline());
|
|
||||||
|
|
||||||
// write message
|
}
|
||||||
outputter->write(static_cast<ILogOutputter::ELevel>(priority),
|
*/
|
||||||
msg + g_maxPriorityLength - n);
|
// find end of message
|
||||||
}
|
//char* end = msg + g_priorityPad + strlen(msg + g_priorityPad);
|
||||||
for (COutputterList::const_iterator index = m_outputters.begin();
|
char* end = msg+strlen(msg)-1;
|
||||||
index != m_outputters.end(); ++index) {
|
|
||||||
// get outputter
|
|
||||||
ILogOutputter* outputter = *index;
|
|
||||||
|
|
||||||
// put an appropriate newline at the end
|
// write to each outputter
|
||||||
strcpy(end, outputter->getNewline());
|
CArchMutexLock lock(m_mutex);
|
||||||
|
for (COutputterList::const_iterator index = m_alwaysOutputters.begin();
|
||||||
|
index != m_alwaysOutputters.end();
|
||||||
|
++index) {
|
||||||
|
// get outputter
|
||||||
|
ILogOutputter* outputter = *index;
|
||||||
|
|
||||||
// write message and break out of loop if it returns false
|
// put an appropriate newline at the end
|
||||||
if (!outputter->write(static_cast<ILogOutputter::ELevel>(priority),
|
strcpy(end, outputter->getNewline());
|
||||||
msg + g_maxPriorityLength - n)) {
|
|
||||||
break;
|
// write message
|
||||||
}
|
outputter->write(static_cast<ILogOutputter::ELevel>(priority),
|
||||||
}
|
msg /*+ g_maxPriorityLength - n*/);
|
||||||
|
}
|
||||||
|
for (COutputterList::const_iterator index = m_outputters.begin();
|
||||||
|
index != m_outputters.end(); ++index) {
|
||||||
|
// get outputter
|
||||||
|
ILogOutputter* outputter = *index;
|
||||||
|
|
||||||
|
// put an appropriate newline at the end
|
||||||
|
strcpy(end, outputter->getNewline());
|
||||||
|
|
||||||
|
// write message and break out of loop if it returns false
|
||||||
|
if (!outputter->write(static_cast<ILogOutputter::ELevel>(priority),
|
||||||
|
msg /*+ g_maxPriorityLength - n*/)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue