log.h revision 16210c12bc0a8dab5523289de1e51b6fe8d38053
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#ifndef LOG_ALWAYS_FATAL_IF 296#define LOG_ALWAYS_FATAL_IF(cond, ...) \ 297 ( (CONDITION(cond)) \ 298 ? ((void)android_printAssert(#cond, LOG_TAG, ## __VA_ARGS__)) \ 299 : (void)0 ) 300#endif 301 302#ifndef LOG_ALWAYS_FATAL 303#define LOG_ALWAYS_FATAL(...) \ 304 ( ((void)android_printAssert(NULL, LOG_TAG, ## __VA_ARGS__)) ) 305#endif 306 307/* 308 * Versions of LOG_ALWAYS_FATAL_IF and LOG_ALWAYS_FATAL that 309 * are stripped out of release builds. 310 */ 311#if LOG_NDEBUG 312 313#ifndef LOG_FATAL_IF 314#define LOG_FATAL_IF(cond, ...) ((void)0) 315#endif 316#ifndef LOG_FATAL 317#define LOG_FATAL(...) ((void)0) 318#endif 319 320#else 321 322#ifndef LOG_FATAL_IF 323#define LOG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__) 324#endif 325#ifndef LOG_FATAL 326#define LOG_FATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__) 327#endif 328 329#endif 330 331/* 332 * Assertion that generates a log message when the assertion fails. 333 * Stripped out of release builds. Uses the current LOG_TAG. 334 */ 335#ifndef LOG_ASSERT 336#define LOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ## __VA_ARGS__) 337//#define LOG_ASSERT(cond) LOG_FATAL_IF(!(cond), "Assertion failed: " #cond) 338#endif 339 340// --------------------------------------------------------------------- 341 342/* 343 * Basic log message macro. 344 * 345 * Example: 346 * LOG(LOG_WARN, NULL, "Failed with error %d", errno); 347 * 348 * The second argument may be NULL or "" to indicate the "global" tag. 349 */ 350#ifndef LOG 351#define LOG(priority, tag, ...) \ 352 LOG_PRI(ANDROID_##priority, tag, __VA_ARGS__) 353#endif 354 355/* 356 * Log macro that allows you to specify a number for the priority. 357 */ 358#ifndef LOG_PRI 359#define LOG_PRI(priority, tag, ...) \ 360 android_printLog(priority, tag, __VA_ARGS__) 361#endif 362 363/* 364 * Log macro that allows you to pass in a varargs ("args" is a va_list). 365 */ 366#ifndef LOG_PRI_VA 367#define LOG_PRI_VA(priority, tag, fmt, args) \ 368 android_vprintLog(priority, NULL, tag, fmt, args) 369#endif 370 371/* 372 * Conditional given a desired logging priority and tag. 373 */ 374#ifndef IF_LOG 375#define IF_LOG(priority, tag) \ 376 if (android_testLog(ANDROID_##priority, tag)) 377#endif 378 379// --------------------------------------------------------------------- 380 381/* 382 * Event logging. 383 */ 384 385/* 386 * Event log entry types. These must match up with the declarations in 387 * java/android/android/util/EventLog.java. 388 */ 389typedef enum { 390 EVENT_TYPE_INT = 0, 391 EVENT_TYPE_LONG = 1, 392 EVENT_TYPE_STRING = 2, 393 EVENT_TYPE_LIST = 3, 394} AndroidEventLogType; 395 396 397#ifndef LOG_EVENT_INT 398#define LOG_EVENT_INT(_tag, _value) { \ 399 int intBuf = _value; \ 400 (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, \ 401 sizeof(intBuf)); \ 402 } 403#endif 404#ifndef LOG_EVENT_LONG 405#define LOG_EVENT_LONG(_tag, _value) { \ 406 long long longBuf = _value; \ 407 (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, \ 408 sizeof(longBuf)); \ 409 } 410#endif 411#ifndef LOG_EVENT_STRING 412#define LOG_EVENT_STRING(_tag, _value) \ 413 ((void) 0) /* not implemented -- must combine len with string */ 414#endif 415/* TODO: something for LIST */ 416 417/* 418 * =========================================================================== 419 * 420 * The stuff in the rest of this file should not be used directly. 421 */ 422 423#define android_printLog(prio, tag, fmt...) \ 424 __android_log_print(prio, tag, fmt) 425 426#define android_vprintLog(prio, cond, tag, fmt...) \ 427 __android_log_vprint(prio, tag, fmt) 428 429/* XXX Macros to work around syntax errors in places where format string 430 * arg is not passed to LOG_ASSERT, LOG_ALWAYS_FATAL or LOG_ALWAYS_FATAL_IF 431 * (happens only in debug builds). 432 */ 433 434/* Returns 2nd arg. Used to substitute default value if caller's vararg list 435 * is empty. 436 */ 437#define __android_second(dummy, second, ...) second 438 439/* If passed multiple args, returns ',' followed by all but 1st arg, otherwise 440 * returns nothing. 441 */ 442#define __android_rest(first, ...) , ## __VA_ARGS__ 443 444#define android_printAssert(cond, tag, fmt...) \ 445 __android_log_assert(cond, tag, \ 446 __android_second(0, ## fmt, NULL) __android_rest(fmt)) 447 448#define android_writeLog(prio, tag, text) \ 449 __android_log_write(prio, tag, text) 450 451#define android_bWriteLog(tag, payload, len) \ 452 __android_log_bwrite(tag, payload, len) 453#define android_btWriteLog(tag, type, payload, len) \ 454 __android_log_btwrite(tag, type, payload, len) 455 456// TODO: remove these prototypes and their users 457#define android_testLog(prio, tag) (1) 458#define android_writevLog(vec,num) do{}while(0) 459#define android_write1Log(str,len) do{}while (0) 460#define android_setMinPriority(tag, prio) do{}while(0) 461//#define android_logToCallback(func) do{}while(0) 462#define android_logToFile(tag, file) (0) 463#define android_logToFd(tag, fd) (0) 464 465typedef enum { 466 LOG_ID_MAIN = 0, 467 LOG_ID_RADIO = 1, 468 LOG_ID_EVENTS = 2, 469 LOG_ID_SYSTEM = 3, 470 471 LOG_ID_MAX 472} log_id_t; 473 474/* 475 * Send a simple string to the log. 476 */ 477int __android_log_buf_write(int bufID, int prio, const char *tag, const char *text); 478int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...); 479 480 481#ifdef __cplusplus 482} 483#endif 484 485#endif // _LIBS_CUTILS_LOG_H 486