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