/
mce-log.c
304 lines (262 loc) · 7 KB
/
mce-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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/**
* @file mce-log.c
* Logging functions for Mode Control Entity
* <p>
* Copyright © 2006-2007, 2010 Nokia Corporation and/or its subsidiary(-ies).
* <p>
* @author David Weinehall <david.weinehall@nokia.com>
*
* mce is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* mce 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mce. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdbool.h>
#include <fnmatch.h>
#include <glib.h>
#include <glib/gprintf.h>
#ifdef OSSOLOG_COMPILE
#include <stdio.h> /* fprintf() */
#include <stdarg.h> /* va_start(), va_end(), vfprintf() */
#include <string.h> /* strdup() */
#include <syslog.h> /* openlog(), closelog(), vsyslog() */
#include <sys/time.h>
#include "mce-log.h"
static unsigned int logverbosity = LL_WARN; /**< Log verbosity */
static int logtype = MCE_LOG_STDERR; /**< Output for log messages */
static char *logname = NULL;
/** Get monotonic time as struct timeval */
static void monotime(struct timeval *tv)
{
struct timespec ts;
clock_gettime(CLOCK_BOOTTIME, &ts);
TIMESPEC_TO_TIMEVAL(tv, &ts);
}
static void timestamp(struct timeval *tv)
{
static struct timeval start, prev;
struct timeval diff;
monotime(tv);
if( !timerisset(&start) )
prev = start = *tv;
timersub(tv, &prev, &diff);
if( diff.tv_sec >= 4 ) {
timersub(tv, &start, &diff);
fprintf(stderr, "%s: T+%ld.%03ld %s\n\n",
logname,
(long)diff.tv_sec, (long)(diff.tv_usec/1000),
"END OF BURST");
start = *tv;
}
prev = *tv;
timersub(tv, &start, tv);
}
/** Make sure loglevel is in the supported range
*
* @param loglevel level to check
*
* @return log level clipped to LL_CRIT ... LL_DEBUG range
*/
static loglevel_t mce_log_level_normalize(loglevel_t loglevel)
{
if( loglevel < LL_ALERT ) {
loglevel = LL_ALERT;
}
else if( loglevel > LL_DEBUG ) {
loglevel = LL_DEBUG;
}
return loglevel;
}
/** Get level indication tag to include in stderr logging
*
* @param loglevel level for the message
*
* @return level indication string
*/
static const char *mce_log_level_tag(loglevel_t loglevel)
{
const char *res = "?";
switch( loglevel ) {
case LL_ALERT: res = "A"; break;
case LL_CRIT: res = "C"; break;
case LL_ERR: res = "E"; break;
case LL_WARN: res = "W"; break;
case LL_NOTICE: res = "N"; break;
case LL_INFO: res = "I"; break;
case LL_DEBUG: res = "D"; break;
default: break;
}
return res;
}
/** Looking at white (ascii) character predicate
*/
static inline bool mce_log_white_p(const char *str)
{
int c = (unsigned char)*str;
return (c > 0x00) && (c <= 0x20);
}
/** Looking at non-white (ascii) character predicate
*/
static inline bool mce_log_black_p(const char *str)
{
int c = (unsigned char)*str;
return c > 0x20;
}
/** Strip whitespace from log string, similarly to what syslog does
*/
static char *mce_log_strip_string(char *str)
{
if( !str )
goto EXIT;
char *src = str;
char *dst = str;
// skip leading white space
while( mce_log_white_p(src) ) ++src;
for( ;; ) {
// copy non-white as-is
while( mce_log_black_p(src) ) *dst++ = *src++;
// skip internal / trailing white space
while( mce_log_white_p(src) ) ++src;
if( !*src ) break;
// compress internal whitespace to single space
*dst++ = ' ';
}
*dst = 0;
EXIT:
return str;
}
/**
* Log debug message with optional filename and function name attached
*
* @param loglevel The level of severity for this message
* @param fmt The format string for this message
* @param ... Input to the format string
*/
void mce_log_file(loglevel_t loglevel, const char *const file,
const char *const function, const char *const fmt, ...)
{
va_list args;
loglevel = mce_log_level_normalize(loglevel);
if( mce_log_p_(loglevel, file, function) ) {
gchar *msg = 0;
va_start(args, fmt);
g_vasprintf(&msg, fmt, args);
va_end(args);
if( file && function ) {
gchar *tmp = g_strconcat(file, ": ", function, "(): ",
mce_log_strip_string(msg), NULL);
g_free(msg), msg = tmp;
}
if (logtype == MCE_LOG_STDERR) {
struct timeval tv;
timestamp(&tv);
fprintf(stderr, "%s: T+%ld.%03ld %s: %s\n",
logname,
(long)tv.tv_sec, (long)(tv.tv_usec/1000),
mce_log_level_tag(loglevel),
msg);
} else {
/* loglevels are subset of syslog priorities, so
* we can use loglevel as is for syslog priority */
syslog(loglevel, "%s", msg);
}
g_free(msg);
}
}
/**
* Set log verbosity
* messages with loglevel higher than or equal to verbosity will be logged
*
* @param verbosity minimum level for log level
*/
void mce_log_set_verbosity(const int verbosity)
{
logverbosity = verbosity;
}
/**
* Open log
*
* @param name identifier to use for log messages
* @param facility the log facility; normally LOG_USER or LOG_DAEMON
* @param type log type to use; MCE_LOG_STDERR or MCE_LOG_SYSLOG
*/
void mce_log_open(const char *const name, const int facility, const int type)
{
logtype = type;
if (logtype == MCE_LOG_SYSLOG)
openlog(name, LOG_PID | LOG_NDELAY, facility);
else
logname = g_strdup(name);
}
/**
* Close log
*/
void mce_log_close(void)
{
g_free(logname), logname = 0;
if (logtype == MCE_LOG_SYSLOG)
closelog();
}
static GSList *mce_log_patterns = 0;
static GHashTable *mce_log_functions = 0;
void mce_log_add_pattern(const char *pat)
{
// NB these are never released by desing
mce_log_patterns = g_slist_prepend(mce_log_patterns, strdup(pat));
if( !mce_log_functions )
mce_log_functions = g_hash_table_new_full(g_str_hash,
g_str_equal,
free, 0);
}
static bool mce_log_check_pattern(const char *func)
{
gpointer hit = 0;
if( !mce_log_functions )
goto EXIT;
if( (hit = g_hash_table_lookup(mce_log_functions, func)) )
goto EXIT;
hit = GINT_TO_POINTER(1);
for( GSList *item = mce_log_patterns; item; item = item->next ) {
const char *pat = item->data;
if( fnmatch(pat, func, 0) != 0 )
continue;
hit = GINT_TO_POINTER(2);
break;
}
g_hash_table_replace(mce_log_functions, strdup(func), hit);
EXIT:
return GPOINTER_TO_INT(hit) > 1;
}
/**
* Log level testing predicate
*
* For testing whether given level of logging is allowed
* before spending cpu time for gathering parameters etc
*
* @param loglevel level of logging we might do
*
* @return 1 if logging at givel level is enabled, 0 if not
*/
int mce_log_p_(const loglevel_t loglevel,
const char *const file,
const char *const func)
{
if( mce_log_functions && file && func ) {
char temp[256];
snprintf(temp, sizeof temp, "%s:%s", file, func);
if( mce_log_check_pattern(temp) )
return true;
}
if( loglevel < LL_CRIT )
return logverbosity >= LL_NOTICE;
return logverbosity >= loglevel;
}
#endif /* OSSOLOG_COMPILE */