/
usb_moded-log.c
291 lines (260 loc) · 7.53 KB
/
usb_moded-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
/**
* @file usb_moded-log.c
*
* Copyright (C) 2010 Nokia Corporation. All rights reserved.
* Copyright (C) 2016-2018 Jolla Ltd.
*
* @author: Philippe De Swert <philippe.de-swert@nokia.com>
* @author: Simo Piiroinen <simo.piiroinen@nokia.com>
* @author: Philippe De Swert <phdeswer@lumi.maa>
* @author: Philippe De Swert <philippedeswert@gmail.com>
* @author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Lesser GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program 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 Lesser GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <errno.h>
#include <ctype.h>
#include "usb_moded-log.h"
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- log -- */
static char *log_strip (char *str);
static void log_gettime (struct timeval *tv);
void log_emit_va (const char *file, const char *func, int line, int lev, const char *fmt, va_list va);
void log_emit_real (const char *file, const char *func, int line, int lev, const char *fmt, ...);
void log_debugf (const char *fmt, ...);
int log_get_level (void);
void log_set_level (int lev);
bool log_p (int lev);
int log_get_type (void);
void log_set_type (int type);
const char *log_get_name (void);
void log_set_name (const char *name);
void log_set_lineinfo(bool lineinfo);
bool log_get_lineinfo(void);
void log_init (void);
/* ========================================================================= *
* Data
* ========================================================================= */
static const char *log_name = "<unset>";
static int log_level = LOG_WARNING;
static int log_type = LOG_TO_STDERR;
static bool log_lineinfo = false;
static struct timeval log_begtime = { 0, 0 };
/* ========================================================================= *
* Functions
* ========================================================================= */
static char *log_strip(char *str)
{
unsigned char *src = (unsigned char *)str;
unsigned char *dst = (unsigned char *)str;
while( *src > 0 && *src <= 32 ) ++src;
for( ;; )
{
while( *src > 32 ) *dst++ = *src++;
while( *src > 0 && *src <= 32 ) ++src;
if( *src == 0 ) break;
*dst++ = ' ';
}
*dst = 0;
return str;
}
static void log_gettime(struct timeval *tv)
{
gettimeofday(tv, 0);
timersub(tv, &log_begtime, tv);
}
/** Print the logged messages to the selected output
*
* @param file Source file name
* @param func Function name
* @param line Line in source file
* @param lev The wanted log level
* @param fmt The message format string
* @param va Arguments for the format string
*/
void log_emit_va(const char *file, const char *func, int line, int lev, const char *fmt, va_list va)
{
int saved = errno;
if( log_p(lev) )
{
switch( log_type )
{
case LOG_TO_SYSLOG:
vsyslog(lev, fmt, va);
break;
case LOG_TO_STDERR:
if( log_get_lineinfo() ) {
/* Use gcc error like prefix for logging so
* that logs can be analyzed with jump to
* line parsing available in editors. */
fprintf(stderr, "%s:%d: %s(): ", file, line, func);
}
else {
fprintf(stderr, "%s: ", log_get_name());
}
#if LOG_ENABLE_TIMESTAMPS
{
struct timeval tv;
log_gettime(&tv);
fprintf(stderr, "%3ld.%03ld ",
(long)tv.tv_sec,
(long)tv.tv_usec/1000);
}
#endif
#if LOG_ENABLE_LEVELTAGS
{
static const char *tag = "U:";
switch( lev )
{
case LOG_CRIT: tag = "C:"; break;
case LOG_ERR: tag = "E:"; break;
case LOG_WARNING: tag = "W:"; break;
case LOG_NOTICE: tag = "N:"; break;
case LOG_INFO: tag = "I:"; break;
case LOG_DEBUG: tag = "D:"; break;
}
fprintf(stderr, "%s ", tag);
}
#endif
{
// squeeze whitespace like syslog does
char buf[1024];
errno = saved;
vsnprintf(buf, sizeof buf - 1, fmt, va);
fprintf(stderr, "%s\n", log_strip(buf));
}
break;
default:
// no logging
break;
}
}
errno = saved;
}
/** Print the logged messages to the selected output
*
* @param file Source file name
* @param func Function name
* @param line Line in source file
* @param lev The wanted log level
* @param fmt The message format string
* @param ... Arguments for the format string
*/
void log_emit_real(const char *file, const char *func, int line, int lev, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
log_emit_va(file, func, line, lev, fmt, va);
va_end(va);
}
void log_debugf(const char *fmt, ...)
{
/* This goes always to stderr */
if( log_type == LOG_TO_STDERR && log_p(LOG_DEBUG) )
{
va_list va;
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
}
}
/** Get the currently set logging level
*
* @return The current logging level
*/
int log_get_level(void)
{
return log_level;
}
/** Set the logging level
*
* @param lev The wanted logging level
*/
void log_set_level(int lev)
{
log_level = lev;
}
/** Test if logging should be done at given level
*
* @param lev The logging level to query
*
* @return true if logging in the given level is allowed, false otherwise
*/
bool log_p(int lev)
{
return lev <= log_level;
}
/** Get the currently set logging type
*
* @return The current logging type
*/
int log_get_type(void)
{
return log_type;
}
/* Set the logging type
*
* @param type The wanted logging type
*/
void log_set_type(int type)
{
log_type = type;
}
/** Get the currently set logging name
*
* @return The current logging name
*/
const char *log_get_name(void)
{
return log_name;
}
/** Set the logging name
*
* @param name The wanted logging name
*/
void log_set_name(const char *name)
{
log_name = name;
}
/** Enable/disable the logging line info
*
* @param lineinfo true to enable line info, false to disable
*/
void log_set_lineinfo(bool lineinfo)
{
log_lineinfo = lineinfo;
}
/** Test if line info should be included in logging
*
* @return true when line info should be emitted, false otherwise
*/
bool log_get_lineinfo(void)
{
return log_lineinfo;
}
/** Initialize logging */
void log_init(void)
{
/* Get reference time used for verbose logging */
if( !timerisset(&log_begtime) )
gettimeofday(&log_begtime, 0);
}