/
usb_moded-log.c
176 lines (151 loc) · 4.42 KB
1
2
3
4
/**
@file usb_moded-log.c
Copyright (C) 2010 Nokia Corporation. All rights reserved.
5
Copyright (C) 2016 Jolla Ltd.
6
7
@author: Philippe De Swert <philippe.de-swert@nokia.com>
8
@author: Simo Piiroinen <simo.piiroinen@nokia.com>
9
10
@author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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>
32
33
#include <errno.h>
#include <ctype.h>
34
35
36
37
38
39
40
#include "usb_moded-log.h"
const char *log_name = "<unset>";
int log_level = LOG_WARNING;
int log_type = LOG_TO_STDERR;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
static char *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;
}
59
60
61
62
63
64
65
66
static struct timeval log_begtime = { 0, 0 };
static void log_gettime(struct timeval *tv)
{
gettimeofday(tv, 0);
timersub(tv, &log_begtime, tv);
}
67
68
69
70
71
72
73
74
75
/**
* Print the logged messages to the selected output
*
* @param lev The wanted log level
* @param fmt The message to be logged
* @param va The stdarg variable list
*/
void log_emit_va(int lev, const char *fmt, va_list va)
{
76
int saved = errno;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
if( log_level >= lev )
{
switch( log_type )
{
case LOG_TO_SYSLOG:
vsyslog(lev, fmt, va);
break;
case LOG_TO_STDERR:
fprintf(stderr, "%s: ", log_name);
#if LOG_ENABLE_TIMESTAMPS
{
struct timeval tv;
93
94
log_gettime(&tv);
fprintf(stderr, "%3ld.%03ld ",
95
(long)tv.tv_sec,
96
(long)tv.tv_usec/1000);
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
}
#endif
#if LOG_ENABLE_LEVELTAGS
{
static const char *tag = "U:";
switch( lev )
{
case 2: tag = "C:"; break;
case 3: tag = "E:"; break;
case 4: tag = "W:"; break;
case 5: tag = "N:"; break;
case 6: tag = "I:"; break;
case 7: tag = "D:"; break;
}
fprintf(stderr, "%s ", tag);
}
#endif
115
116
117
118
119
120
121
{
// squeeze whitespace like syslog does
char buf[1024];
errno = saved;
vsnprintf(buf, sizeof buf - 1, fmt, va);
fprintf(stderr, "%s\n", strip(buf));
}
122
123
124
125
126
127
128
break;
default:
// no logging
break;
}
}
129
errno = saved;
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
}
void log_emit(int lev, const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
log_emit_va(lev, fmt, va);
va_end(va);
}
void log_debugf(const char *fmt, ...)
{
/* This goes always to stderr */
if( log_type == LOG_TO_STDERR && log_level >= LOG_DEBUG )
{
va_list va;
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
}
}
/**
* returns the currently set log level
*
* @return The current log level
*/
156
inline int log_get_level(void)
157
158
159
160
161
162
163
164
{
return log_level;
}
/* Set the log level
*
* @param The wanted log level
*/
165
inline void log_set_level(int lev)
166
167
168
169
{
log_level = lev;
}
170
171
172
173
174
175
176
/** Initialize logging */
void log_init(void)
{
/* Get reference time used for verbose logging */
if( !timerisset(&log_begtime) )
gettimeofday(&log_begtime, 0);
}