log.h revision 31a02dba709166df30b7c3352222c550146d7c81
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#define ALOGV LOGV 83#endif 84 85#define CONDITION(cond) (__builtin_expect((cond)!=0, 0)) 86 87#ifndef LOGV_IF 88#if LOG_NDEBUG 89#define LOGV_IF(cond, ...) ((void)0) 90#else 91#define LOGV_IF(cond, ...) \ 92 ( (CONDITION(cond)) \ 93 ? ((void)LOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \ 94 : (void)0 ) 95#endif 96#define ALOGV_IF LOGV_IF 97#endif 98 99/* 100 * Simplified macro to send a debug log message using the current LOG_TAG. 101 */ 102#ifndef LOGD 103#define LOGD(...) ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) 104#endif 105 106#ifndef LOGD_IF 107#define LOGD_IF(cond, ...) \ 108 ( (CONDITION(cond)) \ 109 ? ((void)LOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \ 110 : (void)0 ) 111#endif 112 113/* 114 * Simplified macro to send an info log message using the current LOG_TAG. 115 */ 116#ifndef LOGI 117#define LOGI(...) ((void)LOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) 118#endif 119 120#ifndef LOGI_IF 121#define LOGI_IF(cond, ...) \ 122 ( (CONDITION(cond)) \ 123 ? ((void)LOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \ 124 : (void)0 ) 125#endif 126 127/* 128 * Simplified macro to send a warning log message using the current LOG_TAG. 129 */ 130#ifndef LOGW 131#define LOGW(...) ((void)LOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) 132#endif 133 134#ifndef LOGW_IF 135#define LOGW_IF(cond, ...) \ 136 ( (CONDITION(cond)) \ 137 ? ((void)LOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \ 138 : (void)0 ) 139#endif 140 141/* 142 * Simplified macro to send an error log message using the current LOG_TAG. 143 */ 144#ifndef LOGE 145#define LOGE(...) ((void)LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) 146#endif 147 148#ifndef LOGE_IF 149#define LOGE_IF(cond, ...) \ 150 ( (CONDITION(cond)) \ 151 ? ((void)LOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ 152 : (void)0 ) 153#endif 154 155// --------------------------------------------------------------------- 156 157/* 158 * Conditional based on whether the current LOG_TAG is enabled at 159 * verbose priority. 160 */ 161#ifndef IF_LOGV 162#if LOG_NDEBUG 163#define IF_LOGV() if (false) 164#else 165#define IF_LOGV() IF_LOG(LOG_VERBOSE, LOG_TAG) 166#endif 167#define IF_ALOGV IF_LOGV 168#endif 169 170/* 171 * Conditional based on whether the current LOG_TAG is enabled at 172 * debug priority. 173 */ 174#ifndef IF_LOGD 175#define IF_LOGD() IF_LOG(LOG_DEBUG, LOG_TAG) 176#endif 177 178/* 179 * Conditional based on whether the current LOG_TAG is enabled at 180 * info priority. 181 */ 182#ifndef IF_LOGI 183#define IF_LOGI() IF_LOG(LOG_INFO, LOG_TAG) 184#endif 185 186/* 187 * Conditional based on whether the current LOG_TAG is enabled at 188 * warn priority. 189 */ 190#ifndef IF_LOGW 191#define IF_LOGW() IF_LOG(LOG_WARN, LOG_TAG) 192#endif 193 194/* 195 * Conditional based on whether the current LOG_TAG is enabled at 196 * error priority. 197 */ 198#ifndef IF_LOGE 199#define IF_LOGE() IF_LOG(LOG_ERROR, LOG_TAG) 200#endif 201 202 203// --------------------------------------------------------------------- 204 205/* 206 * Simplified macro to send a verbose system log message using the current LOG_TAG. 207 */ 208#ifndef SLOGV 209#if LOG_NDEBUG 210#define SLOGV(...) ((void)0) 211#else 212#define SLOGV(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) 213#endif 214#endif 215 216#define CONDITION(cond) (__builtin_expect((cond)!=0, 0)) 217 218#ifndef SLOGV_IF 219#if LOG_NDEBUG 220#define SLOGV_IF(cond, ...) ((void)0) 221#else 222#define SLOGV_IF(cond, ...) \ 223 ( (CONDITION(cond)) \ 224 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \ 225 : (void)0 ) 226#endif 227#endif 228 229/* 230 * Simplified macro to send a debug system log message using the current LOG_TAG. 231 */ 232#ifndef SLOGD 233#define SLOGD(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) 234#endif 235 236#ifndef SLOGD_IF 237#define SLOGD_IF(cond, ...) \ 238 ( (CONDITION(cond)) \ 239 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \ 240 : (void)0 ) 241#endif 242 243/* 244 * Simplified macro to send an info system log message using the current LOG_TAG. 245 */ 246#ifndef SLOGI 247#define SLOGI(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) 248#endif 249 250#ifndef SLOGI_IF 251#define SLOGI_IF(cond, ...) \ 252 ( (CONDITION(cond)) \ 253 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \ 254 : (void)0 ) 255#endif 256 257/* 258 * Simplified macro to send a warning system log message using the current LOG_TAG. 259 */ 260#ifndef SLOGW 261#define SLOGW(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) 262#endif 263 264#ifndef SLOGW_IF 265#define SLOGW_IF(cond, ...) \ 266 ( (CONDITION(cond)) \ 267 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \ 268 : (void)0 ) 269#endif 270 271/* 272 * Simplified macro to send an error system log message using the current LOG_TAG. 273 */ 274#ifndef SLOGE 275#define SLOGE(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) 276#endif 277 278#ifndef SLOGE_IF 279#define SLOGE_IF(cond, ...) \ 280 ( (CONDITION(cond)) \ 281 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ 282 : (void)0 ) 283#endif 284 285 286 287// --------------------------------------------------------------------- 288 289/* 290 * Log a fatal error. If the given condition fails, this stops program 291 * execution like a normal assertion, but also generating the given message. 292 * It is NOT stripped from release builds. Note that the condition test 293 * is -inverted- from the normal assert() semantics. 294 */ 295#define LOG_ALWAYS_FATAL_IF(cond, ...) \ 296 ( (CONDITION(cond)) \ 297 ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \ 298 : (void)0 ) 299 300#define LOG_ALWAYS_FATAL(...) \ 301 ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) ) 302 303/* 304 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that 305 * are stripped out of release builds. 306 */ 307#if LOG_NDEBUG 308 309#define LOG_FATAL_IF(cond, ...) ((void)0) 310#define LOG_FATAL(...) ((void)0) 311 312#else 313 314#define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__) 315#define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__) 316 317#endif 318 319/* 320 * Assertion that generates a log message when the assertion fails. 321 * Stripped out of release builds. Uses the current LOG_TAG. 322 */ 323#define LOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__) 324//#define LOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond) 325 326// --------------------------------------------------------------------- 327 328/* 329 * Basic log message macro. 330 * 331 * Example: 332 * LOG(LOG_WARN, NULL, "Failed with error %d", errno); 333 * 334 * The second argument may be NULL or "" to indicate the "global" tag. 335 */ 336#ifndef LOG 337#define LOG(priority, tag, ...) \ 338 LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__) 339#endif 340 341/* 342 * Log macro that allows you to specify a number for the priority. 343 */ 344#ifndef LOG_PRI 345#define LOG_PRI(priority, tag, ...) \ 346 android_printLog(priority, tag, __VA_ARGS__) 347#endif 348 349/* 350 * Log macro that allows you to pass in a varargs ("args" is a va_list). 351 */ 352#ifndef LOG_PRI_VA 353#define LOG_PRI_VA(priority, tag, fmt, args) \ 354 android_vprintLog(priority, NULL, tag, fmt, args) 355#endif 356 357/* 358 * Conditional given a desired logging priority and tag. 359 */ 360#ifndef IF_LOG 361#define IF_LOG(priority, tag) \ 362 if (android_testLog(ANDROID_##priority, tag)) 363#endif 364 365// --------------------------------------------------------------------- 366 367/* 368 * Event logging. 369 */ 370 371/* 372 * Event log entry types. These must match up with the declarations in 373 * java/android/android/util/EventLog.java. 374 */ 375typedef enum { 376 EVENT_TYPE_INT = 0, 377 EVENT_TYPE_LONG = 1, 378 EVENT_TYPE_STRING = 2, 379 EVENT_TYPE_LIST = 3, 380} AndroidEventLogType; 381 382 383#define LOG_EVENT_INT(_tag, _value) { \ 384 int intBuf = _value; \ 385 (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, \ 386 sizeof(intBuf)); \ 387 } 388#define LOG_EVENT_LONG(_tag, _value) { \ 389 long long longBuf = _value; \ 390 (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, \ 391 sizeof(longBuf)); \ 392 } 393#define LOG_EVENT_STRING(_tag, _value) \ 394 ((void) 0) /* not implemented -- must combine len with string */ 395/* TODO: something for LIST */ 396 397/* 398 * =========================================================================== 399 * 400 * The stuff in the rest of this file should not be used directly. 401 */ 402 403#define android_printLog(prio, tag, fmt...) \ 404 __android_log_print(prio, tag, fmt) 405 406#define android_vprintLog(prio, cond, tag, fmt...) \ 407 __android_log_vprint(prio, tag, fmt) 408 409/* XXX Macros to work around syntax errors in places where format string 410 * arg is not passed to LOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF 411 * (happens only in debug builds). 412 */ 413 414/* Returns 2nd arg. Used to substitute default value if caller's vararg list 415 * is empty. 416 */ 417#define __android_second(dummy, second, ...) second 418 419/* If passed multiple args, returns ',' followed by all but 1st arg, otherwise 420 * returns nothing. 421 */ 422#define __android_rest(first, ...) , ## __VA_ARGS__ 423 424#define android_printAssert(cond, tag, fmt...) \ 425 __android_log_assert(cond, tag, \ 426 __android_second(0, ## fmt, NULL) __android_rest(fmt)) 427 428#define android_writeLog(prio, tag, text) \ 429 __android_log_write(prio, tag, text) 430 431#define android_bWriteLog(tag, payload, len) \ 432 __android_log_bwrite(tag, payload, len) 433#define android_btWriteLog(tag, type, payload, len) \ 434 __android_log_btwrite(tag, type, payload, len) 435 436// TODO: remove these prototypes and their users 437#define android_testLog(prio, tag) (1) 438#define android_writevLog(vec,num) do{}while(0) 439#define android_write1Log(str,len) do{}while (0) 440#define android_setMinPriority(tag, prio) do{}while(0) 441//#define android_logToCallback(func) do{}while(0) 442#define android_logToFile(tag, file) (0) 443#define android_logToFd(tag, fd) (0) 444 445typedef enum { 446 LOG_ID_MAIN = 0, 447 LOG_ID_RADIO = 1, 448 LOG_ID_EVENTS = 2, 449 LOG_ID_SYSTEM = 3, 450 451 LOG_ID_MAX 452} log_id_t; 453 454/* 455 * Send a simple string to the log. 456 */ 457int __android_log_buf_write(int bufID, int prio, const char *tag, const char *text); 458int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...); 459 460 461#ifdef __cplusplus 462} 463#endif 464 465#endif // _LIBS_CUTILS_LOG_H 466