1 /* 2 * Copyright (C) 2005-2017 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 _LIBS_LOG_LOG_MAIN_H 18 #define _LIBS_LOG_LOG_MAIN_H 19 20 #include <android/log.h> 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 /* 27 * Normally we strip the effects of ALOGV (VERBOSE messages), 28 * LOG_FATAL and LOG_FATAL_IF (FATAL assert messages) from the 29 * release builds be defining NDEBUG. You can modify this (for 30 * example with "#define LOG_NDEBUG 0" at the top of your source 31 * file) to change that behavior. 32 */ 33 34 #ifndef LOG_NDEBUG 35 #ifdef NDEBUG 36 #define LOG_NDEBUG 1 37 #else 38 #define LOG_NDEBUG 0 39 #endif 40 #endif 41 42 /* --------------------------------------------------------------------- */ 43 44 /* 45 * This file uses ", ## __VA_ARGS__" zero-argument token pasting to 46 * work around issues with debug-only syntax errors in assertions 47 * that are missing format strings. See commit 48 * 19299904343daf191267564fe32e6cd5c165cd42 49 */ 50 #if defined(__clang__) 51 #pragma clang diagnostic push 52 #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" 53 #endif 54 55 #ifndef __predict_false 56 #define __predict_false(exp) __builtin_expect((exp) != 0, 0) 57 #endif 58 59 #define android_writeLog(prio, tag, text) __android_log_write(prio, tag, text) 60 61 #define android_printLog(prio, tag, ...) \ 62 __android_log_print(prio, tag, __VA_ARGS__) 63 64 #define android_vprintLog(prio, cond, tag, ...) \ 65 __android_log_vprint(prio, tag, __VA_ARGS__) 66 67 /* 68 * Log macro that allows you to specify a number for the priority. 69 */ 70 #ifndef LOG_PRI 71 #define LOG_PRI(priority, tag, ...) android_printLog(priority, tag, __VA_ARGS__) 72 #endif 73 74 /* 75 * Log macro that allows you to pass in a varargs ("args" is a va_list). 76 */ 77 #ifndef LOG_PRI_VA 78 #define LOG_PRI_VA(priority, tag, fmt, args) \ 79 android_vprintLog(priority, NULL, tag, fmt, args) 80 #endif 81 82 /* --------------------------------------------------------------------- */ 83 84 /* XXX Macros to work around syntax errors in places where format string 85 * arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF 86 * (happens only in debug builds). 87 */ 88 89 /* Returns 2nd arg. Used to substitute default value if caller's vararg list 90 * is empty. 91 */ 92 #define __android_second(dummy, second, ...) second 93 94 /* If passed multiple args, returns ',' followed by all but 1st arg, otherwise 95 * returns nothing. 96 */ 97 #define __android_rest(first, ...) , ##__VA_ARGS__ 98 99 #define android_printAssert(cond, tag, ...) \ 100 __android_log_assert(cond, tag, \ 101 __android_second(0, ##__VA_ARGS__, NULL) \ 102 __android_rest(__VA_ARGS__)) 103 104 /* 105 * Log a fatal error. If the given condition fails, this stops program 106 * execution like a normal assertion, but also generating the given message. 107 * It is NOT stripped from release builds. Note that the condition test 108 * is -inverted- from the normal assert() semantics. 109 */ 110 #ifndef LOG_ALWAYS_FATAL_IF 111 #define LOG_ALWAYS_FATAL_IF(cond, ...) \ 112 ((__predict_false(cond)) \ 113 ? ((void)android_printAssert(#cond, LOG_TAG, ##__VA_ARGS__)) \ 114 : (void)0) 115 #endif 116 117 #ifndef LOG_ALWAYS_FATAL 118 #define LOG_ALWAYS_FATAL(...) \ 119 (((void)android_printAssert(NULL, LOG_TAG, ##__VA_ARGS__))) 120 #endif 121 122 /* 123 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that 124 * are stripped out of release builds. 125 */ 126 127 #if LOG_NDEBUG 128 129 #ifndef LOG_FATAL_IF 130 #define LOG_FATAL_IF(cond, ...) ((void)0) 131 #endif 132 #ifndef LOG_FATAL 133 #define LOG_FATAL(...) ((void)0) 134 #endif 135 136 #else 137 138 #ifndef LOG_FATAL_IF 139 #define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ##__VA_ARGS__) 140 #endif 141 #ifndef LOG_FATAL 142 #define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__) 143 #endif 144 145 #endif 146 147 /* 148 * Assertion that generates a log message when the assertion fails. 149 * Stripped out of release builds. Uses the current LOG_TAG. 150 */ 151 #ifndef ALOG_ASSERT 152 #define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ##__VA_ARGS__) 153 #endif 154 155 /* --------------------------------------------------------------------- */ 156 157 /* 158 * C/C++ logging functions. See the logging documentation for API details. 159 * 160 * We'd like these to be available from C code (in case we import some from 161 * somewhere), so this has a C interface. 162 * 163 * The output will be correct when the log file is shared between multiple 164 * threads and/or multiple processes so long as the operating system 165 * supports O_APPEND. These calls have mutex-protected data structures 166 * and so are NOT reentrant. Do not use LOG in a signal handler. 167 */ 168 169 /* --------------------------------------------------------------------- */ 170 171 /* 172 * Simplified macro to send a verbose log message using the current LOG_TAG. 173 */ 174 #ifndef ALOGV 175 #define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) 176 #if LOG_NDEBUG 177 #define ALOGV(...) \ 178 do { \ 179 if (0) { \ 180 __ALOGV(__VA_ARGS__); \ 181 } \ 182 } while (0) 183 #else 184 #define ALOGV(...) __ALOGV(__VA_ARGS__) 185 #endif 186 #endif 187 188 #ifndef ALOGV_IF 189 #if LOG_NDEBUG 190 #define ALOGV_IF(cond, ...) ((void)0) 191 #else 192 #define ALOGV_IF(cond, ...) \ 193 ((__predict_false(cond)) ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \ 194 : (void)0) 195 #endif 196 #endif 197 198 /* 199 * Simplified macro to send a debug log message using the current LOG_TAG. 200 */ 201 #ifndef ALOGD 202 #define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) 203 #endif 204 205 #ifndef ALOGD_IF 206 #define ALOGD_IF(cond, ...) \ 207 ((__predict_false(cond)) ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \ 208 : (void)0) 209 #endif 210 211 /* 212 * Simplified macro to send an info log message using the current LOG_TAG. 213 */ 214 #ifndef ALOGI 215 #define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) 216 #endif 217 218 #ifndef ALOGI_IF 219 #define ALOGI_IF(cond, ...) \ 220 ((__predict_false(cond)) ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \ 221 : (void)0) 222 #endif 223 224 /* 225 * Simplified macro to send a warning log message using the current LOG_TAG. 226 */ 227 #ifndef ALOGW 228 #define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) 229 #endif 230 231 #ifndef ALOGW_IF 232 #define ALOGW_IF(cond, ...) \ 233 ((__predict_false(cond)) ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \ 234 : (void)0) 235 #endif 236 237 /* 238 * Simplified macro to send an error log message using the current LOG_TAG. 239 */ 240 #ifndef ALOGE 241 #define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) 242 #endif 243 244 #ifndef ALOGE_IF 245 #define ALOGE_IF(cond, ...) \ 246 ((__predict_false(cond)) ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ 247 : (void)0) 248 #endif 249 250 /* --------------------------------------------------------------------- */ 251 252 /* 253 * Conditional based on whether the current LOG_TAG is enabled at 254 * verbose priority. 255 */ 256 #ifndef IF_ALOGV 257 #if LOG_NDEBUG 258 #define IF_ALOGV() if (false) 259 #else 260 #define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG) 261 #endif 262 #endif 263 264 /* 265 * Conditional based on whether the current LOG_TAG is enabled at 266 * debug priority. 267 */ 268 #ifndef IF_ALOGD 269 #define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG) 270 #endif 271 272 /* 273 * Conditional based on whether the current LOG_TAG is enabled at 274 * info priority. 275 */ 276 #ifndef IF_ALOGI 277 #define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG) 278 #endif 279 280 /* 281 * Conditional based on whether the current LOG_TAG is enabled at 282 * warn priority. 283 */ 284 #ifndef IF_ALOGW 285 #define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG) 286 #endif 287 288 /* 289 * Conditional based on whether the current LOG_TAG is enabled at 290 * error priority. 291 */ 292 #ifndef IF_ALOGE 293 #define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG) 294 #endif 295 296 /* --------------------------------------------------------------------- */ 297 298 /* 299 * Basic log message macro. 300 * 301 * Example: 302 * ALOG(LOG_WARN, NULL, "Failed with error %d", errno); 303 * 304 * The second argument may be NULL or "" to indicate the "global" tag. 305 */ 306 #ifndef ALOG 307 #define ALOG(priority, tag, ...) LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__) 308 #endif 309 310 /* 311 * Conditional given a desired logging priority and tag. 312 */ 313 #ifndef IF_ALOG 314 #define IF_ALOG(priority, tag) if (android_testLog(ANDROID_##priority, tag)) 315 #endif 316 317 /* --------------------------------------------------------------------- */ 318 319 /* 320 * IF_ALOG uses android_testLog, but IF_ALOG can be overridden. 321 * android_testLog will remain constant in its purpose as a wrapper 322 * for Android logging filter policy, and can be subject to 323 * change. It can be reused by the developers that override 324 * IF_ALOG as a convenient means to reimplement their policy 325 * over Android. 326 */ 327 328 #ifndef __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 329 #ifndef __ANDROID_API__ 330 #define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 2 331 #elif __ANDROID_API__ > 24 /* > Nougat */ 332 #define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 2 333 #elif __ANDROID_API__ > 22 /* > Lollipop */ 334 #define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 1 335 #else 336 #define __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 0 337 #endif 338 #endif 339 340 #if __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE 341 342 /* 343 * Use the per-tag properties "log.tag.<tagname>" to generate a runtime 344 * result of non-zero to expose a log. prio is ANDROID_LOG_VERBOSE to 345 * ANDROID_LOG_FATAL. default_prio if no property. Undefined behavior if 346 * any other value. 347 */ 348 int __android_log_is_loggable(int prio, const char* tag, int default_prio); 349 350 #if __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE > 1 351 #include <sys/types.h> 352 353 int __android_log_is_loggable_len(int prio, const char* tag, size_t len, 354 int default_prio); 355 356 #if LOG_NDEBUG /* Production */ 357 #define android_testLog(prio, tag) \ 358 (__android_log_is_loggable_len(prio, tag, (tag && *tag) ? strlen(tag) : 0, \ 359 ANDROID_LOG_DEBUG) != 0) 360 #else 361 #define android_testLog(prio, tag) \ 362 (__android_log_is_loggable_len(prio, tag, (tag && *tag) ? strlen(tag) : 0, \ 363 ANDROID_LOG_VERBOSE) != 0) 364 #endif 365 366 #else 367 368 #if LOG_NDEBUG /* Production */ 369 #define android_testLog(prio, tag) \ 370 (__android_log_is_loggable(prio, tag, ANDROID_LOG_DEBUG) != 0) 371 #else 372 #define android_testLog(prio, tag) \ 373 (__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE) != 0) 374 #endif 375 376 #endif 377 378 #else /* __ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE */ 379 380 #define android_testLog(prio, tag) (1) 381 382 #endif /* !__ANDROID_USE_LIBLOG_LOGGABLE_INTERFACE */ 383 384 #if defined(__clang__) 385 #pragma clang diagnostic pop 386 #endif 387 388 #ifdef __cplusplus 389 } 390 #endif 391 392 #endif /* _LIBS_LOG_LOG_MAIN_H */ 393