diff options
author | Chris Wilson <chris+github@qwirx.com> | 2014-04-09 22:15:31 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2014-04-09 22:15:31 +0000 |
commit | 44ed218f0b219392a9e89647840c5a61c40858de (patch) | |
tree | dd012f7fa2a1166c8e7937541f8cac9ff89fbb01 /lib/common/Logging.h | |
parent | 5148795dd1ec5b4372008e96bebf1953baeebab4 (diff) |
Add a capturing logger.
Useful in testing whether code logs the expected messages or not.
Diffstat (limited to 'lib/common/Logging.h')
-rw-r--r-- | lib/common/Logging.h | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/common/Logging.h b/lib/common/Logging.h index a0746c69..d74ded61 100644 --- a/lib/common/Logging.h +++ b/lib/common/Logging.h @@ -284,6 +284,58 @@ class Syslog : public Logger // -------------------------------------------------------------------------- // // Class +// Name: Capture +// Purpose: Keeps log messages for analysis in tests. +// Created: 2014/03/08 +// +// -------------------------------------------------------------------------- + +class Capture : public Logger +{ + public: + struct Message + { + Log::Level level; + std::string file; + int line; + std::string message; + }; + + private: + std::vector<Message> mMessages; + + public: + virtual ~Capture() { } + + virtual bool Log(Log::Level level, const std::string& rFile, + int line, std::string& rMessage) + { + Message message; + message.level = level; + message.file = rFile; + message.line = line; + message.message = rMessage; + mMessages.push_back(message); + return true; + } + virtual const char* GetType() { return "Capture"; } + virtual void SetProgramName(const std::string& rProgramName) { } + const std::vector<Message>& GetMessages() const { return mMessages; } + std::string GetString() const + { + std::ostringstream oss; + for (std::vector<Message>::const_iterator i = mMessages.begin(); + i != mMessages.end(); i++) + { + oss << i->message << "\n"; + } + return oss.str(); + } +}; + +// -------------------------------------------------------------------------- +// +// Class // Name: Logging // Purpose: Static logging helper, keeps track of enabled loggers // and distributes log messages to them. |