-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
192 lines (139 loc) · 4.37 KB
/
log.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
/* ************************************************************************
*
* logging, errors and warnings
*
* (c) 1994-2012 by Markus Reschke
*
* ************************************************************************ */
/*
* local constants
*/
#define LOG_C 1
/*
* include header files
*/
/* local header files */
#include "common.h" /* common stuff */
#include "variables.h" /* global variables */
#include "functions.h" /* external funtions */
/*
* extra definitions if required
*/
/* stdout file descriptor */
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
/* stderr file descriptor */
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
/* ************************************************************************
* logging
* ************************************************************************ */
/*
* log stuff (printf syntax)
* - write to logfile if one is set up
* - write to console if not logfile is set up
*/
void Log(unsigned short int Type, const char *Line, ...)
{
va_list ArgPointer;
size_t Length = 0; /* string length written */
size_t Size = 0; /* used size of buffer */
char *Buffer; /* support pointer */
char *Message;
FILE *Stream;
_Bool ExitFlag = False; /* exit flag */
/* sanity checks */
if ((Line == NULL) || (LogBuffer == NULL)) return;
/*
* init
*/
Buffer = LogBuffer; /* init pointer */
if (Type == L_ERR) ExitFlag = True; /* set exit flag for errors */
/*
* add date, time and PID for logfile
* format: <date> <time> [<process#>] ...
*/
if (Env && Env->Log)
{
time_t UnixTime;
struct tm *DateTime;
/* get current date and time */
if (time(&UnixTime) != -1) /* get UNIX time */
{
DateTime = localtime(&UnixTime); /* convert unix time */
if (DateTime != NULL)
{
/* format: year-month-day hh:mm:ss */
Length = strftime(Buffer, 22, "%F %T", DateTime);
if (Length > 0) /* success */
{
Buffer += Length; /* move pointer to end of string */
Size += Length; /* update counter */
}
else /* error */
{
Buffer[0] = 0; /* reset buffer */
}
}
}
/* add process ID */
Length = snprintf(Buffer, DEFAULT_BUFFER_SIZE - 2 - Size," [%d]: ", Env->PID);
Buffer += Length; /* move pointer to end of string */
Size += Length; /* update size */
}
Message = Buffer; /* start of message */
/*
* add log message
*/
va_start(ArgPointer, Line); /* set pointer for first unknown arg */
/* print message to string with size limitation */
Length = vsnprintf(Buffer, DEFAULT_BUFFER_SIZE - 2 - Size, Line, ArgPointer);
va_end(ArgPointer); /* clean up arg list */
/* add LineFeed */
Buffer += Length; /* move pointer to end of string */
Buffer[0] = '\n';
Buffer[1] = 0;
/*
* output message
*/
if (Env && Env->Log) /* log to file */
{
fputs(LogBuffer, Env->Log); /* write */
fflush(Env->Log); /* flush any buffered output */
}
else /* log to console */
{
/* set output stream */
if (Type == L_ERR) Stream = stderr;
else Stream = stdout;
fputs(Message, Stream); /* write */
fflush(Stream); /* flush any buffered output */
}
/*
* exit this program on error
*/
if (ExitFlag) exit(EXIT_FAILURE);
}
/* ************************************************************************
* convenience logging functions
* ************************************************************************ */
/*
* log cfg parser error
*/
void LogCfgError(void)
{
Log(L_WARN, "Error in cfg file (%s), line %d!",
Env->CfgInUse, Env->CfgLinenumber);
}
/* ************************************************************************
* clean up of local definitions
* ************************************************************************ */
/*
* undo local constants
*/
#undef LOG_C
/* ************************************************************************
* EOF
* ************************************************************************ */