[Box Backup-commit] COMMIT r2296 - box/trunk/lib/common
boxbackup-dev@fluffy.co.uk
boxbackup-dev@fluffy.co.uk
Fri, 26 Sep 2008 21:22:26 +0100 (BST)
Author: chris
Date: 2008-09-26 21:22:26 +0100 (Fri, 26 Sep 2008)
New Revision: 2296
Modified:
box/trunk/lib/common/Logging.cpp
box/trunk/lib/common/Logging.h
Log:
Add file logger class.
Modified: box/trunk/lib/common/Logging.cpp
===================================================================
--- box/trunk/lib/common/Logging.cpp 2008-09-26 20:21:00 UTC (rev 2295)
+++ box/trunk/lib/common/Logging.cpp 2008-09-26 20:22:26 UTC (rev 2296)
@@ -222,6 +222,12 @@
Logging::Add(this);
}
+Logger::Logger(Log::Level Level)
+: mCurrentLevel(Level)
+{
+ Logging::Add(this);
+}
+
Logger::~Logger()
{
Logging::Remove(this);
@@ -400,3 +406,56 @@
::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;
+}
Modified: box/trunk/lib/common/Logging.h
===================================================================
--- box/trunk/lib/common/Logging.h 2008-09-26 20:21:00 UTC (rev 2295)
+++ box/trunk/lib/common/Logging.h 2008-09-26 20:22:26 UTC (rev 2296)
@@ -15,6 +15,8 @@
#include <sstream>
#include <vector>
+#include "FileStream.h"
+
/*
#define BOX_LOG(level, stuff) \
{ \
@@ -115,6 +117,7 @@
public:
Logger();
+ Logger(Log::Level level);
virtual ~Logger();
virtual bool Log(Log::Level level, const std::string& rFile,
@@ -268,4 +271,24 @@
};
};
+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