17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2005-2017 The Android Open Source Project
37ec681f3Smrg *
47ec681f3Smrg * Licensed under the Apache License, Version 2.0 (the "License");
57ec681f3Smrg * you may not use this file except in compliance with the License.
67ec681f3Smrg * You may obtain a copy of the License at
77ec681f3Smrg *
87ec681f3Smrg *      http://www.apache.org/licenses/LICENSE-2.0
97ec681f3Smrg *
107ec681f3Smrg * Unless required by applicable law or agreed to in writing, software
117ec681f3Smrg * distributed under the License is distributed on an "AS IS" BASIS,
127ec681f3Smrg * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137ec681f3Smrg * See the License for the specific language governing permissions and
147ec681f3Smrg * limitations under the License.
157ec681f3Smrg */
167ec681f3Smrg
177ec681f3Smrg#pragma once
187ec681f3Smrg
197ec681f3Smrg#include <stdbool.h>
207ec681f3Smrg#include <sys/cdefs.h>
217ec681f3Smrg#include <sys/types.h>
227ec681f3Smrg
237ec681f3Smrg#include <android/log.h>
247ec681f3Smrg
257ec681f3Smrg__BEGIN_DECLS
267ec681f3Smrg
277ec681f3Smrg/*
287ec681f3Smrg * Normally we strip the effects of ALOGV (VERBOSE messages),
297ec681f3Smrg * LOG_FATAL and LOG_FATAL_IF (FATAL assert messages) from the
307ec681f3Smrg * release builds be defining NDEBUG.  You can modify this (for
317ec681f3Smrg * example with "#define LOG_NDEBUG 0" at the top of your source
327ec681f3Smrg * file) to change that behavior.
337ec681f3Smrg */
347ec681f3Smrg
357ec681f3Smrg#ifndef LOG_NDEBUG
367ec681f3Smrg#ifdef NDEBUG
377ec681f3Smrg#define LOG_NDEBUG 1
387ec681f3Smrg#else
397ec681f3Smrg#define LOG_NDEBUG 0
407ec681f3Smrg#endif
417ec681f3Smrg#endif
427ec681f3Smrg
437ec681f3Smrg/* --------------------------------------------------------------------- */
447ec681f3Smrg
457ec681f3Smrg/*
467ec681f3Smrg * This file uses ", ## __VA_ARGS__" zero-argument token pasting to
477ec681f3Smrg * work around issues with debug-only syntax errors in assertions
487ec681f3Smrg * that are missing format strings.  See commit
497ec681f3Smrg * 19299904343daf191267564fe32e6cd5c165cd42
507ec681f3Smrg */
517ec681f3Smrg#if defined(__clang__)
527ec681f3Smrg#pragma clang diagnostic push
537ec681f3Smrg#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
547ec681f3Smrg#endif
557ec681f3Smrg
567ec681f3Smrg/*
577ec681f3Smrg * Use __VA_ARGS__ if running a static analyzer,
587ec681f3Smrg * to avoid warnings of unused variables in __VA_ARGS__.
597ec681f3Smrg * Use constexpr function in C++ mode, so these macros can be used
607ec681f3Smrg * in other constexpr functions without warning.
617ec681f3Smrg */
627ec681f3Smrg#ifdef __clang_analyzer__
637ec681f3Smrg#ifdef __cplusplus
647ec681f3Smrgextern "C++" {
657ec681f3Smrgtemplate <typename... Ts>
667ec681f3Smrgconstexpr int __fake_use_va_args(Ts...) {
677ec681f3Smrg  return 0;
687ec681f3Smrg}
697ec681f3Smrg}
707ec681f3Smrg#else
717ec681f3Smrgextern int __fake_use_va_args(int, ...);
727ec681f3Smrg#endif /* __cplusplus */
737ec681f3Smrg#define __FAKE_USE_VA_ARGS(...) ((void)__fake_use_va_args(0, ##__VA_ARGS__))
747ec681f3Smrg#else
757ec681f3Smrg#define __FAKE_USE_VA_ARGS(...) ((void)(0))
767ec681f3Smrg#endif /* __clang_analyzer__ */
777ec681f3Smrg
787ec681f3Smrg#ifndef __predict_false
797ec681f3Smrg#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
807ec681f3Smrg#endif
817ec681f3Smrg
827ec681f3Smrg#define android_writeLog(prio, tag, text) __android_log_write(prio, tag, text)
837ec681f3Smrg
847ec681f3Smrg#define android_printLog(prio, tag, ...) \
857ec681f3Smrg  __android_log_print(prio, tag, __VA_ARGS__)
867ec681f3Smrg
877ec681f3Smrg#define android_vprintLog(prio, cond, tag, ...) \
887ec681f3Smrg  __android_log_vprint(prio, tag, __VA_ARGS__)
897ec681f3Smrg
907ec681f3Smrg/*
917ec681f3Smrg * Log macro that allows you to specify a number for the priority.
927ec681f3Smrg */
937ec681f3Smrg#ifndef LOG_PRI
947ec681f3Smrg#define LOG_PRI(priority, tag, ...) android_printLog(priority, tag, __VA_ARGS__)
957ec681f3Smrg#endif
967ec681f3Smrg
977ec681f3Smrg/*
987ec681f3Smrg * Log macro that allows you to pass in a varargs ("args" is a va_list).
997ec681f3Smrg */
1007ec681f3Smrg#ifndef LOG_PRI_VA
1017ec681f3Smrg#define LOG_PRI_VA(priority, tag, fmt, args) \
1027ec681f3Smrg  android_vprintLog(priority, NULL, tag, fmt, args)
1037ec681f3Smrg#endif
1047ec681f3Smrg
1057ec681f3Smrg/* --------------------------------------------------------------------- */
1067ec681f3Smrg
1077ec681f3Smrg/* XXX Macros to work around syntax errors in places where format string
1087ec681f3Smrg * arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF
1097ec681f3Smrg * (happens only in debug builds).
1107ec681f3Smrg */
1117ec681f3Smrg
1127ec681f3Smrg/* Returns 2nd arg.  Used to substitute default value if caller's vararg list
1137ec681f3Smrg * is empty.
1147ec681f3Smrg */
1157ec681f3Smrg#define __android_second(dummy, second, ...) second
1167ec681f3Smrg
1177ec681f3Smrg/* If passed multiple args, returns ',' followed by all but 1st arg, otherwise
1187ec681f3Smrg * returns nothing.
1197ec681f3Smrg */
1207ec681f3Smrg#define __android_rest(first, ...) , ##__VA_ARGS__
1217ec681f3Smrg
1227ec681f3Smrg#define android_printAssert(cond, tag, ...)                     \
1237ec681f3Smrg  __android_log_assert(cond, tag,                               \
1247ec681f3Smrg                       __android_second(0, ##__VA_ARGS__, NULL) \
1257ec681f3Smrg                           __android_rest(__VA_ARGS__))
1267ec681f3Smrg
1277ec681f3Smrg/*
1287ec681f3Smrg * Log a fatal error.  If the given condition fails, this stops program
1297ec681f3Smrg * execution like a normal assertion, but also generating the given message.
1307ec681f3Smrg * It is NOT stripped from release builds.  Note that the condition test
1317ec681f3Smrg * is -inverted- from the normal assert() semantics.
1327ec681f3Smrg */
1337ec681f3Smrg#ifndef LOG_ALWAYS_FATAL_IF
1347ec681f3Smrg#define LOG_ALWAYS_FATAL_IF(cond, ...)                                                    \
1357ec681f3Smrg  ((__predict_false(cond)) ? (__FAKE_USE_VA_ARGS(__VA_ARGS__),                            \
1367ec681f3Smrg                              ((void)android_printAssert(#cond, LOG_TAG, ##__VA_ARGS__))) \
1377ec681f3Smrg                           : ((void)0))
1387ec681f3Smrg#endif
1397ec681f3Smrg
1407ec681f3Smrg#ifndef LOG_ALWAYS_FATAL
1417ec681f3Smrg#define LOG_ALWAYS_FATAL(...) \
1427ec681f3Smrg  (((void)android_printAssert(NULL, LOG_TAG, ##__VA_ARGS__)))
1437ec681f3Smrg#endif
1447ec681f3Smrg
1457ec681f3Smrg/*
1467ec681f3Smrg * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that
1477ec681f3Smrg * are stripped out of release builds.
1487ec681f3Smrg */
1497ec681f3Smrg
1507ec681f3Smrg#if LOG_NDEBUG
1517ec681f3Smrg
1527ec681f3Smrg#ifndef LOG_FATAL_IF
1537ec681f3Smrg#define LOG_FATAL_IF(cond, ...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
1547ec681f3Smrg#endif
1557ec681f3Smrg#ifndef LOG_FATAL
1567ec681f3Smrg#define LOG_FATAL(...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
1577ec681f3Smrg#endif
1587ec681f3Smrg
1597ec681f3Smrg#else
1607ec681f3Smrg
1617ec681f3Smrg#ifndef LOG_FATAL_IF
1627ec681f3Smrg#define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ##__VA_ARGS__)
1637ec681f3Smrg#endif
1647ec681f3Smrg#ifndef LOG_FATAL
1657ec681f3Smrg#define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
1667ec681f3Smrg#endif
1677ec681f3Smrg
1687ec681f3Smrg#endif
1697ec681f3Smrg
1707ec681f3Smrg/*
1717ec681f3Smrg * Assertion that generates a log message when the assertion fails.
1727ec681f3Smrg * Stripped out of release builds.  Uses the current LOG_TAG.
1737ec681f3Smrg */
1747ec681f3Smrg#ifndef ALOG_ASSERT
1757ec681f3Smrg#define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ##__VA_ARGS__)
1767ec681f3Smrg#endif
1777ec681f3Smrg
1787ec681f3Smrg/* --------------------------------------------------------------------- */
1797ec681f3Smrg
1807ec681f3Smrg/*
1817ec681f3Smrg * C/C++ logging functions.  See the logging documentation for API details.
1827ec681f3Smrg *
1837ec681f3Smrg * We'd like these to be available from C code (in case we import some from
1847ec681f3Smrg * somewhere), so this has a C interface.
1857ec681f3Smrg *
1867ec681f3Smrg * The output will be correct when the log file is shared between multiple
1877ec681f3Smrg * threads and/or multiple processes so long as the operating system
1887ec681f3Smrg * supports O_APPEND.  These calls have mutex-protected data structures
1897ec681f3Smrg * and so are NOT reentrant.  Do not use LOG in a signal handler.
1907ec681f3Smrg */
1917ec681f3Smrg
1927ec681f3Smrg/* --------------------------------------------------------------------- */
1937ec681f3Smrg
1947ec681f3Smrg/*
1957ec681f3Smrg * Simplified macro to send a verbose log message using the current LOG_TAG.
1967ec681f3Smrg */
1977ec681f3Smrg#ifndef ALOGV
1987ec681f3Smrg#define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
1997ec681f3Smrg#if LOG_NDEBUG
2007ec681f3Smrg#define ALOGV(...)                   \
2017ec681f3Smrg  do {                               \
2027ec681f3Smrg    __FAKE_USE_VA_ARGS(__VA_ARGS__); \
2037ec681f3Smrg    if (false) {                     \
2047ec681f3Smrg      __ALOGV(__VA_ARGS__);          \
2057ec681f3Smrg    }                                \
2067ec681f3Smrg  } while (false)
2077ec681f3Smrg#else
2087ec681f3Smrg#define ALOGV(...) __ALOGV(__VA_ARGS__)
2097ec681f3Smrg#endif
2107ec681f3Smrg#endif
2117ec681f3Smrg
2127ec681f3Smrg#ifndef ALOGV_IF
2137ec681f3Smrg#if LOG_NDEBUG
2147ec681f3Smrg#define ALOGV_IF(cond, ...) __FAKE_USE_VA_ARGS(__VA_ARGS__)
2157ec681f3Smrg#else
2167ec681f3Smrg#define ALOGV_IF(cond, ...)                                                               \
2177ec681f3Smrg  ((__predict_false(cond))                                                                \
2187ec681f3Smrg       ? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \
2197ec681f3Smrg       : ((void)0))
2207ec681f3Smrg#endif
2217ec681f3Smrg#endif
2227ec681f3Smrg
2237ec681f3Smrg/*
2247ec681f3Smrg * Simplified macro to send a debug log message using the current LOG_TAG.
2257ec681f3Smrg */
2267ec681f3Smrg#ifndef ALOGD
2277ec681f3Smrg#define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
2287ec681f3Smrg#endif
2297ec681f3Smrg
2307ec681f3Smrg#ifndef ALOGD_IF
2317ec681f3Smrg#define ALOGD_IF(cond, ...)                                                             \
2327ec681f3Smrg  ((__predict_false(cond))                                                              \
2337ec681f3Smrg       ? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \
2347ec681f3Smrg       : ((void)0))
2357ec681f3Smrg#endif
2367ec681f3Smrg
2377ec681f3Smrg/*
2387ec681f3Smrg * Simplified macro to send an info log message using the current LOG_TAG.
2397ec681f3Smrg */
2407ec681f3Smrg#ifndef ALOGI
2417ec681f3Smrg#define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))
2427ec681f3Smrg#endif
2437ec681f3Smrg
2447ec681f3Smrg#ifndef ALOGI_IF
2457ec681f3Smrg#define ALOGI_IF(cond, ...)                                                            \
2467ec681f3Smrg  ((__predict_false(cond))                                                             \
2477ec681f3Smrg       ? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \
2487ec681f3Smrg       : ((void)0))
2497ec681f3Smrg#endif
2507ec681f3Smrg
2517ec681f3Smrg/*
2527ec681f3Smrg * Simplified macro to send a warning log message using the current LOG_TAG.
2537ec681f3Smrg */
2547ec681f3Smrg#ifndef ALOGW
2557ec681f3Smrg#define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
2567ec681f3Smrg#endif
2577ec681f3Smrg
2587ec681f3Smrg#ifndef ALOGW_IF
2597ec681f3Smrg#define ALOGW_IF(cond, ...)                                                            \
2607ec681f3Smrg  ((__predict_false(cond))                                                             \
2617ec681f3Smrg       ? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \
2627ec681f3Smrg       : ((void)0))
2637ec681f3Smrg#endif
2647ec681f3Smrg
2657ec681f3Smrg/*
2667ec681f3Smrg * Simplified macro to send an error log message using the current LOG_TAG.
2677ec681f3Smrg */
2687ec681f3Smrg#ifndef ALOGE
2697ec681f3Smrg#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
2707ec681f3Smrg#endif
2717ec681f3Smrg
2727ec681f3Smrg#ifndef ALOGE_IF
2737ec681f3Smrg#define ALOGE_IF(cond, ...)                                                             \
2747ec681f3Smrg  ((__predict_false(cond))                                                              \
2757ec681f3Smrg       ? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \
2767ec681f3Smrg       : ((void)0))
2777ec681f3Smrg#endif
2787ec681f3Smrg
2797ec681f3Smrg/* --------------------------------------------------------------------- */
2807ec681f3Smrg
2817ec681f3Smrg/*
2827ec681f3Smrg * Conditional based on whether the current LOG_TAG is enabled at
2837ec681f3Smrg * verbose priority.
2847ec681f3Smrg */
2857ec681f3Smrg#ifndef IF_ALOGV
2867ec681f3Smrg#if LOG_NDEBUG
2877ec681f3Smrg#define IF_ALOGV() if (false)
2887ec681f3Smrg#else
2897ec681f3Smrg#define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)
2907ec681f3Smrg#endif
2917ec681f3Smrg#endif
2927ec681f3Smrg
2937ec681f3Smrg/*
2947ec681f3Smrg * Conditional based on whether the current LOG_TAG is enabled at
2957ec681f3Smrg * debug priority.
2967ec681f3Smrg */
2977ec681f3Smrg#ifndef IF_ALOGD
2987ec681f3Smrg#define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)
2997ec681f3Smrg#endif
3007ec681f3Smrg
3017ec681f3Smrg/*
3027ec681f3Smrg * Conditional based on whether the current LOG_TAG is enabled at
3037ec681f3Smrg * info priority.
3047ec681f3Smrg */
3057ec681f3Smrg#ifndef IF_ALOGI
3067ec681f3Smrg#define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)
3077ec681f3Smrg#endif
3087ec681f3Smrg
3097ec681f3Smrg/*
3107ec681f3Smrg * Conditional based on whether the current LOG_TAG is enabled at
3117ec681f3Smrg * warn priority.
3127ec681f3Smrg */
3137ec681f3Smrg#ifndef IF_ALOGW
3147ec681f3Smrg#define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)
3157ec681f3Smrg#endif
3167ec681f3Smrg
3177ec681f3Smrg/*
3187ec681f3Smrg * Conditional based on whether the current LOG_TAG is enabled at
3197ec681f3Smrg * error priority.
3207ec681f3Smrg */
3217ec681f3Smrg#ifndef IF_ALOGE
3227ec681f3Smrg#define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)
3237ec681f3Smrg#endif
3247ec681f3Smrg
3257ec681f3Smrg/* --------------------------------------------------------------------- */
3267ec681f3Smrg
3277ec681f3Smrg/*
3287ec681f3Smrg * Basic log message macro.
3297ec681f3Smrg *
3307ec681f3Smrg * Example:
3317ec681f3Smrg *  ALOG(LOG_WARN, NULL, "Failed with error %d", errno);
3327ec681f3Smrg *
3337ec681f3Smrg * The second argument may be NULL or "" to indicate the "global" tag.
3347ec681f3Smrg */
3357ec681f3Smrg#ifndef ALOG
3367ec681f3Smrg#define ALOG(priority, tag, ...) LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)
3377ec681f3Smrg#endif
3387ec681f3Smrg
3397ec681f3Smrg/*
3407ec681f3Smrg * Conditional given a desired logging priority and tag.
3417ec681f3Smrg */
3427ec681f3Smrg#ifndef IF_ALOG
3437ec681f3Smrg#define IF_ALOG(priority, tag) if (android_testLog(ANDROID_##priority, tag))
3447ec681f3Smrg#endif
3457ec681f3Smrg
3467ec681f3Smrg/* --------------------------------------------------------------------- */
3477ec681f3Smrg
3487ec681f3Smrg/*
3497ec681f3Smrg *    IF_ALOG uses android_testLog, but IF_ALOG can be overridden.
3507ec681f3Smrg *    android_testLog will remain constant in its purpose as a wrapper
3517ec681f3Smrg *        for Android logging filter policy, and can be subject to
3527ec681f3Smrg *        change. It can be reused by the developers that override
3537ec681f3Smrg *        IF_ALOG as a convenient means to reimplement their policy
3547ec681f3Smrg *        over Android.
3557ec681f3Smrg */
3567ec681f3Smrg
3577ec681f3Smrg/*
3587ec681f3Smrg * Use the per-tag properties "log.tag.<tagname>" to generate a runtime
3597ec681f3Smrg * result of non-zero to expose a log. prio is ANDROID_LOG_VERBOSE to
3607ec681f3Smrg * ANDROID_LOG_FATAL. default_prio if no property. Undefined behavior if
3617ec681f3Smrg * any other value.
3627ec681f3Smrg */
3637ec681f3Smrgint __android_log_is_loggable(int prio, const char* tag, int default_prio);
3647ec681f3Smrgint __android_log_is_loggable_len(int prio, const char* tag, size_t len, int default_prio);
3657ec681f3Smrg
3667ec681f3Smrg#if LOG_NDEBUG /* Production */
3677ec681f3Smrg#define android_testLog(prio, tag) \
3687ec681f3Smrg  (__android_log_is_loggable_len(prio, tag, (tag) ? strlen(tag) : 0, ANDROID_LOG_DEBUG) != 0)
3697ec681f3Smrg#else
3707ec681f3Smrg#define android_testLog(prio, tag) \
3717ec681f3Smrg  (__android_log_is_loggable_len(prio, tag, (tag) ? strlen(tag) : 0, ANDROID_LOG_VERBOSE) != 0)
3727ec681f3Smrg#endif
3737ec681f3Smrg
3747ec681f3Smrg#if defined(__clang__)
3757ec681f3Smrg#pragma clang diagnostic pop
3767ec681f3Smrg#endif
3777ec681f3Smrg
3787ec681f3Smrg__END_DECLS
379