Design Pattern Examples
Overview of object-oriented design patterns
Bridge_ConsoleLogger.c
Go to the documentation of this file.
1
6
7#include <stdlib.h>
8#include <stdio.h>
9
10#include "Bridge_LogHelper.h"
12
18static void _WriteLine(const char* loglevel, const char* message)
19{
20 char buffer[512] = { 0 };
21
22 if (LogHelper_FormatLogLine(loglevel, message, buffer, sizeof(buffer)))
23 {
24 printf("%s", buffer);
25 }
26}
27
33static void _Console_LogTrace(const char* message, void* data)
34{
35 (void)data; // Unused
36
37 // We don't use the data field, so just add a local reference to avoid
38 // a compiler warning.
39
40 _WriteLine("TRACE", message);
41}
42
43
49static void _Console_LogInfo(const char* message, void* data)
50{
51 (void)data; // Unused
52
53 // We don't use the data field, so just add a local reference to avoid
54 // a compiler warning.
55
56 _WriteLine("INFO ", message);
57}
58
59
65static void _Console_LogError(const char* message, void* data)
66{
67 (void)data; // Unused
68
69 // We don't use the data field, so just add a local reference to avoid
70 // a compiler warning.
71
72 _WriteLine("ERROR", message);
73}
74
75
77// CreateConsoleLogger()
80{
81 ILogger* logger = calloc(1, sizeof(ILogger));
82 if (logger != NULL)
83 {
85 logger->LogInfo = _Console_LogInfo;
87 logger->data = NULL;
88 }
89
90 return logger;
91}
92
94// DestroyConsoleLogger()
97{
98 free(logger);
99}
static void _Console_LogError(const char *message, void *data)
Write an error message to the console.
void DestroyConsoleLogger(ILogger *logger)
Destroy the given instance of an ILogger for outputting to a console.
static void _Console_LogTrace(const char *message, void *data)
Write a trace message to the console.
ILogger * CreateConsoleLogger(void)
Create an instance of an ILogger that outputs to a console.
static void _Console_LogInfo(const char *message, void *data)
Write an informational message to the console.
static void _WriteLine(const char *loglevel, const char *message)
Send a formatted line to the console.
bool LogHelper_FormatLogLine(const char *loglevel, const char *message, char *output, size_t maxOutputSize)
Format a line for logging, including time stamp.
Declaration of the LogHelper_FormatLogLine() function, used in the Bridge Pattern.
Declaration of the ConsoleLogger class used in the Bridge Pattern.
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)