-
Notifications
You must be signed in to change notification settings - Fork 3
/
logmsg.c
249 lines (225 loc) · 7.51 KB
/
logmsg.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*************************************************
* logmsg.c
* Copyright (c) Rennie deGraaf, 2005-2007. All rights reserved.
* $Id: logmsg.c 14 2005-07-26 02:00:59Z degraaf $
*
* Generic system for logging error or status messages to various targets.
* Currently, valid targets are stdout, stderr, syslog, or any file. The
* default target is stderr. Messages are formatted along syslog conventions.
*
* Note: this facility is not re-entrant. Be careful using it in a multi-
* threaded environment.
*
* logmsg_open() - open the logmsg facility
* logmsg() - write a message to the current log
* logmsg_close() - close the logmsg facility
*
* This file is part of the libwheel project.
*
* libwheel is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* libwheel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libwheel; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**************************************************/
#include <syslog.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include "logmsg.h"
/* internal logmsg configuration object */
typedef struct
{
logmsg_facility_t facility;
unsigned options;
const char* name;
FILE* file;
} logmsg_t;
/* string equivalents for values of logmsg_priority_t */
static const char* _priority_tag[] =
{
"Emergency:",
"Alert:",
"Critical:",
"Error:",
"Warning:",
"Notice:",
"Info:",
"Debug:"
};
/* syslog priority equivalents for values of logmsg_priority_t */
static const int _priority_id[] =
{
LOG_EMERG,
LOG_ALERT,
LOG_CRIT,
LOG_ERR,
LOG_WARNING,
LOG_NOTICE,
LOG_INFO,
LOG_DEBUG
};
/* global logmsg configuration object */
static logmsg_t _log_config = {logmsg_stderr, 0, "", NULL};
/*****************************
int logmsg_open(logmst_facility_t, unsigned, const char*)
Initializes the logmsg facility.
PARAMS: logmsg_facility_t facility - the logging facility to use
unsigned options - a set of option flags
const char* name - a string whose purpose is determined by the value
of facility:
file -> the name of the file to use
syslog, stdout, stderr -> a string to prepend to each log message
RETURNS: 0 on success
1 on failure, with errno set appropriately.
*/
int logmsg_open(logmsg_facility_t facility, unsigned options, const char* name)
{
int syslog_opt = 0;
_log_config.facility = facility;
_log_config.options = options;
_log_config.name = name;
_log_config.file = NULL;
switch (_log_config.facility)
{
case logmsg_stdout:
_log_config.file = stdout;
break;
case logmsg_stderr:
_log_config.file = stderr;
break;
case logmsg_syslog:
if (_log_config.options & LOGMSG_PID)
syslog_opt |= LOG_PID;
openlog(name, syslog_opt, LOG_USER);
break;
case logmsg_file:
_log_config.file = fopen(name, "a");
if (_log_config.file == NULL)
return -1;
break;
}
return 0;
}
/*****************************
int logmsg(logmsg_priority_t, const char*, ...)
Prints a message to the appropriate destination. The message is formatted
according to syslog() conventions.
PARAMS: logmsg_priority_t priority - the priority of the message. How this is
interpreted depends on the facility:
file, stdout, stderr -> prepends a string indicating the priority
syslog -> uses te equivalent syslog priority
const char* format - a printf()-style format string, followed by
arguments.
Returns: 0 on success
-1 on failure
*/
int logmsg(logmsg_priority_t priority, const char* format, ...)
{
va_list args;
int ret;
va_start(args, format);
ret = vlogmsg(priority, format, args);
va_end(args);
return ret;
}
/*****************************
int vlogmsg(logmsg_priority_t, const char*, va_list)
Prints a message to the appropriate destination. The message is formatted
according to syslog() conventions.
PARAMS: logmsg_priority_t priority - the priority of the message. How this is
interpreted depends on the facility:
file, stdout, stderr -> prepends a string indicating the priority
syslog -> uses te equivalent syslog priority
const char* format - a printf()-style format string
va_list args - arguments to *format
Returns: 0 on success
-1 on failure
*/
int vlogmsg(logmsg_priority_t priority, const char* format, va_list args)
{
time_t t;
struct tm tm;
char timebuf[100];
int ret;
/* safety check, in case logmsg is called without first calling logmsg_open */
if (_log_config.file == NULL)
_log_config.file = stderr;
switch (_log_config.facility)
{
case logmsg_stdout:
case logmsg_stderr:
case logmsg_file:
/* print the time */
t = time(NULL);
localtime_r(&t, &tm);
strftime(timebuf, 100, "%b %d %T ", &tm);
ret = fputs(timebuf, _log_config.file);
if (ret == EOF) return -1;
/* print name for stdout and stderr */
if (_log_config.facility == logmsg_stdout || _log_config.facility == logmsg_stderr)
{
ret = fputs(_log_config.name, _log_config.file);
if (ret == EOF) return -1;
ret = fputc(' ', _log_config.file);
if (ret == EOF) return -1;
}
/* print the PID, if LOGMSG_PID is set */
if (_log_config.options & LOGMSG_PID)
{
char buf[30];
snprintf(buf, 30, "[%i] ", getpid());
ret = fputs(buf, _log_config.file);
if (ret == EOF) return -1;
}
/* print the priority */
ret = fputs(_priority_tag[priority], _log_config.file);
if (ret == EOF) return -1;
ret = fputc(' ', _log_config.file);
if (ret == EOF) return -1;
/* print the actual message */
ret = vfprintf(_log_config.file, format, args);
if (ret < 0) return -1;
ret = fputc('\n', _log_config.file);
if (ret == EOF) return -1;
break;
case logmsg_syslog:
vsyslog(_priority_id[priority], format, args);
break;
}
return 0;
}
/*****************************
void logmsg_close()
Shuts down the logmsg facility, in a manner appropriate to the facility
Returns: 0 on success
EOF on failure, with errno set appropriately
*/
int logmsg_close()
{
switch (_log_config.facility)
{
case logmsg_stdout:
return 0;
case logmsg_stderr:
return 0;
case logmsg_syslog:
closelog();
return 0;
case logmsg_file:
return fclose(_log_config.file);
default:
return EOF;
}
}