Created
September 19, 2014 13:06
-
-
Save christianparpart/0cda0b326d7951841c44 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <xzero/Api.h> | |
/* | |
* <xzero/logging/...> | |
* LogSource the logging source, that initiates/triggers the log msgs | |
* LogLevel the log level (none, error, warn, info, debug) | |
* LogTarget the logging target, where to log to | |
* LogAggregator logging aggregator, that manages the sources and their target. | |
* | |
* The logging user app/lib itself *only* needs to know about LogSource. | |
*/ | |
namespace xzero { | |
/** | |
* A logging source. | |
* | |
* Creates a log message such as "[$ClassName] $LogMessage" | |
*/ | |
class LogSource { | |
public: | |
explicit LogSource(const std::string& className); | |
~LogSource(); | |
void debug(const char* fmt, ...); | |
void info(const char* fmt, ...); | |
void warn(const char* fmt, ...); | |
void error(const char* fmt, ...); | |
void enable(); | |
bool isEnabled() const noexcept; | |
void disable(); | |
const std::string& className() const noexcept { return className_; } | |
private: | |
std::string className_; | |
bool enabled_; | |
}; | |
/** | |
* Log Target Interface. | |
* | |
* Logging target implementations (such as a console logger or syslog logger) | |
* must implement this interface. | |
*/ | |
class XZERO_API LogTarget { | |
public: | |
virtual ~LogTarget() {} | |
virtual void debug(const std::string& msg) = 0; | |
virtual void info(const std::string& msg) = 0; | |
virtual void warn(const std::string& msg) = 0; | |
virtual void error(const std::string& msg) = 0; | |
static LogTarget* console(); // standard console logger | |
static LogTarget* syslog(); // standard syslog logger | |
}; | |
enum class LogLevel { | |
None = 0, | |
Error = 1, | |
Warning = 2, | |
Info = 3, | |
Debug = 4, | |
}; | |
/** | |
* Logging Aggregator Service. | |
*/ | |
class XZERO_API LogAggregator { | |
public: | |
LogAggregator() : LogAggregator(LogLevel::Warning, nullptr) {} | |
LogAggregator(LogLevel logLevel, LogTarget* logTarget); | |
LogLevel logLevel() const noexcept { return logLevel_; } | |
void setLogLevel(LogLevel level) { logLevel_ = level; } | |
LogTarget* logTarget() const { return target_; } | |
void setLogTarget(LogTarget* target); | |
void declareSource(LogSource* source); | |
void undeclareSource(LogSource* source); | |
LogSource* findSource(const std::string& className) const; | |
static LogAggregator& get(); | |
private: | |
LogTarget* target; | |
LogLevel logLevel_; | |
std::unordered_map<std::string, bool> enabledSources_; | |
}; | |
} // namespace xzero |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment