summaryrefslogtreecommitdiff
path: root/lib/common/Logging.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/common/Logging.h')
-rw-r--r--lib/common/Logging.h138
1 files changed, 129 insertions, 9 deletions
diff --git a/lib/common/Logging.h b/lib/common/Logging.h
index 78db2bea..9bb2cf6c 100644
--- a/lib/common/Logging.h
+++ b/lib/common/Logging.h
@@ -10,10 +10,14 @@
#ifndef LOGGING__H
#define LOGGING__H
+#include <cerrno>
+#include <cstring>
#include <iomanip>
#include <sstream>
#include <vector>
+#include "FileStream.h"
+
/*
#define BOX_LOG(level, stuff) \
{ \
@@ -27,9 +31,16 @@
#define BOX_LOG(level, stuff) \
{ \
- std::ostringstream line; \
- line << stuff; \
- Logging::Log(level, __FILE__, __LINE__, line.str()); \
+ std::ostringstream _box_log_line; \
+ _box_log_line << stuff; \
+ Logging::Log(level, __FILE__, __LINE__, _box_log_line.str()); \
+}
+
+#define BOX_SYSLOG(level, stuff) \
+{ \
+ std::ostringstream _box_log_line; \
+ _box_log_line << stuff; \
+ Logging::LogToSyslog(level, __FILE__, __LINE__, _box_log_line.str()); \
}
#define BOX_FATAL(stuff) BOX_LOG(Log::FATAL, stuff)
@@ -41,15 +52,56 @@
if (Logging::IsEnabled(Log::TRACE)) \
{ BOX_LOG(Log::TRACE, stuff) }
-#define BOX_FORMAT_ACCOUNT(accno) \
+#define BOX_LOG_SYS_WARNING(stuff) \
+ BOX_WARNING(stuff << ": " << std::strerror(errno) << " (" << errno << ")")
+#define BOX_LOG_SYS_ERROR(stuff) \
+ BOX_ERROR(stuff << ": " << std::strerror(errno) << " (" << errno << ")")
+#define BOX_LOG_SYS_FATAL(stuff) \
+ BOX_FATAL(stuff << ": " << std::strerror(errno) << " (" << errno << ")")
+
+inline std::string GetNativeErrorMessage()
+{
+#ifdef WIN32
+ return GetErrorMessage(GetLastError());
+#else
+ std::ostringstream _box_log_line;
+ _box_log_line << std::strerror(errno) << " (" << errno << ")";
+ return _box_log_line.str();
+#endif
+}
+
+#ifdef WIN32
+ #define BOX_LOG_WIN_ERROR(stuff) \
+ BOX_ERROR(stuff << ": " << GetErrorMessage(GetLastError()))
+ #define BOX_LOG_WIN_WARNING(stuff) \
+ BOX_WARNING(stuff << ": " << GetErrorMessage(GetLastError()))
+ #define BOX_LOG_WIN_ERROR_NUMBER(stuff, number) \
+ BOX_ERROR(stuff << ": " << GetErrorMessage(number))
+ #define BOX_LOG_WIN_WARNING_NUMBER(stuff, number) \
+ BOX_WARNING(stuff << ": " << GetErrorMessage(number))
+ #define BOX_LOG_NATIVE_ERROR(stuff) BOX_LOG_WIN_ERROR(stuff)
+ #define BOX_LOG_NATIVE_WARNING(stuff) BOX_LOG_WIN_WARNING(stuff)
+#else
+ #define BOX_LOG_NATIVE_ERROR(stuff) BOX_LOG_SYS_ERROR(stuff)
+ #define BOX_LOG_NATIVE_WARNING(stuff) BOX_LOG_SYS_WARNING(stuff)
+#endif
+
+#define BOX_LOG_SOCKET_ERROR(_type, _name, _port, stuff) \
+ BOX_LOG_NATIVE_ERROR(stuff << " (type " << _type << ", name " << \
+ _name << ", port " << _port << ")")
+
+#define BOX_FORMAT_HEX32(number) \
std::hex << \
std::showbase << \
std::internal << \
std::setw(10) << \
std::setfill('0') << \
- (accno) << \
+ (number) << \
std::dec
+#define BOX_FORMAT_ACCOUNT(accno) \
+ BOX_FORMAT_HEX32(accno)
+
#define BOX_FORMAT_OBJECTID(objectid) \
std::hex << \
std::showbase << \
@@ -69,7 +121,8 @@ namespace Log
NOTICE,
INFO,
TRACE,
- EVERYTHING
+ EVERYTHING,
+ INVALID = -1
};
}
@@ -89,6 +142,7 @@ class Logger
public:
Logger();
+ Logger(Log::Level level);
virtual ~Logger();
virtual bool Log(Log::Level level, const std::string& rFile,
@@ -117,20 +171,22 @@ class Logger
class Console : public Logger
{
private:
+ static bool sShowTag;
static bool sShowTime;
static bool sShowTimeMicros;
- static bool sShowTag;
+ static bool sShowPID;
static std::string sTag;
public:
virtual bool Log(Log::Level level, const std::string& rFile,
int line, std::string& rMessage);
virtual const char* GetType() { return "Console"; }
- virtual void SetProgramName(const std::string& rProgramName) { }
+ virtual void SetProgramName(const std::string& rProgramName);
- static void SetTag(const std::string& rTag);
+ static void SetShowTag(bool enabled);
static void SetShowTime(bool enabled);
static void SetShowTimeMicros(bool enabled);
+ static void SetShowPID(bool enabled);
};
// --------------------------------------------------------------------------
@@ -146,6 +202,7 @@ class Syslog : public Logger
{
private:
std::string mName;
+ int mFacility;
public:
Syslog();
@@ -155,6 +212,8 @@ class Syslog : public Logger
int line, std::string& rMessage);
virtual const char* GetType() { return "Syslog"; }
virtual void SetProgramName(const std::string& rProgramName);
+ virtual void SetFacility(int facility);
+ static int GetNamedFacility(const std::string& rFacility);
};
// --------------------------------------------------------------------------
@@ -178,6 +237,7 @@ class Logging
static Syslog* spSyslog;
static Log::Level sGlobalLevel;
static Logging sGlobalLogging;
+ static std::string sProgramName;
public:
Logging ();
@@ -190,14 +250,74 @@ class Logging
static void Remove (Logger* pOldLogger);
static void Log(Log::Level level, const std::string& rFile,
int line, const std::string& rMessage);
+ static void LogToSyslog(Log::Level level, const std::string& rFile,
+ int line, const std::string& rMessage);
static void SetContext(std::string context);
static void ClearContext();
static void SetGlobalLevel(Log::Level level) { sGlobalLevel = level; }
+ static Log::Level GetGlobalLevel() { return sGlobalLevel; }
+ static Log::Level GetNamedLevel(const std::string& rName);
static bool IsEnabled(Log::Level level)
{
return (int)sGlobalLevel >= (int)level;
}
static void SetProgramName(const std::string& rProgramName);
+ static std::string GetProgramName() { return sProgramName; }
+ static void SetFacility(int facility);
+
+ class Guard
+ {
+ private:
+ Log::Level mOldLevel;
+
+ public:
+ Guard(Log::Level newLevel)
+ {
+ mOldLevel = Logging::GetGlobalLevel();
+ Logging::SetGlobalLevel(newLevel);
+ }
+ ~Guard()
+ {
+ Logging::SetGlobalLevel(mOldLevel);
+ }
+ };
+
+ class Tagger
+ {
+ private:
+ std::string mOldTag;
+
+ public:
+ Tagger(const std::string& rTempTag)
+ {
+ mOldTag = Logging::GetProgramName();
+ Logging::SetProgramName(mOldTag + " " + rTempTag);
+ }
+ ~Tagger()
+ {
+ Logging::SetProgramName(mOldTag);
+ }
+ };
+};
+
+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