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 56extern "C" { 57#endif 58 59#if defined ANDROID_LOLLIPOP 60//--yd 61#define LOG ALOG 62#endif 63 64#if defined ANDROID_JELLYBEAN || defined ANDROID_KITKAT 65#define LOG ALOG 66#define LOG_ERRROR ANDROID_LOG_ERROR 67#endif 68 69/* --------------------------------------------------------------------- */ 70 71/* 72 * Normally we strip MPL_LOGV (VERBOSE messages) from release builds. 73 * You can modify this (for example with "#define MPL_LOG_NDEBUG 0" 74 * at the top of your source file) to change that behavior. 75 */ 76#ifndef MPL_LOG_NDEBUG 77#ifdef NDEBUG 78#define MPL_LOG_NDEBUG 1 79#else 80#define MPL_LOG_NDEBUG 0 81#endif 82#endif 83 84#ifdef __KERNEL__ 85#define MPL_LOG_UNKNOWN MPL_LOG_VERBOSE 86#define MPL_LOG_DEFAULT KERN_DEFAULT 87#define MPL_LOG_VERBOSE KERN_CONT 88#define MPL_LOG_DEBUG KERN_NOTICE 89#define MPL_LOG_INFO KERN_INFO 90#define MPL_LOG_WARN KERN_WARNING 91#define MPL_LOG_ERROR KERN_ERR 92#define MPL_LOG_SILENT MPL_LOG_VERBOSE 93 94#else 95 /* Based off the log priorities in android 96 /system/core/include/android/log.h */ 97#if defined ANDROID_LOLLIPOP 98//--yd 99#define MPL_LOG_UNKNOWN LOG_UNKNOWN 100#define MPL_LOG_DEFAULT LOG_DEFAULT 101#define MPL_LOG_VERBOSE LOG_VERBOSE 102#define MPL_LOG_DEBUG LOG_DEBUG 103#define MPL_LOG_INFO LOG_INFO 104#define MPL_LOG_WARN LOG_WARN 105#define MPL_LOG_ERROR LOG_ERROR 106//--yd #define MPL_LOG_FATAL LOG_FATAL 107#define MPL_LOG_SILENT LOG_SILENT 108#else 109#define MPL_LOG_UNKNOWN (0) 110#define MPL_LOG_DEFAULT (1) 111#define MPL_LOG_VERBOSE (2) 112#define MPL_LOG_DEBUG (3) 113#define MPL_LOG_INFO (4) 114#define MPL_LOG_WARN (5) 115#define MPL_LOG_ERROR (6) 116#define MPL_LOG_SILENT (8) 117#endif 118#endif 119 120 121/* 122 * This is the local tag used for the following simplified 123 * logging macros. You can change this preprocessor definition 124 * before using the other macros to change the tag. 125 */ 126#ifndef MPL_LOG_TAG 127#ifdef __KERNEL__ 128#define MPL_LOG_TAG 129#else 130#define MPL_LOG_TAG NULL 131#endif 132#endif 133 134/* --------------------------------------------------------------------- */ 135 136/* 137 * Simplified macro to send a verbose log message using the current MPL_LOG_TAG. 138 */ 139#ifndef MPL_LOGV 140#if MPL_LOG_NDEBUG 141#ifdef _WIN32 142#define MPL_LOGV(fmt, ...) \ 143 do { \ 144 __pragma (warning(suppress : 4127 )) \ 145 if (0) \ 146 MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__);\ 147 __pragma (warning(suppress : 4127 )) \ 148 } while (0) 149#else 150#if defined ANDROID_LOLLIPOP 151//--yd 152#define MPL_LOGV(fmt, ...) \ 153 do { \ 154 if (0) \ 155 MPL_LOG(MPL_LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__);\ 156 } while (0) 157#else 158#define MPL_LOGV(fmt, ...) \ 159 do { \ 160 if (0) \ 161 MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__);\ 162 } while (0) 163#endif 164#endif 165#else 166#if defined ANDROID_LOLLIPOP 167//--yd 168#define MPL_LOGV(fmt, ...) MPL_LOG(MPL_LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 169#else 170#define MPL_LOGV(fmt, ...) MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 171#endif 172#endif 173#endif 174 175#ifndef CONDITION 176#define CONDITION(cond) ((cond) != 0) 177#endif 178 179#ifndef MPL_LOGV_IF 180#if MPL_LOG_NDEBUG 181#define MPL_LOGV_IF(cond, fmt, ...) \ 182 do { if (0) MPL_LOG(fmt, ##__VA_ARGS__); } while (0) 183#else 184#define MPL_LOGV_IF(cond, fmt, ...) \ 185 ((CONDITION(cond)) \ 186 ? MPL_LOG(LOG_VERBOSE, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \ 187 : (void)0) 188#endif 189#endif 190 191/* 192 * Simplified macro to send a debug log message using the current MPL_LOG_TAG. 193 */ 194#ifndef MPL_LOGD 195#if defined ANDROID_LOLLIPOP 196//--yd 197#define MPL_LOGD(fmt, ...) MPL_LOG(MPL_LOG_DEBUG, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 198#else 199#define MPL_LOGD(fmt, ...) MPL_LOG(LOG_DEBUG, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 200#endif 201#endif 202 203#ifndef MPL_LOGD_IF 204#define MPL_LOGD_IF(cond, fmt, ...) \ 205 ((CONDITION(cond)) \ 206 ? MPL_LOG(LOG_DEBUG, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \ 207 : (void)0) 208#endif 209 210/* 211 * Simplified macro to send an info log message using the current MPL_LOG_TAG. 212 */ 213#ifndef MPL_LOGI 214#ifdef __KERNEL__ 215#define MPL_LOGI(fmt, ...) pr_info(KERN_INFO MPL_LOG_TAG fmt, ##__VA_ARGS__) 216#else 217#if defined ANDROID_LOLLIPOP 218//--yd 219#define MPL_LOGI(fmt, ...) MPL_LOG(MPL_LOG_INFO, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 220#else 221#define MPL_LOGI(fmt, ...) MPL_LOG(LOG_INFO, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 222#endif 223#endif 224#endif 225 226#ifndef MPL_LOGI_IF 227#define MPL_LOGI_IF(cond, fmt, ...) \ 228 ((CONDITION(cond)) \ 229 ? MPL_LOG(LOG_INFO, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \ 230 : (void)0) 231#endif 232 233/* 234 * Simplified macro to send a warning log message using the current MPL_LOG_TAG. 235 */ 236#ifndef MPL_LOGW 237#ifdef __KERNEL__ 238#define MPL_LOGW(fmt, ...) printk(KERN_WARNING MPL_LOG_TAG fmt, ##__VA_ARGS__) 239#else 240#define MPL_LOGW(fmt, ...) MPL_LOG(LOG_WARN, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 241#endif 242#endif 243 244#ifndef MPL_LOGW_IF 245#define MPL_LOGW_IF(cond, fmt, ...) \ 246 ((CONDITION(cond)) \ 247 ? MPL_LOG(LOG_WARN, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \ 248 : (void)0) 249#endif 250 251/* 252 * Simplified macro to send an error log message using the current MPL_LOG_TAG. 253 */ 254#ifndef MPL_LOGE 255#ifdef __KERNEL__ 256#define MPL_LOGE(fmt, ...) printk(KERN_ERR MPL_LOG_TAG fmt, ##__VA_ARGS__) 257#else 258#if defined ANDROID_LOLLIPOP 259//--yd 260#define MPL_LOGE(fmt, ...) MPL_LOG(MPL_LOG_ERROR, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 261#else 262#define MPL_LOGE(fmt, ...) MPL_LOG(LOG_ERROR, MPL_LOG_TAG, fmt, ##__VA_ARGS__) 263#endif 264#endif 265#endif 266 267#ifndef MPL_LOGE_IF 268#if defined ANDROID_LOLLIPOP 269//--yd 270#define MPL_LOGE_IF(cond, fmt, ...) \ 271 ((CONDITION(cond)) \ 272 ? MPL_LOG(MPL_LOG_ERROR, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \ 273 : (void)0) 274#else 275#define MPL_LOGE_IF(cond, fmt, ...) \ 276 ((CONDITION(cond)) \ 277 ? MPL_LOG(LOG_ERROR, MPL_LOG_TAG, fmt, ##__VA_ARGS__) \ 278 : (void)0) 279#endif 280#endif 281 282/* --------------------------------------------------------------------- */ 283 284/* 285 * Log a fatal error. If the given condition fails, this stops program 286 * execution like a normal assertion, but also generating the given message. 287 * It is NOT stripped from release builds. Note that the condition test 288 * is -inverted- from the normal assert() semantics. 289 */ 290#define MPL_LOG_ALWAYS_FATAL_IF(cond, fmt, ...) \ 291 ((CONDITION(cond)) \ 292 ? ((void)android_printAssert(#cond, MPL_LOG_TAG, \ 293 fmt, ##__VA_ARGS__)) \ 294 : (void)0) 295 296#define MPL_LOG_ALWAYS_FATAL(fmt, ...) \ 297 (((void)android_printAssert(NULL, MPL_LOG_TAG, fmt, ##__VA_ARGS__))) 298 299/* 300 * Versions of MPL_LOG_ALWAYS_FATAL_IF and MPL_LOG_ALWAYS_FATAL that 301 * are stripped out of release builds. 302 */ 303#if MPL_LOG_NDEBUG 304#define MPL_LOG_FATAL_IF(cond, fmt, ...) \ 305 do { \ 306 if (0) \ 307 MPL_LOG_ALWAYS_FATAL_IF(cond, fmt, ##__VA_ARGS__); \ 308 } while (0) 309#define MPL_LOG_FATAL(fmt, ...) \ 310 do { \ 311 if (0) \ 312 MPL_LOG_ALWAYS_FATAL(fmt, ##__VA_ARGS__) \ 313 } while (0) 314#else 315#define MPL_LOG_FATAL_IF(cond, fmt, ...) \ 316 MPL_LOG_ALWAYS_FATAL_IF(cond, fmt, ##__VA_ARGS__) 317#define MPL_LOG_FATAL(fmt, ...) \ 318 MPL_LOG_ALWAYS_FATAL(fmt, ##__VA_ARGS__) 319#endif 320 321/* 322 * Assertion that generates a log message when the assertion fails. 323 * Stripped out of release builds. Uses the current MPL_LOG_TAG. 324 */ 325#define MPL_LOG_ASSERT(cond, fmt, ...) \ 326 MPL_LOG_FATAL_IF(!(cond), fmt, ##__VA_ARGS__) 327 328/* --------------------------------------------------------------------- */ 329 330/* 331 * Basic log message macro. 332 * 333 * Example: 334 * MPL_LOG(MPL_LOG_WARN, NULL, "Failed with error %d", errno); 335 * 336 * The second argument may be NULL or "" to indicate the "global" tag. 337 */ 338#ifndef MPL_LOG 339#define MPL_LOG(priority, tag, fmt, ...) \ 340 MPL_LOG_PRI(priority, tag, fmt, ##__VA_ARGS__) 341#endif 342 343/* 344 * Log macro that allows you to specify a number for the priority. 345 */ 346#ifndef MPL_LOG_PRI 347#ifdef ANDROID 348#define MPL_LOG_PRI(priority, tag, fmt, ...) \ 349 LOG(priority, tag, fmt, ##__VA_ARGS__) 350#elif defined __KERNEL__ 351#define MPL_LOG_PRI(priority, tag, fmt, ...) \ 352 pr_debug(MPL_##priority tag fmt, ##__VA_ARGS__) 353#else 354#define MPL_LOG_PRI(priority, tag, fmt, ...) \ 355 _MLPrintLog(MPL_##priority, tag, fmt, ##__VA_ARGS__) 356#endif 357#endif 358 359/* 360 * Log macro that allows you to pass in a varargs ("args" is a va_list). 361 */ 362#ifndef MPL_LOG_PRI_VA 363#ifdef ANDROID 364#define MPL_LOG_PRI_VA(priority, tag, fmt, args) \ 365 android_vprintLog(priority, NULL, tag, fmt, args) 366#elif defined __KERNEL__ 367/* not allowed in the Kernel because there is no dev_dbg that takes a va_list */ 368#else 369#define MPL_LOG_PRI_VA(priority, tag, fmt, args) \ 370 _MLPrintVaLog(priority, NULL, tag, fmt, args) 371#endif 372#endif 373 374/* --------------------------------------------------------------------- */ 375 376/* 377 * =========================================================================== 378 * 379 * The stuff in the rest of this file should not be used directly. 380 */ 381 382#ifndef ANDROID 383int _MLPrintLog(int priority, const char *tag, const char *fmt, ...); 384int _MLPrintVaLog(int priority, const char *tag, const char *fmt, va_list args); 385/* Final implementation of actual writing to a character device */ 386int _MLWriteLog(const char *buf, int buflen); 387#endif 388 389static inline void __print_result_location(int result, 390 const char *file, 391 const char *func, int line) 392{ 393 MPL_LOGE("%s|%s|%d returning %d\n", file, func, line, result); 394} 395 396#ifdef _WIN32 397/* The pragma removes warning about expression being constant */ 398#define LOG_RESULT_LOCATION(condition) \ 399 do { \ 400 __print_result_location((int)(condition), __FILE__, \ 401 __func__, __LINE__); \ 402 __pragma (warning(suppress : 4127 )) \ 403 } while (0) 404#else 405#define LOG_RESULT_LOCATION(condition) \ 406 do { \ 407 __print_result_location((int)(condition), __FILE__, \ 408 __func__, __LINE__); \ 409 } while (0) 410#endif 411 412 413#define INV_ERROR_CHECK(r_1329) \ 414 if (r_1329) { \ 415 LOG_RESULT_LOCATION(r_1329); \ 416 return r_1329; \ 417 } 418 419#ifdef __cplusplus 420} 421#endif 422#endif /* _LIBS_CUTILS_MPL_LOG_H */ 423