1*4882a593Smuzhiyun // 2*4882a593Smuzhiyun // Copyright 2005 The Android Open Source Project 3*4882a593Smuzhiyun // 4*4882a593Smuzhiyun // C/C++ logging functions. See the logging documentation for API details. 5*4882a593Smuzhiyun // 6*4882a593Smuzhiyun // We'd like these to be available from C code (in case we import some from 7*4882a593Smuzhiyun // somewhere), so this has a C interface. 8*4882a593Smuzhiyun // 9*4882a593Smuzhiyun // The output will be correct when the log file is shared between multiple 10*4882a593Smuzhiyun // threads and/or multiple processes so long as the operating system 11*4882a593Smuzhiyun // supports O_APPEND. These calls have mutex-protected data structures 12*4882a593Smuzhiyun // and so are NOT reentrant. Do not use LOG in a signal handler. 13*4882a593Smuzhiyun // 14*4882a593Smuzhiyun #ifndef _MINZIP_LOG_H 15*4882a593Smuzhiyun #define _MINZIP_LOG_H 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun #include <stdio.h> 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun // --------------------------------------------------------------------- 20*4882a593Smuzhiyun 21*4882a593Smuzhiyun /* 22*4882a593Smuzhiyun * Normally we strip LOGV (VERBOSE messages) from release builds. 23*4882a593Smuzhiyun * You can modify this (for example with "#define LOG_NDEBUG 0" 24*4882a593Smuzhiyun * at the top of your source file) to change that behavior. 25*4882a593Smuzhiyun */ 26*4882a593Smuzhiyun #ifndef LOG_NDEBUG 27*4882a593Smuzhiyun #ifdef NDEBUG 28*4882a593Smuzhiyun #define LOG_NDEBUG 1 29*4882a593Smuzhiyun #else 30*4882a593Smuzhiyun #define LOG_NDEBUG 0 31*4882a593Smuzhiyun #endif 32*4882a593Smuzhiyun #endif 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun /* 35*4882a593Smuzhiyun * This is the local tag used for the following simplified 36*4882a593Smuzhiyun * logging macros. You can change this preprocessor definition 37*4882a593Smuzhiyun * before using the other macros to change the tag. 38*4882a593Smuzhiyun */ 39*4882a593Smuzhiyun #ifndef LOG_TAG 40*4882a593Smuzhiyun #define LOG_TAG NULL 41*4882a593Smuzhiyun #endif 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun // --------------------------------------------------------------------- 44*4882a593Smuzhiyun 45*4882a593Smuzhiyun /* 46*4882a593Smuzhiyun * Simplified macro to send a verbose log message using the current LOG_TAG. 47*4882a593Smuzhiyun */ 48*4882a593Smuzhiyun #ifndef LOGV 49*4882a593Smuzhiyun #if LOG_NDEBUG 50*4882a593Smuzhiyun #define LOGV(...) ((void)0) 51*4882a593Smuzhiyun #else 52*4882a593Smuzhiyun #define LOGV(...) ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) 53*4882a593Smuzhiyun #endif 54*4882a593Smuzhiyun #endif 55*4882a593Smuzhiyun 56*4882a593Smuzhiyun #define CONDITION(cond) (__builtin_expect((cond)!=0, 0)) 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun #ifndef LOGV_IF 59*4882a593Smuzhiyun #if LOG_NDEBUG 60*4882a593Smuzhiyun #define LOGV_IF(cond, ...) ((void)0) 61*4882a593Smuzhiyun #else 62*4882a593Smuzhiyun #define LOGV_IF(cond, ...) \ 63*4882a593Smuzhiyun ( (CONDITION(cond)) \ 64*4882a593Smuzhiyun ? ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \ 65*4882a593Smuzhiyun : (void)0 ) 66*4882a593Smuzhiyun #endif 67*4882a593Smuzhiyun #endif 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun #define LOGVV LOGV 70*4882a593Smuzhiyun #define LOGVV_IF LOGV_IF 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun /* 73*4882a593Smuzhiyun * Simplified macro to send a debug log message using the current LOG_TAG. 74*4882a593Smuzhiyun */ 75*4882a593Smuzhiyun #ifndef LOGD 76*4882a593Smuzhiyun #define LOGD(...) ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) 77*4882a593Smuzhiyun #endif 78*4882a593Smuzhiyun 79*4882a593Smuzhiyun #ifndef LOGD_IF 80*4882a593Smuzhiyun #define LOGD_IF(cond, ...) \ 81*4882a593Smuzhiyun ( (CONDITION(cond)) \ 82*4882a593Smuzhiyun ? ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \ 83*4882a593Smuzhiyun : (void)0 ) 84*4882a593Smuzhiyun #endif 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun /* 87*4882a593Smuzhiyun * Simplified macro to send an info log message using the current LOG_TAG. 88*4882a593Smuzhiyun */ 89*4882a593Smuzhiyun #ifndef LOGI 90*4882a593Smuzhiyun #define LOGI(...) ((void)LOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) 91*4882a593Smuzhiyun #endif 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun #ifndef LOGI_IF 94*4882a593Smuzhiyun #define LOGI_IF(cond, ...) \ 95*4882a593Smuzhiyun ( (CONDITION(cond)) \ 96*4882a593Smuzhiyun ? ((void)LOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \ 97*4882a593Smuzhiyun : (void)0 ) 98*4882a593Smuzhiyun #endif 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun /* 101*4882a593Smuzhiyun * Simplified macro to send a warning log message using the current LOG_TAG. 102*4882a593Smuzhiyun */ 103*4882a593Smuzhiyun #ifndef LOGW 104*4882a593Smuzhiyun #define LOGW(...) ((void)LOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) 105*4882a593Smuzhiyun #endif 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun #ifndef LOGW_IF 108*4882a593Smuzhiyun #define LOGW_IF(cond, ...) \ 109*4882a593Smuzhiyun ( (CONDITION(cond)) \ 110*4882a593Smuzhiyun ? ((void)LOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \ 111*4882a593Smuzhiyun : (void)0 ) 112*4882a593Smuzhiyun #endif 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun /* 115*4882a593Smuzhiyun * Simplified macro to send an error log message using the current LOG_TAG. 116*4882a593Smuzhiyun */ 117*4882a593Smuzhiyun #ifndef LOGE 118*4882a593Smuzhiyun #define LOGE(...) ((void)LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) 119*4882a593Smuzhiyun #endif 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun #ifndef LOGE_IF 122*4882a593Smuzhiyun #define LOGE_IF(cond, ...) \ 123*4882a593Smuzhiyun ( (CONDITION(cond)) \ 124*4882a593Smuzhiyun ? ((void)LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ 125*4882a593Smuzhiyun : (void)0 ) 126*4882a593Smuzhiyun #endif 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun /* 130*4882a593Smuzhiyun * Conditional based on whether the current LOG_TAG is enabled at 131*4882a593Smuzhiyun * verbose priority. 132*4882a593Smuzhiyun */ 133*4882a593Smuzhiyun #ifndef IF_LOGV 134*4882a593Smuzhiyun #if LOG_NDEBUG 135*4882a593Smuzhiyun #define IF_LOGV() if (false) 136*4882a593Smuzhiyun #else 137*4882a593Smuzhiyun #define IF_LOGV() IF_LOG(LOG_VERBOSE, LOG_TAG) 138*4882a593Smuzhiyun #endif 139*4882a593Smuzhiyun #endif 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun /* 142*4882a593Smuzhiyun * Conditional based on whether the current LOG_TAG is enabled at 143*4882a593Smuzhiyun * debug priority. 144*4882a593Smuzhiyun */ 145*4882a593Smuzhiyun #ifndef IF_LOGD 146*4882a593Smuzhiyun #define IF_LOGD() IF_LOG(LOG_DEBUG, LOG_TAG) 147*4882a593Smuzhiyun #endif 148*4882a593Smuzhiyun 149*4882a593Smuzhiyun /* 150*4882a593Smuzhiyun * Conditional based on whether the current LOG_TAG is enabled at 151*4882a593Smuzhiyun * info priority. 152*4882a593Smuzhiyun */ 153*4882a593Smuzhiyun #ifndef IF_LOGI 154*4882a593Smuzhiyun #define IF_LOGI() IF_LOG(LOG_INFO, LOG_TAG) 155*4882a593Smuzhiyun #endif 156*4882a593Smuzhiyun 157*4882a593Smuzhiyun /* 158*4882a593Smuzhiyun * Conditional based on whether the current LOG_TAG is enabled at 159*4882a593Smuzhiyun * warn priority. 160*4882a593Smuzhiyun */ 161*4882a593Smuzhiyun #ifndef IF_LOGW 162*4882a593Smuzhiyun #define IF_LOGW() IF_LOG(LOG_WARN, LOG_TAG) 163*4882a593Smuzhiyun #endif 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun /* 166*4882a593Smuzhiyun * Conditional based on whether the current LOG_TAG is enabled at 167*4882a593Smuzhiyun * error priority. 168*4882a593Smuzhiyun */ 169*4882a593Smuzhiyun #ifndef IF_LOGE 170*4882a593Smuzhiyun #define IF_LOGE() IF_LOG(LOG_ERROR, LOG_TAG) 171*4882a593Smuzhiyun #endif 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun // --------------------------------------------------------------------- 174*4882a593Smuzhiyun 175*4882a593Smuzhiyun /* 176*4882a593Smuzhiyun * Basic log message macro. 177*4882a593Smuzhiyun * 178*4882a593Smuzhiyun * Example: 179*4882a593Smuzhiyun * LOG(LOG_WARN, NULL, "Failed with error %d", errno); 180*4882a593Smuzhiyun * 181*4882a593Smuzhiyun * The second argument may be NULL or "" to indicate the "global" tag. 182*4882a593Smuzhiyun * 183*4882a593Smuzhiyun * Non-gcc probably won't have __FUNCTION__. It's not vital. gcc also 184*4882a593Smuzhiyun * offers __PRETTY_FUNCTION__, which is rather more than we need. 185*4882a593Smuzhiyun */ 186*4882a593Smuzhiyun #ifndef LOG 187*4882a593Smuzhiyun #define LOG(priority, tag, ...) \ 188*4882a593Smuzhiyun LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__) 189*4882a593Smuzhiyun #endif 190*4882a593Smuzhiyun 191*4882a593Smuzhiyun /* 192*4882a593Smuzhiyun * Log macro that allows you to specify a number for the priority. 193*4882a593Smuzhiyun */ 194*4882a593Smuzhiyun #ifndef LOG_PRI 195*4882a593Smuzhiyun #define LOG_PRI(priority, tag, ...) \ 196*4882a593Smuzhiyun printf(tag ": " __VA_ARGS__) 197*4882a593Smuzhiyun #endif 198*4882a593Smuzhiyun 199*4882a593Smuzhiyun /* 200*4882a593Smuzhiyun * Conditional given a desired logging priority and tag. 201*4882a593Smuzhiyun */ 202*4882a593Smuzhiyun #ifndef IF_LOG 203*4882a593Smuzhiyun #define IF_LOG(priority, tag) \ 204*4882a593Smuzhiyun if (1) 205*4882a593Smuzhiyun #endif 206*4882a593Smuzhiyun 207*4882a593Smuzhiyun #endif // _MINZIP_LOG_H 208