1 /* 2 $License: 3 Copyright (C) 2011 InvenSense Corporation, All Rights Reserved. 4 $ 5 */ 6 7 /* 8 * This file incorporates work covered by the following copyright and 9 * permission notice: 10 * 11 * Copyright (C) 2005 The Android Open Source Project 12 * 13 * Licensed under the Apache License, Version 2.0 (the "License"); 14 * you may not use this file except in compliance with the License. 15 * You may obtain a copy of the License at 16 * 17 * http://www.apache.org/licenses/LICENSE-2.0 18 * 19 * Unless required by applicable law or agreed to in writing, software 20 * distributed under the License is distributed on an "AS IS" BASIS, 21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 * See the License for the specific language governing permissions and 23 * limitations under the License. 24 */ 25 26 /* 27 * C/C++ logging functions. See the logging documentation for API details. 28 * 29 * We'd like these to be available from C code (in case we import some from 30 * somewhere), so this has a C interface. 31 * 32 * The output will be correct when the log file is shared between multiple 33 * threads and/or multiple processes so long as the operating system 34 * supports O_APPEND. These calls have mutex-protected data structures 35 * and so are NOT reentrant. Do not use MPL_LOG in a signal handler. 36 */ 37 #ifndef _LIBS_CUTILS_MPL_LOG_H 38 #define _LIBS_CUTILS_MPL_LOG_H 39 40 #include <stdlib.h> 41 #include <stdarg.h> 42 43 #ifdef ANDROID 44 #ifdef NDK_BUILD 45 #include "log_macros.h" 46 #else 47 #include <utils/Log.h> /* For the LOG macro */ 48 #endif 49 #endif 50 51 #ifdef __KERNEL__ 52 #include <linux/kernel.h> 53 #endif 54 55 #ifdef __cplusplus 56 extern "C" { 57 #endif 58 59 60 /* --------------------------------------------------------------------- */ 61 62 /* 63 * Normally we strip MPL_LOGV (VERBOSE messages) from release builds. 64 * You can modify this (for example with "#define MPL_LOG_NDEBUG 0" 65 * at the top of your source file) to change that behavior. 66 */ 67 #ifndef MPL_LOG_NDEBUG 68 #ifdef NDEBUG 69 #define MPL_LOG_NDEBUG 1 70 #else 71 #define MPL_LOG_NDEBUG 0 72 #endif 73 #endif 74 75 #ifdef __KERNEL__ 76 #define MPL_LOG_UNKNOWN MPL_LOG_VERBOSE 77 #define MPL_LOG_DEFAULT KERN_DEFAULT 78 #define MPL_LOG_VERBOSE KERN_CONT 79 #define MPL_LOG_DEBUG KERN_NOTICE 80 #define MPL_LOG_INFO KERN_INFO 81 #define MPL_LOG_WARN KERN_WARNING 82 #define MPL_LOG_ERROR KERN_ERR 83 #define MPL_LOG_SILENT MPL_LOG_VERBOSE 84 85 #else 86 /* Based off the log priorities in android 87 /system/core/include/android/log.h */ 88 #define MPL_LOG_UNKNOWN (0) 89 #define MPL_LOG_DEFAULT (1) 90 #define MPL_LOG_VERBOSE (2) 91 #define MPL_LOG_DEBUG (3) 92 #define MPL_LOG_INFO (4) 93 #define MPL_LOG_WARN (5) 94 #define MPL_LOG_ERROR (6) 95 #define MPL_LOG_SILENT (8) 96 #endif 97 98 99 /* 100 * This is the local tag used for the following simplified 101 * logging macros. You can change this preprocessor definition 102 * before using the other macros to change the tag. 103 */ 104 #ifndef MPL_LOG_TAG 105 #ifdef __KERNEL__ 106 #define MPL_LOG_TAG 107 #else 108 #define MPL_LOG_TAG NULL 109 #endif 110 #endif 111 112 /* --------------------------------------------------------------------- */ 113 114 /* 115 * Simplified macro to send a verbose log message using the current MPL_LOG_TAG. 116 */ 117 #ifndef MPL_LOGV 118 #if MPL_LOG_NDEBUG 119 #ifdef _WIN32 120 #define MPL_LOGV(fmt, ...) \ 121 do { \ 122 __pragma (warning(suppress : 4127 )) \ 123 if (0) \ 124 MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__);\ 125 __pragma (warning(suppress : 4127 )) \ 126 } while (0) 127 #else 128 #define MPL_LOGV(fmt, ...) \ 129 do { \ 130 if (0) \ 131 MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__);\ 132 } while (0) 133 #endif 134 135 #else 136 #define MPL_LOGV(fmt, ...) MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 137 #endif 138 #endif 139 140 #ifndef CONDITION 141 #define CONDITION(cond) ((cond) != 0) 142 #endif 143 144 #ifndef MPL_LOGV_IF 145 #if MPL_LOG_NDEBUG 146 #define MPL_LOGV_IF(cond, fmt, ...) \ 147 do { if (0) MPL_LOG(fmt, ##__VA_ARGS__); } while (0) 148 #else 149 #define MPL_LOGV_IF(cond, fmt, ...) \ 150 ((CONDITION(cond)) \ 151 ? MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \ 152 : (void)0) 153 #endif 154 #endif 155 156 /* 157 * Simplified macro to send a debug log message using the current MPL_LOG_TAG. 158 */ 159 #ifndef MPL_LOGD 160 #define MPL_LOGD(fmt, ...) MPL_LOG(LOG_DEBUG, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 161 #endif 162 163 #ifndef MPL_LOGD_IF 164 #define MPL_LOGD_IF(cond, fmt, ...) \ 165 ((CONDITION(cond)) \ 166 ? MPL_LOG(LOG_DEBUG, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \ 167 : (void)0) 168 #endif 169 170 /* 171 * Simplified macro to send an info log message using the current MPL_LOG_TAG. 172 */ 173 #ifndef MPL_LOGI 174 #ifdef __KERNEL__ 175 #define MPL_LOGI(fmt, ...) pr_info(KERN_INFO MPL_LOG_TAG fmt, ##__VA_ARGS__) 176 #else 177 #define MPL_LOGI(fmt, ...) MPL_LOG(LOG_INFO, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 178 #endif 179 #endif 180 181 #ifndef MPL_LOGI_IF 182 #define MPL_LOGI_IF(cond, fmt, ...) \ 183 ((CONDITION(cond)) \ 184 ? MPL_LOG(LOG_INFO, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \ 185 : (void)0) 186 #endif 187 188 /* 189 * Simplified macro to send a warning log message using the current MPL_LOG_TAG. 190 */ 191 #ifndef MPL_LOGW 192 #ifdef __KERNEL__ 193 #define MPL_LOGW(fmt, ...) printk(KERN_WARNING MPL_LOG_TAG fmt, ##__VA_ARGS__) 194 #else 195 #define MPL_LOGW(fmt, ...) MPL_LOG(LOG_WARN, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 196 #endif 197 #endif 198 199 #ifndef MPL_LOGW_IF 200 #define MPL_LOGW_IF(cond, fmt, ...) \ 201 ((CONDITION(cond)) \ 202 ? MPL_LOG(LOG_WARN, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \ 203 : (void)0) 204 #endif 205 206 /* 207 * Simplified macro to send an error log message using the current MPL_LOG_TAG. 208 */ 209 #ifndef MPL_LOGE 210 #ifdef __KERNEL__ 211 #define MPL_LOGE(fmt, ...) printk(KERN_ERR MPL_LOG_TAG fmt, ##__VA_ARGS__) 212 #else 213 #define MPL_LOGE(fmt, ...) MPL_LOG(LOG_ERROR, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 214 #endif 215 #endif 216 217 #ifndef MPL_LOGE_IF 218 #define MPL_LOGE_IF(cond, fmt, ...) \ 219 ((CONDITION(cond)) \ 220 ? MPL_LOG(LOG_ERROR, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \ 221 : (void)0) 222 #endif 223 224 /* --------------------------------------------------------------------- */ 225 226 /* 227 * Log a fatal error. If the given condition fails, this stops program 228 * execution like a normal assertion, but also generating the given message. 229 * It is NOT stripped from release builds. Note that the condition test 230 * is -inverted- from the normal assert() semantics. 231 */ 232 #define MPL_LOG_ALWAYS_FATAL_IF(cond, fmt, ...) \ 233 ((CONDITION(cond)) \ 234 ? ((void)android_printAssert(#cond, MPL_LOG_TAG, \ 235 fmt, ##__VA_ARGS__)) \ 236 : (void)0) 237 238 #define MPL_LOG_ALWAYS_FATAL(fmt, ...) \ 239 (((void)android_printAssert(NULL, MPL_LOG_TAG, fmt, ##__VA_ARGS__))) 240 241 /* 242 * Versions of MPL_LOG_ALWAYS_FATAL_IF and MPL_LOG_ALWAYS_FATAL that 243 * are stripped out of release builds. 244 */ 245 #if MPL_LOG_NDEBUG 246 #define MPL_LOG_FATAL_IF(cond, fmt, ...) \ 247 do { \ 248 if (0) \ 249 MPL_LOG_ALWAYS_FATAL_IF(cond, fmt, ##__VA_ARGS__); \ 250 } while (0) 251 #define MPL_LOG_FATAL(fmt, ...) \ 252 do { \ 253 if (0) \ 254 MPL_LOG_ALWAYS_FATAL(fmt, ##__VA_ARGS__) \ 255 } while (0) 256 #else 257 #define MPL_LOG_FATAL_IF(cond, fmt, ...) \ 258 MPL_LOG_ALWAYS_FATAL_IF(cond, fmt, ##__VA_ARGS__) 259 #define MPL_LOG_FATAL(fmt, ...) \ 260 MPL_LOG_ALWAYS_FATAL(fmt, ##__VA_ARGS__) 261 #endif 262 263 /* 264 * Assertion that generates a log message when the assertion fails. 265 * Stripped out of release builds. Uses the current MPL_LOG_TAG. 266 */ 267 #define MPL_LOG_ASSERT(cond, fmt, ...) \ 268 MPL_LOG_FATAL_IF(!(cond), fmt, ##__VA_ARGS__) 269 270 /* --------------------------------------------------------------------- */ 271 272 /* 273 * Basic log message macro. 274 * 275 * Example: 276 * MPL_LOG(MPL_LOG_WARN, NULL, "Failed with error %d", errno); 277 * 278 * The second argument may be NULL or "" to indicate the "global" tag. 279 */ 280 #ifndef MPL_LOG 281 #define MPL_LOG(priority, tag, fmt, ...) \ 282 MPL_LOG_PRI(priority, tag, fmt, ##__VA_ARGS__) 283 #endif 284 285 /* 286 * Log macro that allows you to specify a number for the priority. 287 */ 288 #ifndef MPL_LOG_PRI 289 #ifdef ANDROID 290 #define MPL_LOG_PRI(priority, tag, fmt, ...) \ 291 ALOG(priority, tag, fmt, ##__VA_ARGS__) 292 #elif defined __KERNEL__ 293 #define MPL_LOG_PRI(priority, tag, fmt, ...) \ 294 pr_debug(MPL_##priority tag fmt, ##__VA_ARGS__) 295 #else 296 #define MPL_LOG_PRI(priority, tag, fmt, ...) \ 297 _MLPrintLog(MPL_##priority, tag, fmt, ##__VA_ARGS__) 298 #endif 299 #endif 300 301 /* 302 * Log macro that allows you to pass in a varargs ("args" is a va_list). 303 */ 304 #ifndef MPL_LOG_PRI_VA 305 #ifdef ANDROID 306 #define MPL_LOG_PRI_VA(priority, tag, fmt, args) \ 307 android_vprintLog(priority, NULL, tag, fmt, args) 308 #elif defined __KERNEL__ 309 /* not allowed in the Kernel because there is no dev_dbg that takes a va_list */ 310 #else 311 #define MPL_LOG_PRI_VA(priority, tag, fmt, args) \ 312 _MLPrintVaLog(priority, NULL, tag, fmt, args) 313 #endif 314 #endif 315 316 /* --------------------------------------------------------------------- */ 317 318 /* 319 * =========================================================================== 320 * 321 * The stuff in the rest of this file should not be used directly. 322 */ 323 324 #ifndef ANDROID 325 int _MLPrintLog(int priority, const char *tag, const char *fmt, ...); 326 int _MLPrintVaLog(int priority, const char *tag, const char *fmt, va_list args); 327 /* Final implementation of actual writing to a character device */ 328 int _MLWriteLog(const char *buf, int buflen); 329 #endif 330 331 static inline void __print_result_location(int result, 332 const char *file, 333 const char *func, int line) 334 { 335 MPL_LOGE("%s|%s|%d returning %d\n", file, func, line, result); 336 } 337 338 #ifdef _WIN32 339 /* The pragma removes warning about expression being constant */ 340 #define LOG_RESULT_LOCATION(condition) \ 341 do { \ 342 __print_result_location((int)(condition), __FILE__, \ 343 __func__, __LINE__); \ 344 __pragma (warning(suppress : 4127 )) \ 345 } while (0) 346 #else 347 #define LOG_RESULT_LOCATION(condition) \ 348 do { \ 349 __print_result_location((int)(condition), __FILE__, \ 350 __func__, __LINE__); \ 351 } while (0) 352 #endif 353 354 355 #define INV_ERROR_CHECK(r_1329) \ 356 if (r_1329) { \ 357 LOG_RESULT_LOCATION(r_1329); \ 358 return r_1329; \ 359 } 360 361 #ifdef __cplusplus 362 } 363 #endif 364 #endif /* _LIBS_CUTILS_MPL_LOG_H */ 365