-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.c
202 lines (154 loc) · 5.42 KB
/
logger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* File: commandQueue.c
* author: patrick conroy
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <sys/time.h>
#include "logger.h"
static int Logger_Log (char *format, char *level, ...);
static char *getCurrentDateTime( void );
static char currentDateTimeBuffer[ 80 ];
static FILE *fp;
static int logFileOpen = FALSE;
static int debugValue = 3;
// ----------------------------------------------------------------------------
//
// How "debugValue" works. It's an integer
// 0 = Log nothing
// 5 = Log Fatal and Error and Warning and Debug and Info
// 4 = Log Fatal and Error and Warning and Debug
// 3 = Log Fatal and Error and Warning
// 2 = Log Fatal and Error
// 1 = Log Fatal
// ----------------------------------------------------------------------------
void Logger_Initialize (char *fileName, int debugLevel)
{
printf( "ATTEMPTING TO OPEN [%s] DEBUG LEVEL [%d]\n", fileName, debugLevel ); fflush(stdout);
if (fileName != (char *) 0 ) {
debugValue = debugLevel;
fp = fopen( fileName, "a" );
if (fp != (FILE *) 0)
logFileOpen = TRUE;
}
}
// ----------------------------------------------------------------------------
void Logger_Terminate()
{
if (logFileOpen)
fclose( fp );
}
// ----------------------------------------------------------------------------
void Logger_LogDebug (char *format, ... )
{
if (!logFileOpen || debugValue < 4)
return;
va_list args;
va_start( args, format ); // the last fixed parameter
int numWritten = fprintf( fp, "DEBUG|%s|", getCurrentDateTime() );
numWritten += vfprintf( fp, format, args );
va_end( args );
fflush( fp );
}
// ----------------------------------------------------------------------------
void Logger_LogWarning (char *format, ... )
{
if (!logFileOpen || debugValue < 3)
return;
va_list args;
va_start( args, format ); // the last fixed parameter
int numWritten = fprintf( fp, "WARNING|%s|", getCurrentDateTime() );
numWritten += vfprintf( fp, format, args );
va_end( args );
fflush( fp );
}
// ----------------------------------------------------------------------------
void Logger_LogError (char *format, ... )
{
if (!logFileOpen || debugValue < 2)
return;
va_list args;
va_start( args, format ); // the last fixed parameter
int numWritten = fprintf( fp, "ERROR|%s|", getCurrentDateTime() );
numWritten += vfprintf( fp, format, args );
va_end( args );
fflush( fp );
}
// ----------------------------------------------------------------------------
void Logger_LogFatal (char *format, ... )
{
if (!logFileOpen || debugValue < 1)
return;
va_list args;
va_start( args, format ); // the last fixed parameter
int numWritten = fprintf( fp, "FATAL|%s|", getCurrentDateTime() );
numWritten += vfprintf( fp, format, args );
va_end( args );
fflush( fp );
exit( 1 );
}
// ----------------------------------------------------------------------------
void Logger_LogInfo (char *format, ... )
{
if (!logFileOpen || debugValue < 5)
return;
va_list args;
va_start( args, format ); // the last fixed parameter
int numWritten = fprintf( fp, "INFO|%s|", getCurrentDateTime() );
numWritten += vfprintf( fp, format, args );
va_end( args );
fflush( fp );
}
// ----------------------------------------------------------------------------
static int Logger_Log (char *level, char *format, ...)
{
if (!logFileOpen)
return 0;
va_list args;
va_start( args, format ); // the last fixed parameter
int numWritten = fprintf( fp, "%s|%s|", level, getCurrentDateTime() );
numWritten += vfprintf( fp, format, args );
va_end( args );
return numWritten;
}
// ----------------------------------------------------------------------------
static char *getCurrentDateTime()
{
//
// Something quick and dirty... Fix this later - thread safe
time_t current_time;
struct tm *tmPtr;
struct timeval tv;
memset( currentDateTimeBuffer, '\0', sizeof currentDateTimeBuffer );
int milliSeconds = -1;
if (gettimeofday( &tv, NULL ) == 0) {
milliSeconds = ((int) tv.tv_usec) / 1000;
}
int len = 0;
/* Obtain current time as seconds elapsed since the Epoch. */
current_time = time( NULL );
if (current_time > 0) {
/* Convert to local time format. */
tmPtr = localtime( ¤t_time );
if (tmPtr != NULL) {
len = strftime( currentDateTimeBuffer,
sizeof currentDateTimeBuffer,
"%F %T",
tmPtr );
}
}
//
// strftime returns number of bytes put into the string
if (len > 0 && milliSeconds >= 0) {
currentDateTimeBuffer[ len ] = '.';
currentDateTimeBuffer[ len + 1 ] = (milliSeconds % 1000) / 100 + '0';
currentDateTimeBuffer[ len + 2 ] = ((milliSeconds % 100) / 10) + '0';
currentDateTimeBuffer[ len + 3 ] = (milliSeconds % 10) + '0';
currentDateTimeBuffer[ len + 4 ] = '\0';
}
return ¤tDateTimeBuffer[ 0 ];
}