log.h revision b5fb6d0f18fed511256fd3aa07bb3eccbbb15f80
1/*
2 * Copyright (C) 2013 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#ifndef LOG_H_
18#define LOG_H_
19
20#include <android/log.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26// ---------------------------------------------------------------------
27
28/*
29 * Normally we strip ALOGV (VERBOSE messages) from release builds.
30 * You can modify this (for example with "#define LOG_NDEBUG 0"
31 * at the top of your source file) to change that behavior.
32 */
33#ifndef LOG_NDEBUG
34#ifdef NDEBUG
35#define LOG_NDEBUG 1
36#else
37#define LOG_NDEBUG 0
38#endif
39#endif
40
41/*
42 * This is the local tag used for the following simplified
43 * logging macros.  You can change this preprocessor definition
44 * before using the other macros to change the tag.
45 */
46#ifndef LOG_TAG
47#define LOG_TAG "RasterMill"
48#endif
49
50// ---------------------------------------------------------------------
51
52/*
53 * Simplified macro to send a verbose log message using the current LOG_TAG.
54 */
55#ifndef ALOGV
56#if LOG_NDEBUG
57#define ALOGV(...)   ((void)0)
58#else
59#define ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
60#endif
61#endif
62
63#define CONDITION(cond)     (__builtin_expect((cond)!=0, 0))
64
65#ifndef ALOGV_IF
66#if LOG_NDEBUG
67#define ALOGV_IF(cond, ...)   ((void)0)
68#else
69#define ALOGV_IF(cond, ...) \
70    ( (CONDITION(cond)) \
71    ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
72    : (void)0 )
73#endif
74#endif
75
76/*
77 * Simplified macro to send a debug log message using the current LOG_TAG.
78 */
79#ifndef ALOGD
80#define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
81#endif
82
83#ifndef ALOGD_IF
84#define ALOGD_IF(cond, ...) \
85    ( (CONDITION(cond)) \
86    ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
87    : (void)0 )
88#endif
89
90/*
91 * Simplified macro to send an info log message using the current LOG_TAG.
92 */
93#ifndef ALOGI
94#define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
95#endif
96
97#ifndef ALOGI_IF
98#define ALOGI_IF(cond, ...) \
99    ( (CONDITION(cond)) \
100    ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
101    : (void)0 )
102#endif
103
104/*
105 * Simplified macro to send a warning log message using the current LOG_TAG.
106 */
107#ifndef ALOGW
108#define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
109#endif
110
111#ifndef ALOGW_IF
112#define ALOGW_IF(cond, ...) \
113    ( (CONDITION(cond)) \
114    ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
115    : (void)0 )
116#endif
117
118/*
119 * Simplified macro to send an error log message using the current LOG_TAG.
120 */
121#ifndef ALOGE
122#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
123#endif
124
125#ifndef ALOGE_IF
126#define ALOGE_IF(cond, ...) \
127    ( (CONDITION(cond)) \
128    ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
129    : (void)0 )
130#endif
131
132// ---------------------------------------------------------------------
133
134/*
135 * Conditional based on whether the current LOG_TAG is enabled at
136 * verbose priority.
137 */
138#ifndef IF_ALOGV
139#if LOG_NDEBUG
140#define IF_ALOGV() if (false)
141#else
142#define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)
143#endif
144#endif
145
146/*
147 * Conditional based on whether the current LOG_TAG is enabled at
148 * debug priority.
149 */
150#ifndef IF_ALOGD
151#define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)
152#endif
153
154/*
155 * Conditional based on whether the current LOG_TAG is enabled at
156 * info priority.
157 */
158#ifndef IF_ALOGI
159#define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)
160#endif
161
162/*
163 * Conditional based on whether the current LOG_TAG is enabled at
164 * warn priority.
165 */
166#ifndef IF_ALOGW
167#define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)
168#endif
169
170/*
171 * Conditional based on whether the current LOG_TAG is enabled at
172 * error priority.
173 */
174#ifndef IF_ALOGE
175#define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)
176#endif
177
178// ---------------------------------------------------------------------
179
180/*
181 * Log a fatal error.  If the given condition fails, this stops program
182 * execution like a normal assertion, but also generating the given message.
183 * It is NOT stripped from release builds.  Note that the condition test
184 * is -inverted- from the normal assert() semantics.
185 */
186#ifndef LOG_ALWAYS_FATAL_IF
187#define LOG_ALWAYS_FATAL_IF(cond, ...) \
188    ( (CONDITION(cond)) \
189    ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \
190    : (void)0 )
191#endif
192
193#ifndef LOG_ALWAYS_FATAL
194#define LOG_ALWAYS_FATAL(...) \
195    ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) )
196#endif
197
198/*
199 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that
200 * are stripped out of release builds.
201 */
202#if LOG_NDEBUG
203
204#ifndef LOG_FATAL_IF
205#define LOG_FATAL_IF(cond, ...) ((void)0)
206#endif
207#ifndef LOG_FATAL
208#define LOG_FATAL(...) ((void)0)
209#endif
210
211#else
212
213#ifndef LOG_FATAL_IF
214#define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__)
215#endif
216#ifndef LOG_FATAL
217#define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
218#endif
219
220#endif
221
222/*
223 * Assertion that generates a log message when the assertion fails.
224 * Stripped out of release builds.  Uses the current LOG_TAG.
225 */
226#ifndef ALOG_ASSERT
227#define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__)
228//#define ALOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond)
229#endif
230
231// ---------------------------------------------------------------------
232
233/*
234 * Basic log message macro.
235 *
236 * Example:
237 *  ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
238 *
239 * The second argument may be NULL or "" to indicate the "global" tag.
240 */
241#ifndef ALOG
242#define ALOG(priority, tag, ...) \
243    LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
244#endif
245
246/*
247 * Log macro that allows you to specify a number for the priority.
248 */
249#ifndef LOG_PRI
250#define LOG_PRI(priority, tag, ...) \
251    __android_log_print(priority, tag, __VA_ARGS__)
252#endif
253
254/*
255 * Log macro that allows you to pass in a varargs ("args" is a va_list).
256 */
257#ifndef LOG_PRI_VA
258#define LOG_PRI_VA(priority, tag, fmt, args) \
259    __android_log_vprint(priority, NULL, tag, fmt, args)
260#endif
261
262/*
263 * Conditional given a desired logging priority and tag.
264 */
265#ifndef IF_ALOG
266#define IF_ALOG(priority, tag) \
267    if (__android_log_assert(ANDROID_##priority, tag))
268#endif
269
270/* Returns 2nd arg.  Used to substitute default value if caller's vararg list
271 * is empty.
272 */
273#define __android_second(dummy, second, ...)     second
274
275/* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
276 * returns nothing.
277 */
278#define __android_rest(first, ...)               , ## __VA_ARGS__
279
280#define android_printAssert(cond, tag, fmt...) \
281    __android_log_assert(cond, tag, \
282        __android_second(0, ## fmt, NULL) __android_rest(fmt))
283
284#ifdef __cplusplus
285}
286#endif
287
288#endif /* LOG_H_ */
289