From 3e51e918dafe8c6ab3ebc6e501ef15a8fc01a8ad Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Fri, 26 Sep 2008 20:22:26 +0000 Subject: Add file logger class. --- lib/common/Logging.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/common/Logging.h | 23 ++++++++++++++++++++ 2 files changed, 82 insertions(+) (limited to 'lib') 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; +} diff --git a/lib/common/Logging.h b/lib/common/Logging.h index 9a850c44..39e76740 100644 --- a/lib/common/Logging.h +++ b/lib/common/Logging.h @@ -15,6 +15,8 @@ #include #include +#include "FileStream.h" + /* #define BOX_LOG(level, stuff) \ { \ @@ -115,6 +117,7 @@ class Logger public: Logger(); + Logger(Log::Level level); virtual ~Logger(); virtual bool Log(Log::Level level, const std::string& rFile, @@ -268,4 +271,24 @@ class Logging }; }; +class FileLogger : public Logger +{ + private: + FileStream mLogFile; + FileLogger(const FileLogger& forbidden) + : mLogFile("") { /* do not call */ } + + public: + FileLogger(const std::string& rFileName, Log::Level Level) + : Logger(Level), + mLogFile(rFileName, O_WRONLY | O_CREAT | O_APPEND) + { } + + virtual bool Log(Log::Level Level, const std::string& rFile, + int Line, std::string& rMessage); + + virtual const char* GetType() { return "FileLogger"; } + virtual void SetProgramName(const std::string& rProgramName) { } +}; + #endif // LOGGING__H -- cgit v1.2.3