Skip to content

Latest commit

 

History

History
376 lines (325 loc) · 8.71 KB

mce-log.c

File metadata and controls

376 lines (325 loc) · 8.71 KB
 
Dec 16, 2010
Dec 16, 2010
1
2
3
4
/**
* @file mce-log.c
* Logging functions for Mode Control Entity
* <p>
Jun 23, 2020
Jun 23, 2020
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.
Dec 16, 2010
Dec 16, 2010
8
9
* <p>
* @author David Weinehall <david.weinehall@nokia.com>
May 17, 2019
May 17, 2019
10
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
Dec 16, 2010
Dec 16, 2010
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/>.
*/
Jan 16, 2014
Jan 16, 2014
24
May 27, 2014
May 27, 2014
25
26
27
28
29
30
#ifdef OSSOLOG_COMPILE
#include "mce-log.h"
#include <sys/time.h>
Jan 16, 2014
Jan 16, 2014
31
32
#include <stdlib.h>
#include <stdbool.h>
May 27, 2014
May 27, 2014
33
#include <string.h>
Jan 16, 2014
Jan 16, 2014
34
35
#include <fnmatch.h>
Dec 16, 2010
Dec 16, 2010
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;
Aug 31, 2014
Aug 31, 2014
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";
}
Aug 26, 2013
Aug 26, 2013
51
52
53
54
/** Get monotonic time as struct timeval */
static void monotime(struct timeval *tv)
{
struct timespec ts;
Feb 11, 2014
Feb 11, 2014
55
clock_gettime(CLOCK_BOOTTIME, &ts);
Aug 26, 2013
Aug 26, 2013
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);
Feb 11, 2014
Feb 11, 2014
63
64
if( !timerisset(&start) )
prev = start = *tv;
Aug 26, 2013
Aug 26, 2013
65
timersub(tv, &prev, &diff);
Sep 3, 2013
Sep 3, 2013
66
if( diff.tv_sec >= 4 ) {
May 13, 2014
May 13, 2014
67
68
timersub(tv, &start, &diff);
fprintf(stderr, "%s: T+%ld.%03ld %s\n\n",
Aug 31, 2014
Aug 31, 2014
69
mce_log_name(),
May 13, 2014
May 13, 2014
70
71
(long)diff.tv_sec, (long)(diff.tv_usec/1000),
"END OF BURST");
Jun 23, 2020
Jun 23, 2020
72
fflush(stderr);
May 13, 2014
May 13, 2014
73
start = *tv;
Sep 3, 2013
Sep 3, 2013
74
}
Aug 26, 2013
Aug 26, 2013
75
76
77
78
prev = *tv;
timersub(tv, &start, tv);
}
Dec 10, 2012
Dec 10, 2012
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
*/
Oct 11, 2016
Oct 11, 2016
85
static loglevel_t mce_log_level_clip(loglevel_t loglevel)
Dec 10, 2012
Dec 10, 2012
86
{
Oct 11, 2016
Oct 11, 2016
87
88
if( loglevel <= LL_MINIMUM ) {
loglevel = LL_MINIMUM;
Dec 10, 2012
Dec 10, 2012
89
}
Oct 11, 2016
Oct 11, 2016
90
91
else if( loglevel > LL_MAXIMUM ) {
loglevel = LL_MAXIMUM;
Dec 10, 2012
Dec 10, 2012
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 ) {
Oct 11, 2016
Oct 11, 2016
106
case LL_CRUCIAL:res = "T"; break;
Jan 16, 2015
Jan 16, 2015
107
case LL_EXTRA: res = "X"; break;
May 13, 2014
May 13, 2014
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;
}
Dec 10, 2012
Dec 10, 2012
116
117
118
return res;
}
Jan 22, 2014
Jan 22, 2014
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;
}
Mar 8, 2019
Mar 8, 2019
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);
Jun 23, 2020
Jun 23, 2020
196
fflush(stderr);
Mar 8, 2019
Mar 8, 2019
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);
}
Dec 16, 2010
Dec 16, 2010
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
*/
Dec 10, 2012
Dec 10, 2012
235
void mce_log_file(loglevel_t loglevel, const char *const file,
Dec 16, 2010
Dec 16, 2010
236
237
const char *const function, const char *const fmt, ...)
{
Oct 11, 2016
Oct 11, 2016
238
loglevel = mce_log_level_clip(loglevel);
Dec 16, 2010
Dec 16, 2010
239
Jan 16, 2014
Jan 16, 2014
240
if( mce_log_p_(loglevel, file, function) ) {
Mar 8, 2019
Mar 8, 2019
241
242
243
244
va_list va;
va_start(va, fmt);
mce_log_unconditional_va(loglevel, file, function, fmt, va);
va_end(va);
Dec 16, 2010
Dec 16, 2010
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
*/
Oct 2, 2015
Oct 2, 2015
254
void mce_log_set_verbosity(int verbosity)
Dec 16, 2010
Dec 16, 2010
255
{
Oct 2, 2015
Oct 2, 2015
256
257
258
259
260
if( verbosity < LL_MINIMUM )
verbosity = LL_MINIMUM;
else if( verbosity > LL_MAXIMUM )
verbosity = LL_MAXIMUM;
Dec 16, 2010
Dec 16, 2010
261
262
263
logverbosity = verbosity;
}
Oct 2, 2015
Oct 2, 2015
264
265
266
267
268
269
270
271
272
/** Set log verbosity
*
* @return current verbosity level
*/
int mce_log_get_verbosity(void)
{
return logverbosity;
}
Dec 16, 2010
Dec 16, 2010
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;
Aug 31, 2014
Aug 31, 2014
283
logname = g_strdup(name);
Dec 16, 2010
Dec 16, 2010
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)
{
Aug 31, 2014
Aug 31, 2014
294
/* Logging (to stderr) after this will use default identity */
Dec 10, 2012
Dec 10, 2012
295
g_free(logname), logname = 0;
Dec 16, 2010
Dec 16, 2010
296
Aug 31, 2014
Aug 31, 2014
297
/* Any further syslog() calls will automatically reopen */
Dec 16, 2010
Dec 16, 2010
298
299
300
if (logtype == MCE_LOG_SYSLOG)
closelog();
}
Dec 7, 2012
Dec 7, 2012
301
Jan 16, 2014
Jan 16, 2014
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)
{
May 13, 2014
May 13, 2014
307
// NB these are never released by desing
Jan 16, 2014
Jan 16, 2014
308
May 13, 2014
May 13, 2014
309
mce_log_patterns = g_slist_prepend(mce_log_patterns, strdup(pat));
Jan 16, 2014
Jan 16, 2014
310
May 13, 2014
May 13, 2014
311
312
313
314
if( !mce_log_functions )
mce_log_functions = g_hash_table_new_full(g_str_hash,
g_str_equal,
free, 0);
Jan 16, 2014
Jan 16, 2014
315
316
317
318
}
static bool mce_log_check_pattern(const char *func)
{
May 13, 2014
May 13, 2014
319
gpointer hit = 0;
Jan 16, 2014
Jan 16, 2014
320
May 13, 2014
May 13, 2014
321
322
if( !mce_log_functions )
goto EXIT;
Jan 16, 2014
Jan 16, 2014
323
May 13, 2014
May 13, 2014
324
325
if( (hit = g_hash_table_lookup(mce_log_functions, func)) )
goto EXIT;
Jan 16, 2014
Jan 16, 2014
326
May 13, 2014
May 13, 2014
327
hit = GINT_TO_POINTER(1);
Jan 16, 2014
Jan 16, 2014
328
May 13, 2014
May 13, 2014
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);
Jan 16, 2014
Jan 16, 2014
337
338
EXIT:
May 13, 2014
May 13, 2014
339
return GPOINTER_TO_INT(hit) > 1;
Jan 16, 2014
Jan 16, 2014
340
341
}
Dec 7, 2012
Dec 7, 2012
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
*/
Oct 11, 2016
Oct 11, 2016
352
int mce_log_p_(loglevel_t loglevel,
Jan 16, 2014
Jan 16, 2014
353
354
const char *const file,
const char *const func)
Dec 7, 2012
Dec 7, 2012
355
{
May 13, 2014
May 13, 2014
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;
}
Jan 16, 2014
Jan 16, 2014
362
Oct 11, 2016
Oct 11, 2016
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;
}
Jan 16, 2014
Jan 16, 2014
372
May 13, 2014
May 13, 2014
373
return logverbosity >= loglevel;
Dec 7, 2012
Dec 7, 2012
374
375
}
Dec 16, 2010
Dec 16, 2010
376
#endif /* OSSOLOG_COMPILE */