/
mce-log.c
376 lines (325 loc) · 8.71 KB
1
2
3
4
/**
* @file mce-log.c
* Logging functions for Mode Control Entity
* <p>
5
6
7
* 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.
8
9
* <p>
* @author David Weinehall <david.weinehall@nokia.com>
10
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
11
12
13
14
15
16
17
18
19
20
21
22
23
*
* 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/>.
*/
24
25
26
27
28
29
30
#ifdef OSSOLOG_COMPILE
#include "mce-log.h"
#include <sys/time.h>
31
32
#include <stdlib.h>
#include <stdbool.h>
33
#include <string.h>
34
35
#include <fnmatch.h>
36
37
38
39
40
41
#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;
42
43
44
45
46
47
48
49
50
/** 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";
}
51
52
53
54
/** Get monotonic time as struct timeval */
static void monotime(struct timeval *tv)
{
struct timespec ts;
55
clock_gettime(CLOCK_BOOTTIME, &ts);
56
57
58
59
60
61
62
TIMESPEC_TO_TIMEVAL(tv, &ts);
}
static void timestamp(struct timeval *tv)
{
static struct timeval start, prev;
struct timeval diff;
monotime(tv);
63
64
if( !timerisset(&start) )
prev = start = *tv;
65
timersub(tv, &prev, &diff);
66
if( diff.tv_sec >= 4 ) {
67
68
timersub(tv, &start, &diff);
fprintf(stderr, "%s: T+%ld.%03ld %s\n\n",
69
mce_log_name(),
70
71
(long)diff.tv_sec, (long)(diff.tv_usec/1000),
"END OF BURST");
72
fflush(stderr);
73
start = *tv;
74
}
75
76
77
78
prev = *tv;
timersub(tv, &start, tv);
}
79
80
81
82
83
84
/** Make sure loglevel is in the supported range
*
* @param loglevel level to check
*
* @return log level clipped to LL_CRIT ... LL_DEBUG range
*/
85
static loglevel_t mce_log_level_clip(loglevel_t loglevel)
86
{
87
88
if( loglevel <= LL_MINIMUM ) {
loglevel = LL_MINIMUM;
89
}
90
91
else if( loglevel > LL_MAXIMUM ) {
loglevel = LL_MAXIMUM;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
}
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 ) {
106
case LL_CRUCIAL:res = "T"; break;
107
case LL_EXTRA: res = "X"; break;
108
109
110
111
112
113
114
115
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;
}
116
117
118
return res;
}
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
/** 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;
}
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
/* 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);
196
fflush(stderr);
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
} 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);
}
228
229
230
231
232
233
234
/**
* 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
*/
235
void mce_log_file(loglevel_t loglevel, const char *const file,
236
237
const char *const function, const char *const fmt, ...)
{
238
loglevel = mce_log_level_clip(loglevel);
239
240
if( mce_log_p_(loglevel, file, function) ) {
241
242
243
244
va_list va;
va_start(va, fmt);
mce_log_unconditional_va(loglevel, file, function, fmt, va);
va_end(va);
245
246
247
248
249
250
251
252
253
}
}
/**
* Set log verbosity
* messages with loglevel higher than or equal to verbosity will be logged
*
* @param verbosity minimum level for log level
*/
254
void mce_log_set_verbosity(int verbosity)
255
{
256
257
258
259
260
if( verbosity < LL_MINIMUM )
verbosity = LL_MINIMUM;
else if( verbosity > LL_MAXIMUM )
verbosity = LL_MAXIMUM;
261
262
263
logverbosity = verbosity;
}
264
265
266
267
268
269
270
271
272
/** Set log verbosity
*
* @return current verbosity level
*/
int mce_log_get_verbosity(void)
{
return logverbosity;
}
273
274
275
276
277
278
279
280
281
282
/**
* 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;
283
logname = g_strdup(name);
284
285
286
287
288
289
290
291
292
293
if (logtype == MCE_LOG_SYSLOG)
openlog(name, LOG_PID | LOG_NDELAY, facility);
}
/**
* Close log
*/
void mce_log_close(void)
{
294
/* Logging (to stderr) after this will use default identity */
295
g_free(logname), logname = 0;
296
297
/* Any further syslog() calls will automatically reopen */
298
299
300
if (logtype == MCE_LOG_SYSLOG)
closelog();
}
301
302
303
304
305
306
static GSList *mce_log_patterns = 0;
static GHashTable *mce_log_functions = 0;
void mce_log_add_pattern(const char *pat)
{
307
// NB these are never released by desing
308
309
mce_log_patterns = g_slist_prepend(mce_log_patterns, strdup(pat));
310
311
312
313
314
if( !mce_log_functions )
mce_log_functions = g_hash_table_new_full(g_str_hash,
g_str_equal,
free, 0);
315
316
317
318
}
static bool mce_log_check_pattern(const char *func)
{
319
gpointer hit = 0;
320
321
322
if( !mce_log_functions )
goto EXIT;
323
324
325
if( (hit = g_hash_table_lookup(mce_log_functions, func)) )
goto EXIT;
326
327
hit = GINT_TO_POINTER(1);
328
329
330
331
332
333
334
335
336
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);
337
338
EXIT:
339
return GPOINTER_TO_INT(hit) > 1;
340
341
}
342
343
344
345
346
347
348
349
350
351
/**
* 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
*/
352
int mce_log_p_(loglevel_t loglevel,
353
354
const char *const file,
const char *const func)
355
{
356
357
358
359
360
361
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;
}
362
363
364
365
366
367
368
369
370
371
/* LL_EXTRA & LL_CRUCIAL are evaluated as WARNING level */
switch( loglevel ) {
case LL_EXTRA:
case LL_CRUCIAL:
loglevel = LL_WARN;;
break;
default:
break;
}
372
373
return logverbosity >= loglevel;
374
375
}
376
#endif /* OSSOLOG_COMPILE */