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