summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2008-09-26 20:22:26 +0000
committerChris Wilson <chris+github@qwirx.com>2008-09-26 20:22:26 +0000
commit3e51e918dafe8c6ab3ebc6e501ef15a8fc01a8ad (patch)
tree9814bf73556c0a9f5923b68abe8c4c14f380147f
parent0493ea11b9e1b06af149551b49118bbf25c52ad2 (diff)
Add file logger class.
-rw-r--r--lib/common/Logging.cpp59
-rw-r--r--lib/common/Logging.h23
2 files changed, 82 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;
+}
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 <sstream>
#include <vector>
+#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