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