Design Pattern Examples
Overview of object-oriented design patterns
Bridge_Exercise.c
Go to the documentation of this file.
1
6
7#include <stdbool.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include "helpers/formatstring.h"
13
14#include "Bridge_ILogger.h"
15
16#include "Bridge_Exercise.h"
17
18//=============================================================================
19//=============================================================================
20
30static void _Bridge_Exercise_Demonstrate_Logging(ILogger* logger, const char* loggerType)
31{
32 if (logger != NULL && loggerType != NULL)
33 {
34 char *buffer = formatstring("Starting log to %s example", loggerType);
35 if (buffer != NULL)
36 {
37 logger->LogTrace(buffer, logger->data);
38 logger->LogInfo("An example of an informational line", logger->data);
39 logger->LogError("An example of an error log entry", logger->data);
40 free(buffer);
41 buffer = NULL;
42 }
43 buffer = formatstring("Done with log to %s example", loggerType);
44 if (buffer != NULL)
45 {
46 logger->LogTrace(buffer, logger->data);
47 free(buffer);
48 buffer = NULL;
49 }
50 }
51}
52
53
54//=============================================================================
55//=============================================================================
56
67// ! [Using Bridge in C]
69{
70 printf("\nBridge_Exercise\n");
71
72 {
73 ILogger* fileLogger = CreateLogger(LoggerType_ToFile, "Bridge.log");
74 if (fileLogger != NULL)
75 {
76 printf(" Example of writing to a log file...\n");
77 _Bridge_Exercise_Demonstrate_Logging(fileLogger, "file");
78 DestroyLogger(fileLogger);
79 }
80 else
81 {
82 printf(" Error! Failed to create a file logger\n");
83 }
84 }
85
86 {
87 ILogger* consoleLogger = CreateLogger(LoggerType_ToConsole, NULL);
88 if (consoleLogger != NULL)
89 {
90 printf(" Example of writing to the console...\n");
91 _Bridge_Exercise_Demonstrate_Logging(consoleLogger, "console");
92 DestroyLogger(consoleLogger);
93 }
94 else
95 {
96 printf(" Error! Failed to create a console logger\n");
97 }
98 }
99
100 {
101 ILogger* nullLogger = CreateLogger(LoggerType_ToNull, NULL);
102 if (nullLogger != NULL)
103 {
104 printf(" Example of writing to a Null object (no output)...\n");
105 _Bridge_Exercise_Demonstrate_Logging(nullLogger, "null");
106 DestroyLogger(nullLogger);
107 }
108 else
109 {
110 printf(" Error! Failed to create a console logger\n");
111 }
112 }
113
114 printf(" Done.\n");
115}
116// ! [Using Bridge in C]
void Bridge_Exercise(void)
Example of using the Bridge Pattern.
static void _Bridge_Exercise_Demonstrate_Logging(ILogger *logger, const char *loggerType)
Helper function to show an example of writing to a logger.
ILogger * CreateLogger(LoggerTypes loggerType, const char *filename)
Return an interface for the specified logger.
void DestroyLogger(ILogger *logger)
Release any resources associated with the given logger. The given ILogger instance is no longer valid...
Declaration of the ILogger interface, along with the CreateLogger() and DestroyLogger() functions use...
@ LoggerType_ToFile
Log to a file. One additional parameter: the name of the file to log to.
@ LoggerType_ToNull
Log to nowhere, that is, throw out all logging. No additional parameters.
@ LoggerType_ToConsole
Log to the console. No additional parameters.
Declaration of the Bridge_Exercise() function as used in the Bridge Pattern.
char * formatstring(const char *format,...)
Use the given string and arguments to return a buffer containing the formatted string....
Definition: formatstring.c:15
void(* LogError)(const char *message, void *data)
Log error messages to the configured output.
void * data
Data associated with a specific instance of a logger.
void(* LogInfo)(const char *message, void *data)
Log informational messages to the configured output.
void(* LogTrace)(const char *message, void *data)