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