-
Notifications
You must be signed in to change notification settings - Fork 9
/
mce-log.c
376 lines (325 loc) · 8.71 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
* @file mce-log.c
* Logging functions for Mode Control Entity
* <p>
* Copyright (c) 2006 - 2007, 2010 Nokia Corporation and/or its subsidiary(-ies).
* Copyright (c) 2012 - 2020 Jolla Ltd.
* Copyright (c) 2020 Open Mobile Platform LLC.
* <p>
* @author David Weinehall <david.weinehall@nokia.com>
* @author Simo Piiroinen <simo.piiroinen@jollamobile.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/>.
*/
#ifdef OSSOLOG_COMPILE
#include "mce-log.h"
#include <sys/time.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <fnmatch.h>
#include <glib/gprintf.h>
static unsigned int logverbosity = LL_WARN; /**< Log verbosity */
static int logtype = MCE_LOG_STDERR; /**< Output for log messages */
static char *logname = NULL;
/** Get process identity to use for logging
*
* Will default to "mce" before mce_log_open() and after mce_log_close().
*/
static const char *mce_log_name(void)
{
return logname ?: "mce";
}
/** 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",
mce_log_name(),
(long)diff.tv_sec, (long)(diff.tv_usec/1000),
"END OF BURST");
fflush(stderr);
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_clip(loglevel_t loglevel)
{
if( loglevel <= LL_MINIMUM ) {
loglevel = LL_MINIMUM;
}
else if( loglevel > LL_MAXIMUM ) {
loglevel = LL_MAXIMUM;
}
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_CRUCIAL:res = "T"; break;
case LL_EXTRA: res = "X"; 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;
}
/* There is false positive warning during compilation:
*
* "function might be possible candidate for 'gnu_printf' format attribute"
*
* Instead of disabling the whole check via -Wno-suggest-attribute=format,
* use pragmas locally to sweep the issue under carpet...
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
void mce_log_unconditional_va(loglevel_t loglevel, const char *const file,
const char *const function, const char *const fmt, va_list va)
{
gchar *msg = 0;
g_vasprintf(&msg, fmt, va);
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",
mce_log_name(),
(long)tv.tv_sec, (long)(tv.tv_usec/1000),
mce_log_level_tag(loglevel),
msg);
fflush(stderr);
} else {
/* Use NOTICE priority when reporting LL_EXTRA
* and LL_CRUCIAL logging */
switch( loglevel ) {
case LL_EXTRA:
case LL_CRUCIAL:
loglevel = LL_NOTICE;
break;
default:
break;
}
/* loglevels are subset of syslog priorities, so
* we can use loglevel as is for syslog priority */
syslog(loglevel, "%s", msg);
}
g_free(msg);
}
#pragma GCC diagnostic pop
void mce_log_unconditional(loglevel_t loglevel, const char *const file,
const char *const function, const char *const fmt, ...)
{
va_list va;
va_start(va, fmt);
mce_log_unconditional_va(loglevel, file, function, fmt, va);
va_end(va);
}
/**
* 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, ...)
{
loglevel = mce_log_level_clip(loglevel);
if( mce_log_p_(loglevel, file, function) ) {
va_list va;
va_start(va, fmt);
mce_log_unconditional_va(loglevel, file, function, fmt, va);
va_end(va);
}
}
/**
* 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(int verbosity)
{
if( verbosity < LL_MINIMUM )
verbosity = LL_MINIMUM;
else if( verbosity > LL_MAXIMUM )
verbosity = LL_MAXIMUM;
logverbosity = verbosity;
}
/** Set log verbosity
*
* @return current verbosity level
*/
int mce_log_get_verbosity(void)
{
return logverbosity;
}
/**
* 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;
logname = g_strdup(name);
if (logtype == MCE_LOG_SYSLOG)
openlog(name, LOG_PID | LOG_NDELAY, facility);
}
/**
* Close log
*/
void mce_log_close(void)
{
/* Logging (to stderr) after this will use default identity */
g_free(logname), logname = 0;
/* Any further syslog() calls will automatically reopen */
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_(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;
}
/* LL_EXTRA & LL_CRUCIAL are evaluated as WARNING level */
switch( loglevel ) {
case LL_EXTRA:
case LL_CRUCIAL:
loglevel = LL_WARN;;
break;
default:
break;
}
return logverbosity >= loglevel;
}
#endif /* OSSOLOG_COMPILE */