log.h revision c96a44888a50fdc6155cb1a7fa8e37bee00fafc0
1/* 2 * Copyright (C) 2005-2013 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_LOG_LOG_H 29#define _LIBS_LOG_LOG_H 30 31#include <sys/types.h> 32#ifdef HAVE_PTHREADS 33#include <pthread.h> 34#endif 35#include <stdarg.h> 36#include <stdio.h> 37#include <time.h> 38#include <unistd.h> 39#include <log/logd.h> 40#include <log/uio.h> 41 42#ifdef __cplusplus 43extern "C" { 44#endif 45 46// --------------------------------------------------------------------- 47 48/* 49 * Normally we strip ALOGV (VERBOSE messages) from release builds. 50 * You can modify this (for example with "#define LOG_NDEBUG 0" 51 * at the top of your source file) to change that behavior. 52 */ 53#ifndef LOG_NDEBUG 54#ifdef NDEBUG 55#define LOG_NDEBUG 1 56#else 57#define LOG_NDEBUG 0 58#endif 59#endif 60 61/* 62 * This is the local tag used for the following simplified 63 * logging macros. You can change this preprocessor definition 64 * before using the other macros to change the tag. 65 */ 66#ifndef LOG_TAG 67#define LOG_TAG NULL 68#endif 69 70// --------------------------------------------------------------------- 71 72/* 73 * Simplified macro to send a verbose log message using the current LOG_TAG. 74 */ 75#ifndef ALOGV 76#if LOG_NDEBUG 77#define ALOGV(...) ((void)0) 78#else 79#define ALOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) 80#endif 81#endif 82 83#define CONDITION(cond) (__builtin_expect((cond)!=0, 0)) 84 85#ifndef ALOGV_IF 86#if LOG_NDEBUG 87#define ALOGV_IF(cond, ...) ((void)0) 88#else 89#define ALOGV_IF(cond, ...) \ 90 ( (CONDITION(cond)) \ 91 ? ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \ 92 : (void)0 ) 93#endif 94#endif 95 96/* 97 * Simplified macro to send a debug log message using the current LOG_TAG. 98 */ 99#ifndef ALOGD 100#define ALOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) 101#endif 102 103#ifndef ALOGD_IF 104#define ALOGD_IF(cond, ...) \ 105 ( (CONDITION(cond)) \ 106 ? ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \ 107 : (void)0 ) 108#endif 109 110/* 111 * Simplified macro to send an info log message using the current LOG_TAG. 112 */ 113#ifndef ALOGI 114#define ALOGI(...) ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) 115#endif 116 117#ifndef ALOGI_IF 118#define ALOGI_IF(cond, ...) \ 119 ( (CONDITION(cond)) \ 120 ? ((void)ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)) \ 121 : (void)0 ) 122#endif 123 124/* 125 * Simplified macro to send a warning log message using the current LOG_TAG. 126 */ 127#ifndef ALOGW 128#define ALOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) 129#endif 130 131#ifndef ALOGW_IF 132#define ALOGW_IF(cond, ...) \ 133 ( (CONDITION(cond)) \ 134 ? ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)) \ 135 : (void)0 ) 136#endif 137 138/* 139 * Simplified macro to send an error log message using the current LOG_TAG. 140 */ 141#ifndef ALOGE 142#define ALOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) 143#endif 144 145#ifndef ALOGE_IF 146#define ALOGE_IF(cond, ...) \ 147 ( (CONDITION(cond)) \ 148 ? ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ 149 : (void)0 ) 150#endif 151 152// --------------------------------------------------------------------- 153 154/* 155 * Conditional based on whether the current LOG_TAG is enabled at 156 * verbose priority. 157 */ 158#ifndef IF_ALOGV 159#if LOG_NDEBUG 160#define IF_ALOGV() if (false) 161#else 162#define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG) 163#endif 164#endif 165 166/* 167 * Conditional based on whether the current LOG_TAG is enabled at 168 * debug priority. 169 */ 170#ifndef IF_ALOGD 171#define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG) 172#endif 173 174/* 175 * Conditional based on whether the current LOG_TAG is enabled at 176 * info priority. 177 */ 178#ifndef IF_ALOGI 179#define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG) 180#endif 181 182/* 183 * Conditional based on whether the current LOG_TAG is enabled at 184 * warn priority. 185 */ 186#ifndef IF_ALOGW 187#define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG) 188#endif 189 190/* 191 * Conditional based on whether the current LOG_TAG is enabled at 192 * error priority. 193 */ 194#ifndef IF_ALOGE 195#define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG) 196#endif 197 198 199// --------------------------------------------------------------------- 200 201/* 202 * Simplified macro to send a verbose system log message using the current LOG_TAG. 203 */ 204#ifndef SLOGV 205#if LOG_NDEBUG 206#define SLOGV(...) ((void)0) 207#else 208#define SLOGV(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) 209#endif 210#endif 211 212#define CONDITION(cond) (__builtin_expect((cond)!=0, 0)) 213 214#ifndef SLOGV_IF 215#if LOG_NDEBUG 216#define SLOGV_IF(cond, ...) ((void)0) 217#else 218#define SLOGV_IF(cond, ...) \ 219 ( (CONDITION(cond)) \ 220 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \ 221 : (void)0 ) 222#endif 223#endif 224 225/* 226 * Simplified macro to send a debug system log message using the current LOG_TAG. 227 */ 228#ifndef SLOGD 229#define SLOGD(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) 230#endif 231 232#ifndef SLOGD_IF 233#define SLOGD_IF(cond, ...) \ 234 ( (CONDITION(cond)) \ 235 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \ 236 : (void)0 ) 237#endif 238 239/* 240 * Simplified macro to send an info system log message using the current LOG_TAG. 241 */ 242#ifndef SLOGI 243#define SLOGI(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) 244#endif 245 246#ifndef SLOGI_IF 247#define SLOGI_IF(cond, ...) \ 248 ( (CONDITION(cond)) \ 249 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \ 250 : (void)0 ) 251#endif 252 253/* 254 * Simplified macro to send a warning system log message using the current LOG_TAG. 255 */ 256#ifndef SLOGW 257#define SLOGW(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) 258#endif 259 260#ifndef SLOGW_IF 261#define SLOGW_IF(cond, ...) \ 262 ( (CONDITION(cond)) \ 263 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \ 264 : (void)0 ) 265#endif 266 267/* 268 * Simplified macro to send an error system log message using the current LOG_TAG. 269 */ 270#ifndef SLOGE 271#define SLOGE(...) ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) 272#endif 273 274#ifndef SLOGE_IF 275#define SLOGE_IF(cond, ...) \ 276 ( (CONDITION(cond)) \ 277 ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ 278 : (void)0 ) 279#endif 280 281// --------------------------------------------------------------------- 282 283/* 284 * Simplified macro to send a verbose radio log message using the current LOG_TAG. 285 */ 286#ifndef RLOGV 287#if LOG_NDEBUG 288#define RLOGV(...) ((void)0) 289#else 290#define RLOGV(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) 291#endif 292#endif 293 294#define CONDITION(cond) (__builtin_expect((cond)!=0, 0)) 295 296#ifndef RLOGV_IF 297#if LOG_NDEBUG 298#define RLOGV_IF(cond, ...) ((void)0) 299#else 300#define RLOGV_IF(cond, ...) \ 301 ( (CONDITION(cond)) \ 302 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \ 303 : (void)0 ) 304#endif 305#endif 306 307/* 308 * Simplified macro to send a debug radio log message using the current LOG_TAG. 309 */ 310#ifndef RLOGD 311#define RLOGD(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) 312#endif 313 314#ifndef RLOGD_IF 315#define RLOGD_IF(cond, ...) \ 316 ( (CONDITION(cond)) \ 317 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \ 318 : (void)0 ) 319#endif 320 321/* 322 * Simplified macro to send an info radio log message using the current LOG_TAG. 323 */ 324#ifndef RLOGI 325#define RLOGI(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) 326#endif 327 328#ifndef RLOGI_IF 329#define RLOGI_IF(cond, ...) \ 330 ( (CONDITION(cond)) \ 331 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \ 332 : (void)0 ) 333#endif 334 335/* 336 * Simplified macro to send a warning radio log message using the current LOG_TAG. 337 */ 338#ifndef RLOGW 339#define RLOGW(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) 340#endif 341 342#ifndef RLOGW_IF 343#define RLOGW_IF(cond, ...) \ 344 ( (CONDITION(cond)) \ 345 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \ 346 : (void)0 ) 347#endif 348 349/* 350 * Simplified macro to send an error radio log message using the current LOG_TAG. 351 */ 352#ifndef RLOGE 353#define RLOGE(...) ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) 354#endif 355 356#ifndef RLOGE_IF 357#define RLOGE_IF(cond, ...) \ 358 ( (CONDITION(cond)) \ 359 ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ 360 : (void)0 ) 361#endif 362 363 364// --------------------------------------------------------------------- 365 366/* 367 * Log a fatal error. If the given condition fails, this stops program 368 * execution like a normal assertion, but also generating the given message. 369 * It is NOT stripped from release builds. Note that the condition test 370 * is -inverted- from the normal assert() semantics. 371 */ 372#ifndef LOG_ALWAYS_FATAL_IF 373#define LOG_ALWAYS_FATAL_IF(cond, ...) \ 374 ( (CONDITION(cond)) \ 375 ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \ 376 : (void)0 ) 377#endif 378 379#ifndef LOG_ALWAYS_FATAL 380#define LOG_ALWAYS_FATAL(...) \ 381 ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) ) 382#endif 383 384/* 385 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that 386 * are stripped out of release builds. 387 */ 388#if LOG_NDEBUG 389 390#ifndef LOG_FATAL_IF 391#define LOG_FATAL_IF(cond, ...) ((void)0) 392#endif 393#ifndef LOG_FATAL 394#define LOG_FATAL(...) ((void)0) 395#endif 396 397#else 398 399#ifndef LOG_FATAL_IF 400#define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__) 401#endif 402#ifndef LOG_FATAL 403#define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__) 404#endif 405 406#endif 407 408/* 409 * Assertion that generates a log message when the assertion fails. 410 * Stripped out of release builds. Uses the current LOG_TAG. 411 */ 412#ifndef ALOG_ASSERT 413#define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__) 414//#define ALOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond) 415#endif 416 417// --------------------------------------------------------------------- 418 419/* 420 * Basic log message macro. 421 * 422 * Example: 423 * ALOG(LOG_WARN, NULL, "Failed with error %d", errno); 424 * 425 * The second argument may be NULL or "" to indicate the "global" tag. 426 */ 427#ifndef ALOG 428#define ALOG(priority, tag, ...) \ 429 LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__) 430#endif 431 432/* 433 * Log macro that allows you to specify a number for the priority. 434 */ 435#ifndef LOG_PRI 436#define LOG_PRI(priority, tag, ...) \ 437 android_printLog(priority, tag, __VA_ARGS__) 438#endif 439 440/* 441 * Log macro that allows you to pass in a varargs ("args" is a va_list). 442 */ 443#ifndef LOG_PRI_VA 444#define LOG_PRI_VA(priority, tag, fmt, args) \ 445 android_vprintLog(priority, NULL, tag, fmt, args) 446#endif 447 448/* 449 * Conditional given a desired logging priority and tag. 450 */ 451#ifndef IF_ALOG 452#define IF_ALOG(priority, tag) \ 453 if (android_testLog(ANDROID_##priority, tag)) 454#endif 455 456// --------------------------------------------------------------------- 457 458/* 459 * Event logging. 460 */ 461 462/* 463 * Event log entry types. These must match up with the declarations in 464 * java/android/android/util/EventLog.java. 465 */ 466typedef enum { 467 EVENT_TYPE_INT = 0, 468 EVENT_TYPE_LONG = 1, 469 EVENT_TYPE_STRING = 2, 470 EVENT_TYPE_LIST = 3, 471} AndroidEventLogType; 472#define sizeof_AndroidEventLogType sizeof(typeof_AndroidEventLogType) 473#define typeof_AndroidEventLogType unsigned char 474 475#ifndef LOG_EVENT_INT 476#define LOG_EVENT_INT(_tag, _value) { \ 477 int intBuf = _value; \ 478 (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, \ 479 sizeof(intBuf)); \ 480 } 481#endif 482#ifndef LOG_EVENT_LONG 483#define LOG_EVENT_LONG(_tag, _value) { \ 484 long long longBuf = _value; \ 485 (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, \ 486 sizeof(longBuf)); \ 487 } 488#endif 489#ifndef LOG_EVENT_STRING 490#define LOG_EVENT_STRING(_tag, _value) \ 491 ((void) 0) /* not implemented -- must combine len with string */ 492#endif 493/* TODO: something for LIST */ 494 495/* 496 * =========================================================================== 497 * 498 * The stuff in the rest of this file should not be used directly. 499 */ 500 501#define android_printLog(prio, tag, fmt...) \ 502 __android_log_print(prio, tag, fmt) 503 504#define android_vprintLog(prio, cond, tag, fmt...) \ 505 __android_log_vprint(prio, tag, fmt) 506 507/* XXX Macros to work around syntax errors in places where format string 508 * arg is not passed to ALOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF 509 * (happens only in debug builds). 510 */ 511 512/* Returns 2nd arg. Used to substitute default value if caller's vararg list 513 * is empty. 514 */ 515#define __android_second(dummy, second, ...) second 516 517/* If passed multiple args, returns ',' followed by all but 1st arg, otherwise 518 * returns nothing. 519 */ 520#define __android_rest(first, ...) , ## __VA_ARGS__ 521 522#define android_printAssert(cond, tag, fmt...) \ 523 __android_log_assert(cond, tag, \ 524 __android_second(0, ## fmt, NULL) __android_rest(fmt)) 525 526#define android_writeLog(prio, tag, text) \ 527 __android_log_write(prio, tag, text) 528 529#define android_bWriteLog(tag, payload, len) \ 530 __android_log_bwrite(tag, payload, len) 531#define android_btWriteLog(tag, type, payload, len) \ 532 __android_log_btwrite(tag, type, payload, len) 533 534// TODO: remove these prototypes and their users 535#define android_testLog(prio, tag) (1) 536#define android_writevLog(vec,num) do{}while(0) 537#define android_write1Log(str,len) do{}while (0) 538#define android_setMinPriority(tag, prio) do{}while(0) 539//#define android_logToCallback(func) do{}while(0) 540#define android_logToFile(tag, file) (0) 541#define android_logToFd(tag, fd) (0) 542 543typedef enum log_id { 544 LOG_ID_MIN = 0, 545 546 LOG_ID_MAIN = 0, 547 LOG_ID_RADIO = 1, 548 LOG_ID_EVENTS = 2, 549 LOG_ID_SYSTEM = 3, 550 551 LOG_ID_MAX 552} log_id_t; 553#define sizeof_log_id_t sizeof(typeof_log_id_t) 554#define typeof_log_id_t unsigned char 555 556/* 557 * Send a simple string to the log. 558 */ 559int __android_log_buf_write(int bufID, int prio, const char *tag, const char *text); 560int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...); 561 562#ifdef __cplusplus 563} 564#endif 565 566#endif // _LIBS_CUTILS_LOG_H 567