Design Pattern Examples
Overview of object-oriented design patterns
Bridge_LogHelper.c
Go to the documentation of this file.
1
5
6#include "Bridge_LogHelper.h"
7
8#include <stdlib.h>
9#include <stdio.h>
10
11#include "helpers/datetime.h"
12#include "helpers/makelocaltime.h"
13
19static const char* _GetTimeStamp(void)
20{
21 time_t now = datetime_now();
22 return datetime_to_string(now);
23}
24
25
27// LogHelper_FormatLogLine()
29bool LogHelper_FormatLogLine(const char* loglevel, const char* message, char* output, size_t maxOutputSize)
30{
31 bool success = false;
32
33 if (loglevel != NULL && message != NULL && output != NULL && maxOutputSize > 0)
34 {
35 *output = '\0';
36 const char* timestamp = _GetTimeStamp();
37 int numChars = snprintf(output, maxOutputSize, "%s [%s] %s\n", timestamp, loglevel, message);
38 if (numChars != -1)
39 {
40 success = true;
41 }
42 }
43 return success;
44}
bool LogHelper_FormatLogLine(const char *loglevel, const char *message, char *output, size_t maxOutputSize)
Format a line for logging, including time stamp.
static const char * _GetTimeStamp(void)
Return a regular time stamp of the current time in local time.
Declaration of the LogHelper_FormatLogLine() function, used in the Bridge Pattern.
time_t datetime_now(void)
Retrieve a time stamp that represents the current time.
Definition: datetime.c:27
const char * datetime_to_string(time_t timestamp)
Convert the specified time stamp to a string.
Definition: datetime.c:12