diff options
author | Chris Wilson <chris+github@qwirx.com> | 2008-09-26 20:22:26 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2008-09-26 20:22:26 +0000 |
commit | 3e51e918dafe8c6ab3ebc6e501ef15a8fc01a8ad (patch) | |
tree | 9814bf73556c0a9f5923b68abe8c4c14f380147f /lib/common/Logging.cpp | |
parent | 0493ea11b9e1b06af149551b49118bbf25c52ad2 (diff) |
Add file logger class.
Diffstat (limited to 'lib/common/Logging.cpp')
-rw-r--r-- | lib/common/Logging.cpp | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/common/Logging.cpp b/lib/common/Logging.cpp index df7c609e..802f6a3b 100644 --- a/lib/common/Logging.cpp +++ b/lib/common/Logging.cpp @@ -222,6 +222,12 @@ Logger::Logger() Logging::Add(this); } +Logger::Logger(Log::Level Level) +: mCurrentLevel(Level) +{ + Logging::Add(this); +} + Logger::~Logger() { Logging::Remove(this); @@ -400,3 +406,56 @@ void Syslog::SetProgramName(const std::string& rProgramName) ::closelog(); ::openlog(mName.c_str(), LOG_PID, LOG_LOCAL6); } + +bool FileLogger::Log(Log::Level Level, const std::string& rFile, + int line, std::string& rMessage) +{ + if (Level > GetLevel()) + { + return true; + } + + /* avoid infinite loop if this throws an exception */ + Logging::Remove(this); + + std::ostringstream buf; + buf << FormatTime(GetCurrentBoxTime(), false); + buf << " "; + + if (Level <= Log::FATAL) + { + buf << "[FATAL] "; + } + else if (Level <= Log::ERROR) + { + buf << "[ERROR] "; + } + else if (Level <= Log::WARNING) + { + buf << "[WARNING] "; + } + else if (Level <= Log::NOTICE) + { + buf << "[NOTICE] "; + } + else if (Level <= Log::INFO) + { + buf << "[INFO] "; + } + else if (Level <= Log::TRACE) + { + buf << "[TRACE] "; + } + + buf << rMessage << "\n"; + std::string output = buf.str(); + + #ifdef WIN32 + ConvertUtf8ToConsole(output.c_str(), output); + #endif + + mLogFile.Write(output.c_str(), output.length()); + + Logging::Add(this); + return true; +} |