| 1 |
#include "clModuleLogger.hpp" |
| 2 |
|
| 3 |
#include "fileutils.h" |
| 4 |
|
| 5 |
#include <sys/time.h> |
| 6 |
#include <wx/crt.h> |
| 7 |
#include <wx/datetime.h> |
| 8 |
|
| 9 |
clModuleLogger::clModuleLogger() {} |
| 10 |
|
| 11 |
clModuleLogger::~clModuleLogger() { Flush(); } |
| 12 |
|
| 13 |
bool clModuleLogger::CanLog() const { return FileLogger::CanLog(m_current_log_level); } |
| 14 |
|
| 15 |
void clModuleLogger::Flush() |
| 16 |
{ |
| 17 |
if(m_buffer.empty()) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
if(m_buffer.Last() != '\n') { |
| 22 |
m_buffer.Append('\n'); |
| 23 |
} |
| 24 |
FileUtils::AppendFileContent(m_logfile, m_buffer, wxConvUTF8); |
| 25 |
m_buffer.clear(); |
| 26 |
} |
| 27 |
|
| 28 |
void clModuleLogger::Open(const wxFileName& filepath) { m_logfile = filepath; } |
| 29 |
|
| 30 |
clModuleLogger& clModuleLogger::SetCurrentLogLevel(int level) |
| 31 |
{ |
| 32 |
if(!m_buffer.empty()) { |
| 33 |
// flush the current content |
| 34 |
Flush(); |
| 35 |
} |
| 36 |
m_current_log_level = level; |
| 37 |
return *this; |
| 38 |
} |
| 39 |
|
| 40 |
wxString clModuleLogger::Prefix() |
| 41 |
{ |
| 42 |
if(!CanLog()) { |
| 43 |
return wxEmptyString; |
| 44 |
} |
| 45 |
|
| 46 |
wxString prefix; |
| 47 |
timeval tim; |
| 48 |
gettimeofday(&tim, NULL); |
| 49 |
int ms = (int)tim.tv_usec / 1000.0; |
| 50 |
|
| 51 |
wxString msStr = wxString::Format(wxT("%03d"), ms); |
| 52 |
prefix << wxT("[") << wxDateTime::Now().FormatISOTime() << wxT(":") << msStr; |
| 53 |
// add the thread ID |
| 54 |
prefix << wxT(" T:") << wxThread::GetCurrentId(); |
| 55 |
|
| 56 |
switch(m_current_log_level) { |
| 57 |
case FileLogger::System: |
| 58 |
prefix << wxT(" SYSTEM]"); |
| 59 |
break; |
| 60 |
case FileLogger::Error: |
| 61 |
prefix << wxT(" ERROR]"); |
| 62 |
break; |
| 63 |
case FileLogger::Warning: |
| 64 |
prefix << wxT(" WARNING]"); |
| 65 |
break; |
| 66 |
case FileLogger::Dbg: |
| 67 |
prefix << wxT(" DEBUG]"); |
| 68 |
break; |
| 69 |
case FileLogger::Developer: |
| 70 |
prefix << wxT(" TRACE]"); |
| 71 |
break; |
| 72 |
} |
| 73 |
return prefix; |
| 74 |
} |