Path: blob/21.2-virgl/include/android_stub/log/log_main.h
4547 views
/*1* Copyright (C) 2005-2017 The Android Open Source Project2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/1516#pragma once1718#include <stdbool.h>19#include <sys/cdefs.h>20#include <sys/types.h>2122#include <android/log.h>2324__BEGIN_DECLS2526/*27* Normally we strip the effects of ALOGV (VERBOSE messages),28* LOG_FATAL and LOG_FATAL_IF (FATAL assert messages) from the29* release builds be defining NDEBUG. You can modify this (for30* example with "#define LOG_NDEBUG 0" at the top of your source31* file) to change that behavior.32*/3334#ifndef LOG_NDEBUG35#ifdef NDEBUG36#define LOG_NDEBUG 137#else38#define LOG_NDEBUG 039#endif40#endif4142/* --------------------------------------------------------------------- */4344/*45* This file uses ", ## __VA_ARGS__" zero-argument token pasting to46* work around issues with debug-only syntax errors in assertions47* that are missing format strings. See commit48* 19299904343daf191267564fe32e6cd5c165cd4249*/50#if defined(__clang__)51#pragma clang diagnostic push52#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"53#endif5455/*56* Use __VA_ARGS__ if running a static analyzer,57* to avoid warnings of unused variables in __VA_ARGS__.58* Use constexpr function in C++ mode, so these macros can be used59* in other constexpr functions without warning.60*/61#ifdef __clang_analyzer__62#ifdef __cplusplus63extern "C++" {64template <typename... Ts>65constexpr int __fake_use_va_args(Ts...) {66return 0;67}68}69#else70extern int __fake_use_va_args(int, ...);71#endif /* __cplusplus */72#define __FAKE_USE_VA_ARGS(...) ((void)__fake_use_va_args(0, ##__VA_ARGS__))73#else74#define __FAKE_USE_VA_ARGS(...) ((void)(0))75#endif /* __clang_analyzer__ */7677#ifndef __predict_false78#define __predict_false(exp) __builtin_expect((exp) != 0, 0)79#endif8081#define android_writeLog(prio, tag, text) __android_log_write(prio, tag, text)8283#define android_printLog(prio, tag, ...) \84__android_log_print(prio, tag, __VA_ARGS__)8586#define android_vprintLog(prio, cond, tag, ...) \87__android_log_vprint(prio, tag, __VA_ARGS__)8889/*90* Log macro that allows you to specify a number for the priority.91*/92#ifndef LOG_PRI93#define LOG_PRI(priority, tag, ...) android_printLog(priority, tag, __VA_ARGS__)94#endif9596/*97* Log macro that allows you to pass in a varargs ("args" is a va_list).98*/99#ifndef LOG_PRI_VA100#define LOG_PRI_VA(priority, tag, fmt, args) \101android_vprintLog(priority, NULL, tag, fmt, args)102#endif103104/* --------------------------------------------------------------------- */105106/* XXX Macros to work around syntax errors in places where format string107* arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF108* (happens only in debug builds).109*/110111/* Returns 2nd arg. Used to substitute default value if caller's vararg list112* is empty.113*/114#define __android_second(dummy, second, ...) second115116/* If passed multiple args, returns ',' followed by all but 1st arg, otherwise117* returns nothing.118*/119#define __android_rest(first, ...) , ##__VA_ARGS__120121#define android_printAssert(cond, tag, ...) \122__android_log_assert(cond, tag, \123__android_second(0, ##__VA_ARGS__, NULL) \124__android_rest(__VA_ARGS__))125126/*127* Log a fatal error. If the given condition fails, this stops program128* execution like a normal assertion, but also generating the given message.129* It is NOT stripped from release builds. Note that the condition test130* is -inverted- from the normal assert() semantics.131*/132#ifndef LOG_ALWAYS_FATAL_IF133#define LOG_ALWAYS_FATAL_IF(cond, ...) \134((__predict_false(cond)) ? (__FAKE_USE_VA_ARGS(__VA_ARGS__), \135((void)android_printAssert(#cond, LOG_TAG, ##__VA_ARGS__))) \136: ((void)0))137#endif138139#ifndef LOG_ALWAYS_FATAL140#define LOG_ALWAYS_FATAL(...) \141(((void)android_printAssert(NULL, LOG_TAG, ##__VA_ARGS__)))142#endif143144/*145* Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that146* are stripped out of release builds.147*/148149#if LOG_NDEBUG150151#ifndef LOG_FATAL_IF152#define LOG_FATAL_IF(cond, ...) __FAKE_USE_VA_ARGS(__VA_ARGS__)153#endif154#ifndef LOG_FATAL155#define LOG_FATAL(...) __FAKE_USE_VA_ARGS(__VA_ARGS__)156#endif157158#else159160#ifndef LOG_FATAL_IF161#define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ##__VA_ARGS__)162#endif163#ifndef LOG_FATAL164#define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)165#endif166167#endif168169/*170* Assertion that generates a log message when the assertion fails.171* Stripped out of release builds. Uses the current LOG_TAG.172*/173#ifndef ALOG_ASSERT174#define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ##__VA_ARGS__)175#endif176177/* --------------------------------------------------------------------- */178179/*180* C/C++ logging functions. See the logging documentation for API details.181*182* We'd like these to be available from C code (in case we import some from183* somewhere), so this has a C interface.184*185* The output will be correct when the log file is shared between multiple186* threads and/or multiple processes so long as the operating system187* supports O_APPEND. These calls have mutex-protected data structures188* and so are NOT reentrant. Do not use LOG in a signal handler.189*/190191/* --------------------------------------------------------------------- */192193/*194* Simplified macro to send a verbose log message using the current LOG_TAG.195*/196#ifndef ALOGV197#define __ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))198#if LOG_NDEBUG199#define ALOGV(...) \200do { \201__FAKE_USE_VA_ARGS(__VA_ARGS__); \202if (false) { \203__ALOGV(__VA_ARGS__); \204} \205} while (false)206#else207#define ALOGV(...) __ALOGV(__VA_ARGS__)208#endif209#endif210211#ifndef ALOGV_IF212#if LOG_NDEBUG213#define ALOGV_IF(cond, ...) __FAKE_USE_VA_ARGS(__VA_ARGS__)214#else215#define ALOGV_IF(cond, ...) \216((__predict_false(cond)) \217? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \218: ((void)0))219#endif220#endif221222/*223* Simplified macro to send a debug log message using the current LOG_TAG.224*/225#ifndef ALOGD226#define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))227#endif228229#ifndef ALOGD_IF230#define ALOGD_IF(cond, ...) \231((__predict_false(cond)) \232? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \233: ((void)0))234#endif235236/*237* Simplified macro to send an info log message using the current LOG_TAG.238*/239#ifndef ALOGI240#define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__))241#endif242243#ifndef ALOGI_IF244#define ALOGI_IF(cond, ...) \245((__predict_false(cond)) \246? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \247: ((void)0))248#endif249250/*251* Simplified macro to send a warning log message using the current LOG_TAG.252*/253#ifndef ALOGW254#define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))255#endif256257#ifndef ALOGW_IF258#define ALOGW_IF(cond, ...) \259((__predict_false(cond)) \260? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \261: ((void)0))262#endif263264/*265* Simplified macro to send an error log message using the current LOG_TAG.266*/267#ifndef ALOGE268#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))269#endif270271#ifndef ALOGE_IF272#define ALOGE_IF(cond, ...) \273((__predict_false(cond)) \274? (__FAKE_USE_VA_ARGS(__VA_ARGS__), (void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \275: ((void)0))276#endif277278/* --------------------------------------------------------------------- */279280/*281* Conditional based on whether the current LOG_TAG is enabled at282* verbose priority.283*/284#ifndef IF_ALOGV285#if LOG_NDEBUG286#define IF_ALOGV() if (false)287#else288#define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)289#endif290#endif291292/*293* Conditional based on whether the current LOG_TAG is enabled at294* debug priority.295*/296#ifndef IF_ALOGD297#define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)298#endif299300/*301* Conditional based on whether the current LOG_TAG is enabled at302* info priority.303*/304#ifndef IF_ALOGI305#define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)306#endif307308/*309* Conditional based on whether the current LOG_TAG is enabled at310* warn priority.311*/312#ifndef IF_ALOGW313#define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)314#endif315316/*317* Conditional based on whether the current LOG_TAG is enabled at318* error priority.319*/320#ifndef IF_ALOGE321#define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)322#endif323324/* --------------------------------------------------------------------- */325326/*327* Basic log message macro.328*329* Example:330* ALOG(LOG_WARN, NULL, "Failed with error %d", errno);331*332* The second argument may be NULL or "" to indicate the "global" tag.333*/334#ifndef ALOG335#define ALOG(priority, tag, ...) LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__)336#endif337338/*339* Conditional given a desired logging priority and tag.340*/341#ifndef IF_ALOG342#define IF_ALOG(priority, tag) if (android_testLog(ANDROID_##priority, tag))343#endif344345/* --------------------------------------------------------------------- */346347/*348* IF_ALOG uses android_testLog, but IF_ALOG can be overridden.349* android_testLog will remain constant in its purpose as a wrapper350* for Android logging filter policy, and can be subject to351* change. It can be reused by the developers that override352* IF_ALOG as a convenient means to reimplement their policy353* over Android.354*/355356/*357* Use the per-tag properties "log.tag.<tagname>" to generate a runtime358* result of non-zero to expose a log. prio is ANDROID_LOG_VERBOSE to359* ANDROID_LOG_FATAL. default_prio if no property. Undefined behavior if360* any other value.361*/362int __android_log_is_loggable(int prio, const char* tag, int default_prio);363int __android_log_is_loggable_len(int prio, const char* tag, size_t len, int default_prio);364365#if LOG_NDEBUG /* Production */366#define android_testLog(prio, tag) \367(__android_log_is_loggable_len(prio, tag, ((tag) && *(tag)) ? strlen(tag) : 0, \368ANDROID_LOG_DEBUG) != 0)369#else370#define android_testLog(prio, tag) \371(__android_log_is_loggable_len(prio, tag, ((tag) && *(tag)) ? strlen(tag) : 0, \372ANDROID_LOG_VERBOSE) != 0)373#endif374375#if defined(__clang__)376#pragma clang diagnostic pop377#endif378379__END_DECLS380381382