23683136f719a56fa81012da3f5b11fbd5d667c5
[framework/system/dlog.git] / include / dlog.h
1 /*
2  * Copyright (C) 2005 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16  
17
18 /** 
19  * @file        dlog.h
20  * @version     0.4
21  * @brief       This file is the header file of interface of dlog.
22  */
23
24
25 /**
26  * @addtogroup APPLICATION_FRAMEWORK
27  * @{
28  *
29  * @defgroup dlog dlog
30  * @addtogroup dlog
31  * @{
32
33  */
34
35
36 #ifndef _DLOG_H_
37 #define _DLOG_H_
38
39 #include<stdarg.h>
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif /* __cplusplus */
44
45 /*
46  * Normally we strip LOGV (VERBOSE messages) from release builds.
47  */
48 #ifndef LOG_NDEBUG
49 #ifdef NDEBUG
50 #define LOG_NDEBUG 1
51 #else
52 #define LOG_NDEBUG 0
53 #endif
54 #endif
55
56 /*
57  * This is the local tag used for the following simplified
58  * logging macros.  You can change this preprocessor definition
59  * before using the other macros to change the tag.
60  */
61 #ifndef LOG_TAG
62 #define LOG_TAG NULL
63 #endif
64
65 /*
66  * log priority values, in ascending priority order.
67  */
68 typedef enum {
69         DLOG_UNKNOWN = 0,
70         DLOG_DEFAULT,
71         DLOG_VERBOSE,
72         DLOG_DEBUG,
73         DLOG_INFO,
74         DLOG_WARN,
75         DLOG_ERROR,
76         DLOG_FATAL,
77         DLOG_SILENT,
78 } log_priority;
79
80 typedef enum {
81     LOG_ID_MAIN = 0,
82     LOG_ID_RADIO,
83     LOG_ID_SYSTEM,
84     LOG_ID_MAX
85 } log_id_t;
86
87 #define CONDITION(cond)     (__builtin_expect((cond)!=0, 0))
88
89 // ---------------------------------------------------------------------
90
91 /**
92  * Simplified macro to send a verbose log message using the current LOG_TAG.
93  */
94 #ifndef LOGV
95 #if LOG_NDEBUG
96 #define LOGV(...)   ((void)0)
97 #else
98 #define LOGV(...) ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
99
100 #endif
101 #endif
102
103 /**
104  * Simplified macro to send a conditional verbose log message using the current LOG_TAG.
105  */
106 #ifndef LOGV_IF
107 #if LOG_NDEBUG
108 #define LOGV_IF(cond, ...)   ((void)0)
109 #else
110 #define LOGV_IF(cond, ...) \
111     ( (CONDITION(cond)) \
112     ? ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
113     : (void)0 )
114 #endif
115 #endif
116
117 /**
118  * Simplified macro to send a debug log message using the current LOG_TAG.
119  */
120 #ifndef LOGD
121 #define LOGD(...) ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
122 #endif
123
124 /**
125  * Simplified macro to send a conditional debug log message using the current LOG_TAG.
126  */
127 #ifndef LOGD_IF
128 #define LOGD_IF(cond, ...) \
129     ( (CONDITION(cond)) \
130     ? ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
131     : (void)0 )
132 #endif
133
134 /**
135  * Simplified macro to send an info log message using the current LOG_TAG.
136  */
137 #ifndef LOGI
138 #define LOGI(...) ((void)LOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
139 #endif
140
141 /**
142  * Simplified macro to send a conditional info log message using the current LOG_TAG.
143  */
144 #ifndef LOGI_IF
145 #define LOGI_IF(cond, ...) \
146     ( (CONDITION(cond)) \
147     ? ((void)LOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
148     : (void)0 )
149 #endif
150
151 /**
152  * Simplified macro to send a warning log message using the current LOG_TAG.
153  */
154 #ifndef LOGW
155 #define LOGW(...) ((void)LOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
156 #endif
157
158 /**
159  * Simplified macro to send a conditional warning log message using the current LOG_TAG.
160  */
161 #ifndef LOGW_IF
162 #define LOGW_IF(cond, ...) \
163     ( (CONDITION(cond)) \
164     ? ((void)LOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
165     : (void)0 )
166 #endif
167
168 /**
169  * Simplified macro to send an error log message using the current LOG_TAG.
170  */
171 #ifndef LOGE
172 #define LOGE(...) ((void)LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
173 #endif
174
175 /**
176  * Simplified macro to send a conditional error log message using the current LOG_TAG.
177  */
178 #ifndef LOGE_IF
179 #define LOGE_IF(cond, ...) \
180     ( (CONDITION(cond)) \
181     ? ((void)LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
182     : (void)0 )
183 #endif
184
185 // ---------------------------------------------------------------------
186
187 /**
188  * Simplified macro to send a verbose radio log message using the current LOG_TAG.
189  */
190 #ifndef RLOGV
191 #if LOG_NDEBUG
192 #define RLOGV(...)   ((void)0)
193 #else
194 #define RLOGV(...) ((void)RLOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
195 #endif
196 #endif
197         
198 /**
199  * Simplified macro to send a conditional verbose radio log message using the current LOG_TAG.
200  */     
201 #ifndef RLOGV_IF
202 #if LOG_NDEBUG
203 #define RLOGV_IF(cond, ...)   ((void)0)
204 #else
205 #define RLOGV_IF(cond, ...) \
206                 ( (CONDITION(cond)) \
207                 ? ((void)RLOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
208                 : (void)0 )
209 #endif
210 #endif
211         
212 /**
213  * Simplified macro to send a debug radio log message using the current LOG_TAG.
214  */
215 #ifndef RLOGD
216 #define RLOGD(...) ((void)RLOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
217 #endif
218
219 /**
220  * Simplified macro to send a conditional debug radio log message using the current LOG_TAG.
221  */     
222 #ifndef RLOGD_IF
223 #define RLOGD_IF(cond, ...) \
224                 ( (CONDITION(cond)) \
225                 ? ((void)RLOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
226                 : (void)0 )
227 #endif
228         
229 /**
230  * Simplified macro to send an info radio log message using the current LOG_TAG.
231  */
232 #ifndef RLOGI
233 #define RLOGI(...) ((void)RLOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
234 #endif
235
236 /**
237  * Simplified macro to send a conditional info radio log message using the current LOG_TAG.
238  */
239 #ifndef RLOGI_IF
240 #define RLOGI_IF(cond, ...) \
241                 ( (CONDITION(cond)) \
242                 ? ((void)RLOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
243                 : (void)0 )
244 #endif
245         
246 /**
247  * Simplified macro to send a warning radio log message using the current LOG_TAG.
248  */
249 #ifndef RLOGW
250 #define RLOGW(...) ((void)RLOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
251 #endif
252
253 /**
254  * Simplified macro to send a conditional warning radio log message using the current LOG_TAG.
255  */
256 #ifndef RLOGW_IF
257 #define RLOGW_IF(cond, ...) \
258                 ( (CONDITION(cond)) \
259                 ? ((void)RLOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
260                 : (void)0 )
261 #endif
262         
263 /**
264  * Simplified macro to send an error radio log message using the current LOG_TAG.
265  */
266 #ifndef RLOGE
267 #define RLOGE(...) ((void)RLOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
268 #endif
269
270 /**
271  * Simplified macro to send a conditional error radio log message using the current LOG_TAG.
272  */     
273 #ifndef RLOGE_IF
274 #define RLOGE_IF(cond, ...) \
275                 ( (CONDITION(cond)) \
276                 ? ((void)RLOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
277                 : (void)0 )
278 #endif
279
280 // ---------------------------------------------------------------------
281
282 /**
283  * Simplified macro to send a verbose system log message using the current LOG_TAG.
284  */
285 #ifndef SLOGV
286 #if LOG_NDEBUG
287 #define SLOGV(...)   ((void)0)
288 #else
289 #define SLOGV(...) ((void)SLOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
290 #endif
291 #endif
292
293 /**
294  * Simplified macro to send a conditional verbose system log message using the current LOG_TAG.
295  */
296 #ifndef SLOGV_IF
297 #if LOG_NDEBUG
298 #define SLOGV_IF(cond, ...)   ((void)0)
299 #else
300 #define SLOGV_IF(cond, ...) \
301                 ( (CONDITION(cond)) \
302                 ? ((void)SLOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
303                 : (void)0 )
304 #endif
305 #endif
306         
307 /**
308  * Simplified macro to send a debug system log message using the current LOG_TAG.
309  */
310 #ifndef SLOGD
311 #define SLOGD(...) ((void)SLOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
312 #endif
313
314 /**
315  * Simplified macro to send a conditional debug system log message using the current LOG_TAG.
316  */
317 #ifndef SLOGD_IF
318 #define SLOGD_IF(cond, ...) \
319                 ( (CONDITION(cond)) \
320                 ? ((void)SLOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
321                 : (void)0 )
322 #endif
323         
324 /**
325  * Simplified macro to send an info system log message using the current LOG_TAG.
326  */
327 #ifndef SLOGI
328 #define SLOGI(...) ((void)SLOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
329 #endif
330
331 /**
332  * Simplified macro to send a conditional info system log message using the current LOG_TAG.
333  */
334 #ifndef SLOGI_IF
335 #define SLOGI_IF(cond, ...) \
336                 ( (CONDITION(cond)) \
337                 ? ((void)SLOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
338                 : (void)0 )
339 #endif
340         
341 /**
342  * Simplified macro to send a warning system log message using the current LOG_TAG.
343  */
344 #ifndef SLOGW
345 #define SLOGW(...) ((void)SLOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
346 #endif
347
348 /**
349  * Simplified macro to send a conditional warning system log message using the current LOG_TAG.
350  */
351 #ifndef SLOGW_IF
352 #define SLOGW_IF(cond, ...) \
353                 ( (CONDITION(cond)) \
354                 ? ((void)SLOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
355                 : (void)0 )
356 #endif
357         
358 /**
359  * Simplified macro to send an error system log message using the current LOG_TAG.
360  */
361 #ifndef SLOGE
362 #define SLOGE(...) ((void)SLOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
363 #endif
364
365 /**
366  * Simplified macro to send a conditional error system log message using the current LOG_TAG.
367  */
368 #ifndef SLOGE_IF
369 #define SLOGE_IF(cond, ...) \
370                 ( (CONDITION(cond)) \
371                 ? ((void)SLOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
372                 : (void)0 )
373 #endif
374
375 // TODO : support fatal log
376
377 // ---------------------------------------------------------------------
378
379 /**
380  * Basic log message macro that allows you to specify a priority and a tag
381  *
382  * Example:
383  *  LOG(DLOG_WARN, NULL, "Failed with error %d", errno);
384  *
385  * The second argument may be NULL or "" to indicate the "global" tag.
386  * 
387  * Future work : we want to put filename and line number automatically only when debug build mode 
388  */
389 #ifndef LOG
390 #define LOG(priority, tag, ...) \
391         print_log(D##priority, tag, __VA_ARGS__)
392 #endif
393
394 /**
395  * Log macro that allows you to pass in a varargs ("args" is a va_list).
396  */
397 #ifndef LOG_VA
398 #define LOG_VA(priority, tag, fmt, args) \
399     vprint_log(D##priority, tag, fmt, args)
400 #endif
401
402 /**
403  * Basic radio log macro that allows you to specify a priority and a tag.
404  */
405 #ifndef RLOG
406 #define RLOG(priority, tag, ...) \
407         print_radio_log(D##priority, tag, __VA_ARGS__)
408 #endif
409
410 /**
411  * Radio log macro that allows you to pass in a varargs ("args" is a va_list).
412  */
413 #ifndef RLOG_VA
414 #define RLOG_VA(priority, tag, fmt, args) \
415     vprint_radio_log(D##priority, tag, fmt, args)
416 #endif
417
418 /**
419  * Basic system log macro that allows you to specify a priority and a tag.
420  */
421 #ifndef SLOG
422 #define SLOG(priority, tag, ...) \
423     print_system_log(D##priority, tag, __VA_ARGS__)
424 #endif
425
426 /**
427  * System log macro that allows you to pass in a varargs ("args" is a va_list).
428  */
429 #ifndef SLOG_VA
430 #define SLOG_VA(priority, tag, fmt, args) \
431     vprint_system_log(D##priority, tag, fmt, args)
432 #endif
433
434
435 /*
436  * ===========================================================================
437  *
438  * The stuff in the rest of this file should not be used directly.
439  */
440
441 #define print_log(prio, tag, fmt...) \
442         __dlog_print(LOG_ID_MAIN, prio, tag, fmt)
443
444 #define vprint_log(prio, tag, fmt...) \
445         __dlog_vprint(LOG_ID_MAIN, prio, tag, fmt)
446         
447 #define print_radio_log(prio, tag, fmt...)\
448         __dlog_print(LOG_ID_RADIO, prio, tag, fmt)
449
450 #define vprint_radio_log(prio, tag, fmt...) \
451         __dlog_vprint(LOG_ID_RADIO, prio, tag, fmt)
452
453 #define print_system_log(prio, tag, fmt...)\
454         __dlog_print(LOG_ID_SYSTEM, prio, tag, fmt)
455
456 #define vprint_system_log(prio, tag, fmt...) \
457         __dlog_vprint(LOG_ID_SYSTEM, prio, tag, fmt)
458
459 /**
460  * @brief               send log. must specify log_id ,priority, tag and format string.
461  * @pre         none
462  * @post                none
463  * @see         __dlog_vprint
464  * @remarks     you must not use this API directly. use macros instead.
465  * @param[in]   log_id  log device id
466  * @param[in]   prio    priority
467  * @param[in]   tag     tag
468  * @param[in]   fmt     format string 
469  * @return                      Operation result
470  * @retval              0>=     Success
471  * @retval              -1      Error
472  * @code
473 // you have to use LOG(), SLOG(), RLOG() family not to use __dlog_print() directly
474 // so below example is just for passing Documentation Verification !!!
475 #define LOG_TAG USR_TAG
476 #include<dlog.h>
477  __dlog_print(LOG_ID_MAIN, DLOG_INFO, USR_TAG, "you must not use this API directly");
478  * @endcode
479  */
480 int __dlog_print(log_id_t log_id, int prio, const char *tag, const char *fmt, ...);
481
482 /**
483  * @brief               send log with va_list. must specify log_id ,priority, tag and format string.
484  * @pre         none
485  * @post                none
486  * @see         __dlog_print 
487  * @remarks     you must not use this API directly. use macros instead. 
488  * @param[in]   log_id  log device id
489  * @param[in]   prio    priority
490  * @param[in]   tag     tag
491  * @param[in]   fmt     format string 
492  * @param[in]   ap      va_list 
493  * @return                      Operation result
494  * @retval              0>=     Success
495  * @retval              -1      Error
496  * @code
497  // you have to use LOG_VA(), SLOG_VA(), RLOG_VA() family not to use __dlog_print() directly
498  // so below example is just for passing Documentation Verification !!!
499 #define LOG_TAG USR_TAG
500 #include<dlog.h>
501   __dlog_vprint(LOG_ID_MAIN, DLOG_INFO, USR_TAG, "you must not use this API directly", ap);
502  * @endcode
503   */
504 int __dlog_vprint(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap);
505
506 #ifdef __cplusplus
507 }
508 #endif /* __cplusplus */
509
510
511 /** @} */
512 /** @} */
513
514 #endif /* _DLOG_H_*/
515