log.h revision dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0
1/* 2 * Copyright (C) 2005 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17// 18// C/C++ logging functions. See the logging documentation for API details. 19// 20// We'd like these to be available from C code (in case we import some from 21// somewhere), so this has a C interface. 22// 23// The output will be correct when the log file is shared between multiple 24// threads and/or multiple processes so long as the operating system 25// supports O_APPEND. These calls have mutex-protected data structures 26// and so are NOT reentrant. Do not use LOG in a signal handler. 27// 28#ifndef _LIBS_CUTILS_LOG_H 29#define _LIBS_CUTILS_LOG_H 30 31#include <stdio.h> 32#include <time.h> 33#include <sys/types.h> 34#include <unistd.h> 35#ifdef HAVE_PTHREADS 36#include <pthread.h> 37#endif 38#include <stdarg.h> 39 40#include <cutils/uio.h> 41#include <cutils/logd.h> 42 43#ifdef __cplusplus 44extern "C" { 45#endif 46 47// --------------------------------------------------------------------- 48 49/* 50 * Normally we strip LOGV (VERBOSE messages) from release builds. 51 * You can modify this (for example with "#define LOG_NDEBUG 0" 52 * at the top of your source file) to change that behavior. 53 */ 54#ifndef LOG_NDEBUG 55#ifdef NDEBUG 56#define LOG_NDEBUG 1 57#else 58#define LOG_NDEBUG 0 59#endif 60#endif 61 62/* 63 * This is the local tag used for the following simplified 64 * logging macros. You can change this preprocessor definition 65 * before using the other macros to change the tag. 66 */ 67#ifndef LOG_TAG 68#define LOG_TAG NULL 69#endif 70 71// --------------------------------------------------------------------- 72 73/* 74 * Simplified macro to send a verbose log message using the current LOG_TAG. 75 */ 76#ifndef LOGV 77#if LOG_NDEBUG 78#define LOGV(...) ((void)0) 79#else 80#define LOGV(...) ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) 81#endif 82#endif 83 84#define CONDITION(cond) (__builtin_expect((cond)!=0, 0)) 85 86#ifndef LOGV_IF 87#if LOG_NDEBUG 88#define LOGV_IF(cond, ...) ((void)0) 89#else 90#define LOGV_IF(cond, ...) \ 91 ( (CONDITION(cond)) \ 92 ? ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \ 93 : (void)0 ) 94#endif 95#endif 96 97/* 98 * Simplified macro to send a debug log message using the current LOG_TAG. 99 */ 100#ifndef LOGD 101#define LOGD(...) ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) 102#endif 103 104#ifndef LOGD_IF 105#define LOGD_IF(cond, ...) \ 106 ( (CONDITION(cond)) \ 107 ? ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \ 108 : (void)0 ) 109#endif 110 111/* 112 * Simplified macro to send an info log message using the current LOG_TAG. 113 */ 114#ifndef LOGI 115#define LOGI(...) ((void)LOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) 116#endif 117 118#ifndef LOGI_IF 119#define LOGI_IF(cond, ...) \ 120 ( (CONDITION(cond)) \ 121 ? ((void)LOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \ 122 : (void)0 ) 123#endif 124 125/* 126 * Simplified macro to send a warning log message using the current LOG_TAG. 127 */ 128#ifndef LOGW 129#define LOGW(...) ((void)LOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) 130#endif 131 132#ifndef LOGW_IF 133#define LOGW_IF(cond, ...) \ 134 ( (CONDITION(cond)) \ 135 ? ((void)LOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \ 136 : (void)0 ) 137#endif 138 139/* 140 * Simplified macro to send an error log message using the current LOG_TAG. 141 */ 142#ifndef LOGE 143#define LOGE(...) ((void)LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) 144#endif 145 146#ifndef LOGE_IF 147#define LOGE_IF(cond, ...) \ 148 ( (CONDITION(cond)) \ 149 ? ((void)LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ 150 : (void)0 ) 151#endif 152 153// --------------------------------------------------------------------- 154 155/* 156 * Conditional based on whether the current LOG_TAG is enabled at 157 * verbose priority. 158 */ 159#ifndef IF_LOGV 160#if LOG_NDEBUG 161#define IF_LOGV() if (false) 162#else 163#define IF_LOGV() IF_LOG(LOG_VERBOSE, LOG_TAG) 164#endif 165#endif 166 167/* 168 * Conditional based on whether the current LOG_TAG is enabled at 169 * debug priority. 170 */ 171#ifndef IF_LOGD 172#define IF_LOGD() IF_LOG(LOG_DEBUG, LOG_TAG) 173#endif 174 175/* 176 * Conditional based on whether the current LOG_TAG is enabled at 177 * info priority. 178 */ 179#ifndef IF_LOGI 180#define IF_LOGI() IF_LOG(LOG_INFO, LOG_TAG) 181#endif 182 183/* 184 * Conditional based on whether the current LOG_TAG is enabled at 185 * warn priority. 186 */ 187#ifndef IF_LOGW 188#define IF_LOGW() IF_LOG(LOG_WARN, LOG_TAG) 189#endif 190 191/* 192 * Conditional based on whether the current LOG_TAG is enabled at 193 * error priority. 194 */ 195#ifndef IF_LOGE 196#define IF_LOGE() IF_LOG(LOG_ERROR, LOG_TAG) 197#endif 198 199// --------------------------------------------------------------------- 200 201/* 202 * Log a fatal error. If the given condition fails, this stops program 203 * execution like a normal assertion, but also generating the given message. 204 * It is NOT stripped from release builds. Note that the condition test 205 * is -inverted- from the normal assert() semantics. 206 */ 207#define LOG_ALWAYS_FATAL_IF(cond, ...) \ 208 ( (CONDITION(cond)) \ 209 ? ((void)android_printAssert(#cond, LOG_TAG, __VA_ARGS__)) \ 210 : (void)0 ) 211 212#define LOG_ALWAYS_FATAL(...) \ 213 ( ((void)android_printAssert(NULL, LOG_TAG, __VA_ARGS__)) ) 214 215/* 216 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that 217 * are stripped out of release builds. 218 */ 219#if LOG_NDEBUG 220 221#define LOG_FATAL_IF(cond, ...) ((void)0) 222#define LOG_FATAL(...) ((void)0) 223 224#else 225 226#define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, __VA_ARGS__) 227#define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__) 228 229#endif 230 231/* 232 * Assertion that generates a log message when the assertion fails. 233 * Stripped out of release builds. Uses the current LOG_TAG. 234 */ 235#define LOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), __VA_ARGS__) 236//#define LOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond) 237 238// --------------------------------------------------------------------- 239 240/* 241 * Basic log message macro. 242 * 243 * Example: 244 * LOG(LOG_WARN, NULL, "Failed with error %d", errno); 245 * 246 * The second argument may be NULL or "" to indicate the "global" tag. 247 */ 248#ifndef LOG 249#define LOG(priority, tag, ...) \ 250 LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__) 251#endif 252 253/* 254 * Log macro that allows you to specify a number for the priority. 255 */ 256#ifndef LOG_PRI 257#define LOG_PRI(priority, tag, ...) \ 258 android_printLog(priority, tag, __VA_ARGS__) 259#endif 260 261/* 262 * Log macro that allows you to pass in a varargs ("args" is a va_list). 263 */ 264#ifndef LOG_PRI_VA 265#define LOG_PRI_VA(priority, tag, fmt, args) \ 266 android_vprintLog(priority, NULL, tag, fmt, args) 267#endif 268 269/* 270 * Conditional given a desired logging priority and tag. 271 */ 272#ifndef IF_LOG 273#define IF_LOG(priority, tag) \ 274 if (android_testLog(ANDROID_##priority, tag)) 275#endif 276 277// --------------------------------------------------------------------- 278 279/* 280 * Event logging. 281 */ 282 283/* 284 * Event log entry types. These must match up with the declarations in 285 * java/android/android/util/EventLog.java. 286 */ 287typedef enum { 288 EVENT_TYPE_INT = 0, 289 EVENT_TYPE_LONG = 1, 290 EVENT_TYPE_STRING = 2, 291 EVENT_TYPE_LIST = 3, 292} AndroidEventLogType; 293 294 295#define LOG_EVENT_INT(_tag, _value) { \ 296 int intBuf = _value; \ 297 (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, \ 298 sizeof(intBuf)); \ 299 } 300#define LOG_EVENT_LONG(_tag, _value) { \ 301 long long longBuf = _value; \ 302 (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, \ 303 sizeof(longBuf)); \ 304 } 305#define LOG_EVENT_STRING(_tag, _value) \ 306 ((void) 0) /* not implemented -- must combine len with string */ 307/* TODO: something for LIST */ 308 309/* 310 * =========================================================================== 311 * 312 * The stuff in the rest of this file should not be used directly. 313 */ 314 315#define android_printLog(prio, tag, fmt...) \ 316 __android_log_print(prio, tag, fmt) 317 318#define android_vprintLog(prio, cond, tag, fmt...) \ 319 __android_log_vprint(prio, tag, fmt) 320 321#define android_printAssert(cond, tag, fmt...) \ 322 __android_log_assert(cond, tag, fmt) 323 324#define android_writeLog(prio, tag, text) \ 325 __android_log_write(prio, tag, text) 326 327#define android_bWriteLog(tag, payload, len) \ 328 __android_log_bwrite(tag, payload, len) 329#define android_btWriteLog(tag, type, payload, len) \ 330 __android_log_btwrite(tag, type, payload, len) 331 332// TODO: remove these prototypes and their users 333#define android_testLog(prio, tag) (1) 334#define android_writevLog(vec,num) do{}while(0) 335#define android_write1Log(str,len) do{}while (0) 336#define android_setMinPriority(tag, prio) do{}while(0) 337//#define android_logToCallback(func) do{}while(0) 338#define android_logToFile(tag, file) (0) 339#define android_logToFd(tag, fd) (0) 340 341 342#ifdef __cplusplus 343} 344#endif 345 346#endif // _LIBS_CUTILS_LOG_H 347