Design Pattern Examples
Overview of object-oriented design patterns
Bridge_Logger.cpp
Go to the documentation of this file.
1
5
6#include <exception>
7#include <memory>
8#include <string>
9#include <sstream>
10#include <cstdarg>
11
12#include "Bridge_Logger.h"
14#include "Bridge_FileLogger.h"
15#include "Bridge_NullLogger.h"
16
17
19{
20
22 // Constructor
25 : _logger(nullptr)
26 {
27
28 switch (loggerType)
29 {
32 break;
33
36 break;
37
39 throw std::runtime_error("A filename must be specified for the Logger.ToFile logger type. Please use Logger::Logger(const std::string&) constructor instead.");
40
41 default:
42 {
43 std::ostringstream msg;
44 msg << "The logger type '" << loggerType << "' is not recognized. Cannot construct a Logger.";
45 throw std::runtime_error(msg.str().c_str());
46 }
47 }
48 }
49
51 // Constructor
53 Logger::Logger(const std::string& filename)
54 {
56 }
57
58
60 // LogTrace
62 void Logger::LogTrace(std::string message)
63 {
64 if (_logger != nullptr)
65 {
66 _logger->LogTrace(message);
67 }
68 }
69
71 // LogInfo
73 void Logger::LogInfo(std::string message)
74 {
75 if (_logger != nullptr)
76 {
77 _logger->LogInfo(message);
78 }
79 }
80
82 // LogError
84 void Logger::LogError(std::string message)
85 {
86 if (_logger != nullptr)
87 {
88 _logger->LogError(message);
89 }
90 }
91
92} // end namespace
Declaration of the Logger class used in the Bridge Pattern.
static std::unique_ptr< ILogger > CreateLogger()
Create an instance of a console logger, which writes to the standard output.
static std::unique_ptr< ILogger > CreateLogger(const std::string &filename)
Create an instance of a file logger, which writes to a file.
void LogInfo(std::string message)
Log informational messages to the configured output.
void LogTrace(std::string message)
LoggerTypes
A value passed to Logger.Logger() constructor to specify the type of logger to create.
Definition: Bridge_Logger.h:34
@ ToFile
Log to a file. One additional parameter: the name of the file to log to.
Definition: Bridge_Logger.h:43
@ ToNull
Log to nowhere, that is, throw out all logging. No additional parameters.
Definition: Bridge_Logger.h:38
@ ToConsole
Log to the console. No additional parameters.
Definition: Bridge_Logger.h:48
void LogError(std::string message)
Log error messages to the configured output.
std::unique_ptr< ILogger > _logger
Definition: Bridge_Logger.h:26
Logger(LoggerTypes loggerType)
Constructor that takes a LoggerTypes value to create a new Logger class.
static std::unique_ptr< ILogger > CreateLogger()
Create an instance of a null logger, a logger that doesn't do anything.
Declaration of the ConsoleLogger class used in the Bridge Pattern.
Declaration of the FileLogger class used in the Bridge Pattern.
Declaration of the NullLogger class used in the Bridge Pattern.
The namespace containing all Design Pattern Examples implemented in C++.