Design Pattern Examples
Overview of object-oriented design patterns
Bridge_FileLogger.c
Go to the documentation of this file.
1
6
7#include <stdlib.h>
8#include <stdio.h>
9#include <errno.h>
10#include <string.h>
11
12#include "Bridge_LogHelper.h"
13#include "Bridge_FileLogger.h"
14
21static void _WriteLine(const char* loglevel, const char* message, FILE* fp)
22{
23 char buffer[512] = { 0 };
24
25 if (fp != NULL)
26 {
27 if (LogHelper_FormatLogLine(loglevel, message, buffer, sizeof(buffer)))
28 {
29 size_t numElementsToWrite = strlen(buffer);
30 size_t numElementsWritten = fwrite(buffer, sizeof(char), numElementsToWrite, fp);
31 if (numElementsWritten != numElementsToWrite)
32 {
33 int errorcode = errno;
34 char *errmessage = strerror(errorcode);
35 printf(" Error (%d) writing log file: %s\n", errorcode, errmessage);
36 printf("%s", buffer);
37 }
38 }
39 }
40}
41
48static void _File_LogTrace(const char* message, void* data)
49{
50 FILE* fp = NULL;
51 if (data != NULL)
52 {
53 fp = (FILE*)data;
54 }
55 _WriteLine("TRACE", message, fp);
56}
57
58
65static void _File_LogInfo(const char* message, void* data)
66{
67 FILE* fp = NULL;
68 if (data != NULL)
69 {
70 fp = (FILE*)data;
71 }
72 _WriteLine("INFO ", message, fp);
73}
74
75
82static void _File_LogError(const char* message, void* data)
83{
84 FILE* fp = NULL;
85 if (data != NULL)
86 {
87 fp = (FILE*)data;
88 }
89 _WriteLine("ERROR", message, fp);
90}
91
92
94// CreateFileLogger()
96ILogger* CreateFileLogger(const char* filename)
97{
98 ILogger* logger = calloc(1, sizeof(ILogger));
99 if (logger != NULL && filename != NULL)
100 {
101 FILE *fp = NULL;
102 logger->LogTrace = _File_LogTrace;
103 logger->LogInfo = _File_LogInfo;
104 logger->LogError = _File_LogError;
105 logger->data = NULL;
106 fp = fopen(filename, "w");
107 if (fp != NULL)
108 {
109 logger->data = fp;
110 }
111 else
112 {
113 int errorcode = errno;
114 char *errmessage = strerror(errorcode);
115 printf(" Error (%d) opening log file '%s': %s\n", errorcode, filename, errmessage);
116 free(logger);
117 logger = NULL;
118 }
119 }
120
121 return logger;
122}
123
125// DestroyFileLogger()
128{
129 if (logger != NULL)
130 {
131 if (logger->data != NULL)
132 {
133 FILE* fp = (FILE*)logger->data;
134 fclose(fp);
135 }
136 free(logger);
137 }
138}
static void _WriteLine(const char *loglevel, const char *message, FILE *fp)
Send a formatted line to a log file.
static void _File_LogError(const char *message, void *data)
Write an error message to a log file.
static void _File_LogInfo(const char *message, void *data)
Write an informational message to a log file.
static void _File_LogTrace(const char *message, void *data)
Write a trace message to a log file.
ILogger * CreateFileLogger(const char *filename)
Create an instance of an ILogger that outputs to a file.
void DestroyFileLogger(ILogger *logger)
Destroy the given instance of an ILogger for outputting to a file.
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 FileLogger 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)