Design Pattern Examples
Overview of object-oriented design patterns
Bridge_FileLogger.cpp
Go to the documentation of this file.
1
5
6#include <iostream>
7#include <fstream>
8
9#include "Bridge_FileLogger.h"
11
12namespace // Anonymous
13{
17 class FileLoggerImpl : public DesignPatternExamples_cpp::ILogger
18 {
19 private:
20 std::ofstream _outputFile;
21
22 private:
23
29 void _WriteLine(const std::string& logLevel, const std::string& msg)
30 {
31 if (_outputFile.is_open())
32 {
33 std::string output = DesignPatternExamples_cpp::LoggerHelpers::FormatLogLine(logLevel, msg);
34 _outputFile << output << std::endl;
35 }
36
37 }
38
39 public:
43 FileLoggerImpl(const std::string& filename)
44 {
45 if (!filename.empty())
46 {
47 _outputFile.open(filename.c_str(), std::ofstream::out);
48 if (!_outputFile.is_open())
49 {
50
51 }
52 }
53 }
54
55 // ILogger Members
56
57 void LogTrace(const std::string& msg)
58 {
59 _WriteLine("TRACE", msg);
60 }
61
62 void LogInfo(const std::string& msg)
63 {
64 _WriteLine("INFO ", msg);
65 }
66
67 void LogError(const std::string& msg)
68 {
69 _WriteLine("ERROR", msg);
70 }
71 };
72
73} // end anonymous namespace
74
75
77{
82 std::unique_ptr<ILogger> FileLogger::CreateLogger(const std::string& filename)
83 {
84 return std::make_unique<FileLoggerImpl>(filename);
85 }
86
87} // end namespace
static void _WriteLine(const char *loglevel, const char *message)
Send a formatted line to the console.
Declaration of the LoggerHelpers namespace functions used in the Bridge Pattern.
static std::unique_ptr< ILogger > CreateLogger(const std::string &filename)
Create an instance of a file logger, which writes to a file.
Declaration of the FileLogger class used in the Bridge Pattern.
std::string FormatLogLine(const std::string &logLevel, const std::string &msg)
Format a line for logging, including time stamp.
The namespace containing all Design Pattern Examples implemented in C++.
Represents an implementation of a logger object as call from the Logger class.
virtual void LogInfo(const std::string &msg)=0
Log informational messages to the configured output.
virtual void LogTrace(const std::string &msg)=0
Log trace messages to the configured output.
virtual void LogError(const std::string &msg)=0
Log error messages to the configured output.