Design Pattern Examples
Overview of object-oriented design patterns
Bridge_Exercise.cpp
Go to the documentation of this file.
1
5
6#include <iostream>
7
8#include "helpers/formatstring.h"
9
10#include "Bridge_Exercise.h"
11#include "Bridge_Logger.h"
12
13namespace // Anonymous
14{
15 using namespace DesignPatternExamples_cpp;
16
27 Logger& logger, std::string loggerType)
28 {
29 std::string output;
30 output = Helpers::formatstring("Starting log to %s example", loggerType.c_str());
31 logger.LogTrace(output);
32
33 logger.LogInfo("An example of an informational line");
34 logger.LogError("An example of an error log entry");
35
36 output = Helpers::formatstring("Done with log to %s example", loggerType.c_str());
37 logger.LogTrace(output);
38 }
39
40} // end anonymous namespace
41
42
44{
45
56 // ! [Using Bridge in C++]
58 {
59 std::cout << std::endl;
60 std::cout << "Bridge Exercise" << std::endl;
61
62 // Note: Use local contexts to automatically close the various loggers
63 // when the loggers go out of scope.
64 {
65 Logger logger("Bridge.log"); // Logger::LoggerTypes::ToFile type
66 std::cout << " Example of writing to a log file..." << std::endl;
68 }
69
70 {
72 std::cout << " Example of writing to the console..." << std::endl;
73 _Bridge_Exercise_Demonstrate_Logging(logger, "console");
74 }
75
76 {
78 std::cout << " Example of writing to a Null object (no output)..."
79 << std::endl;
80 // Note: The resulting log lines will not be shown anywhere.
82 }
83
84 std::cout << " Done." << std::endl;
85 }
86 // ! [Using Bridge in C++]
87
88} // end namespace
static void _Bridge_Exercise_Demonstrate_Logging(ILogger *logger, const char *loggerType)
Helper function to show an example of writing to a logger.
Declaration of the Logger class used in the Bridge Pattern.
Represents the logger object to be used in the program.
Definition: Bridge_Logger.h:23
void LogInfo(std::string message)
Log informational messages to the configured output.
void LogTrace(std::string message)
@ 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.
Declaration of the Bridge_Exercise() function as used in the Bridge Pattern.
The namespace containing all Design Pattern Examples implemented in C++.
void Bridge_Exercise()
Example of using the Bridge design pattern.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....