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
|
/*
Copyright (C) 1995-97 Simon G. Vogl
Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
Copyright (C) 2010 InvenSense Corporation, All Rights Reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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 GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* C/C++ logging functions. See the logging documentation for API details.
*
* We'd like these to be available from C code (in case we import some from
* somewhere), so this has a C interface.
*
* The output will be correct when the log file is shared between multiple
* threads and/or multiple processes so long as the operating system
* supports O_APPEND. These calls have mutex-protected data structures
* and so are NOT reentrant. Do not use MPL_LOG in a signal handler.
*/
#ifndef _LIBS_CUTILS_MPL_LOG_H
#define _LIBS_CUTILS_MPL_LOG_H
#include <stdarg.h>
#ifdef ANDROID
#include <utils/Log.h> /* For the LOG macro */
#endif
#ifdef __KERNEL__
#include <linux/kernel.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
/* --------------------------------------------------------------------- */
/*
* Normally we strip MPL_LOGV (VERBOSE messages) from release builds.
* You can modify this (for example with "#define MPL_LOG_NDEBUG 0"
* at the top of your source file) to change that behavior.
*/
#ifndef MPL_LOG_NDEBUG
#ifdef NDEBUG
#define MPL_LOG_NDEBUG 1
#else
#define MPL_LOG_NDEBUG 0
#endif
#endif
#ifdef __KERNEL__
#define MPL_LOG_UNKNOWN MPL_LOG_VERBOSE
#define MPL_LOG_DEFAULT KERN_DEFAULT
#define MPL_LOG_VERBOSE KERN_CONT
#define MPL_LOG_DEBUG KERN_NOTICE
#define MPL_LOG_INFO KERN_INFO
#define MPL_LOG_WARN KERN_WARNING
#define MPL_LOG_ERROR KERN_ERR
#define MPL_LOG_SILENT MPL_LOG_VERBOSE
#else
/* Based off the log priorities in android
/system/core/include/android/log.h */
#define MPL_LOG_UNKNOWN (0)
#define MPL_LOG_DEFAULT (1)
#define MPL_LOG_VERBOSE (2)
#define MPL_LOG_DEBUG (3)
#define MPL_LOG_INFO (4)
#define MPL_LOG_WARN (5)
#define MPL_LOG_ERROR (6)
#define MPL_LOG_SILENT (8)
#endif
/*
* This is the local tag used for the following simplified
* logging macros. You can change this preprocessor definition
* before using the other macros to change the tag.
*/
#ifndef MPL_LOG_TAG
#ifdef __KERNEL__
#define MPL_LOG_TAG
#else
#define MPL_LOG_TAG NULL
#endif
#endif
/* --------------------------------------------------------------------- */
/*
* Simplified macro to send a verbose log message using the current MPL_LOG_TAG.
*/
#ifndef MPL_LOGV
#if MPL_LOG_NDEBUG
#define MPL_LOGV(...) ((void)0)
#else
#define MPL_LOGV(...) ((void)MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, __VA_ARGS__))
#endif
#endif
#ifndef CONDITION
#define CONDITION(cond) ((cond) != 0)
#endif
#ifndef MPL_LOGV_IF
#if MPL_LOG_NDEBUG
#define MPL_LOGV_IF(cond, ...) ((void)0)
#else
#define MPL_LOGV_IF(cond, ...) \
((CONDITION(cond)) \
? ((void)MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, __VA_ARGS__)) \
: (void)0)
#endif
#endif
/*
* Simplified macro to send a debug log message using the current MPL_LOG_TAG.
*/
#ifndef MPL_LOGD
#define MPL_LOGD(...) ((void)MPL_LOG(LOG_DEBUG, MPL_LOG_TAG, __VA_ARGS__))
#endif
#ifndef MPL_LOGD_IF
#define MPL_LOGD_IF(cond, ...) \
((CONDITION(cond)) \
? ((void)MPL_LOG(LOG_DEBUG, MPL_LOG_TAG, __VA_ARGS__)) \
: (void)0)
#endif
/*
* Simplified macro to send an info log message using the current MPL_LOG_TAG.
*/
#ifndef MPL_LOGI
#define MPL_LOGI(...) ((void)MPL_LOG(LOG_INFO, MPL_LOG_TAG, __VA_ARGS__))
#endif
#ifndef MPL_LOGI_IF
#define MPL_LOGI_IF(cond, ...) \
((CONDITION(cond)) \
? ((void)MPL_LOG(LOG_INFO, MPL_LOG_TAG, __VA_ARGS__)) \
: (void)0)
#endif
/*
* Simplified macro to send a warning log message using the current MPL_LOG_TAG.
*/
#ifndef MPL_LOGW
#define MPL_LOGW(...) ((void)MPL_LOG(LOG_WARN, MPL_LOG_TAG, __VA_ARGS__))
#endif
#ifndef MPL_LOGW_IF
#define MPL_LOGW_IF(cond, ...) \
((CONDITION(cond)) \
? ((void)MPL_LOG(LOG_WARN, MPL_LOG_TAG, __VA_ARGS__)) \
: (void)0)
#endif
/*
* Simplified macro to send an error log message using the current MPL_LOG_TAG.
*/
#ifndef MPL_LOGE
#define MPL_LOGE(...) ((void)MPL_LOG(LOG_ERROR, MPL_LOG_TAG, __VA_ARGS__))
#endif
#ifndef MPL_LOGE_IF
#define MPL_LOGE_IF(cond, ...) \
((CONDITION(cond)) \
? ((void)MPL_LOG(LOG_ERROR, MPL_LOG_TAG, __VA_ARGS__)) \
: (void)0)
#endif
/* --------------------------------------------------------------------- */
/*
* Log a fatal error. If the given condition fails, this stops program
* execution like a normal assertion, but also generating the given message.
* It is NOT stripped from release builds. Note that the condition test
* is -inverted- from the normal assert() semantics.
*/
#define MPL_LOG_ALWAYS_FATAL_IF(cond, ...) \
((CONDITION(cond)) \
? ((void)android_printAssert(#cond, MPL_LOG_TAG, __VA_ARGS__)) \
: (void)0)
#define MPL_LOG_ALWAYS_FATAL(...) \
(((void)android_printAssert(NULL, MPL_LOG_TAG, __VA_ARGS__)))
/*
* Versions of MPL_LOG_ALWAYS_FATAL_IF and MPL_LOG_ALWAYS_FATAL that
* are stripped out of release builds.
*/
#if MPL_LOG_NDEBUG
#define MPL_LOG_FATAL_IF(cond, ...) ((void)0)
#define MPL_LOG_FATAL(...) ((void)0)
#else
#define MPL_LOG_FATAL_IF(cond, ...) MPL_LOG_ALWAYS_FATAL_IF(cond, __VA_ARGS__)
#define MPL_LOG_FATAL(...) MPL_LOG_ALWAYS_FATAL(__VA_ARGS__)
#endif
/*
* Assertion that generates a log message when the assertion fails.
* Stripped out of release builds. Uses the current MPL_LOG_TAG.
*/
#define MPL_LOG_ASSERT(cond, ...) MPL_LOG_FATAL_IF(!(cond), __VA_ARGS__)
/* --------------------------------------------------------------------- */
/*
* Basic log message macro.
*
* Example:
* MPL_LOG(MPL_LOG_WARN, NULL, "Failed with error %d", errno);
*
* The second argument may be NULL or "" to indicate the "global" tag.
*/
#ifndef MPL_LOG
#define MPL_LOG(priority, tag, ...) \
MPL_LOG_PRI(priority, tag, __VA_ARGS__)
#endif
/*
* Log macro that allows you to specify a number for the priority.
*/
#ifndef MPL_LOG_PRI
#ifdef ANDROID
#define MPL_LOG_PRI(priority, tag, ...) \
LOG(priority, tag, __VA_ARGS__)
#elif defined __KERNEL__
#define MPL_LOG_PRI(priority, tag, ...) \
printk(MPL_##priority tag __VA_ARGS__)
#else
#define MPL_LOG_PRI(priority, tag, ...) \
_MLPrintLog(MPL_##priority, tag, __VA_ARGS__)
#endif
#endif
/*
* Log macro that allows you to pass in a varargs ("args" is a va_list).
*/
#ifndef MPL_LOG_PRI_VA
#ifdef ANDROID
#define MPL_LOG_PRI_VA(priority, tag, fmt, args) \
android_vprintLog(priority, NULL, tag, fmt, args)
#elif defined __KERNEL__
#define MPL_LOG_PRI_VA(priority, tag, fmt, args) \
vprintk(MPL_##priority tag fmt, args)
#else
#define MPL_LOG_PRI_VA(priority, tag, fmt, args) \
_MLPrintVaLog(priority, NULL, tag, fmt, args)
#endif
#endif
/* --------------------------------------------------------------------- */
/*
* ===========================================================================
*
* The stuff in the rest of this file should not be used directly.
*/
#ifndef ANDROID
int _MLPrintLog(int priority, const char *tag, const char *fmt,
...);
int _MLPrintVaLog(int priority, const char *tag, const char *fmt,
va_list args);
/* Final implementation of actual writing to a character device */
int _MLWriteLog(const char *buf, int buflen);
#endif
#ifdef __cplusplus
}
#endif
#endif /* _LIBS_CUTILS_MPL_LOG_H */
|