Mark Salyzyn | 749a298 | 2016-10-11 07:34:52 -0700 | [diff] [blame^] | 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 | #ifndef _LIBS_LOG_LOG_H |
| 18 | #define _LIBS_LOG_LOG_H |
| 19 | |
| 20 | /* Too many in the ecosystem assume these are included */ |
| 21 | #if !defined(_WIN32) |
| 22 | #include <pthread.h> |
| 23 | #endif |
| 24 | #include <stdint.h> /* uint16_t, int32_t */ |
| 25 | #include <stdio.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <time.h> /* clock_gettime */ |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <log/uio.h> /* helper to define iovec for portability */ |
| 31 | |
Mark Salyzyn | a166708 | 2016-09-28 09:58:56 -0700 | [diff] [blame] | 32 | #include <android/log.h> |
Mark Salyzyn | 749a298 | 2016-10-11 07:34:52 -0700 | [diff] [blame^] | 33 | |
| 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
| 38 | /* |
| 39 | * LOG_TAG is the local tag used for the following simplified |
| 40 | * logging macros. You can change this preprocessor definition |
| 41 | * before using the other macros to change the tag. |
| 42 | */ |
| 43 | |
| 44 | #ifndef LOG_TAG |
| 45 | #define LOG_TAG NULL |
| 46 | #endif |
| 47 | |
| 48 | /* --------------------------------------------------------------------- */ |
| 49 | |
| 50 | /* |
| 51 | * This file uses ", ## __VA_ARGS__" zero-argument token pasting to |
| 52 | * work around issues with debug-only syntax errors in assertions |
| 53 | * that are missing format strings. See commit |
| 54 | * 19299904343daf191267564fe32e6cd5c165cd42 |
| 55 | */ |
| 56 | #if defined(__clang__) |
| 57 | #pragma clang diagnostic push |
| 58 | #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" |
| 59 | #endif |
| 60 | |
| 61 | /* |
| 62 | * Send a simple string to the log. |
| 63 | */ |
| 64 | int __android_log_buf_write(int bufID, int prio, const char* tag, const char* text); |
| 65 | int __android_log_buf_print(int bufID, int prio, const char* tag, const char* fmt, ...) |
| 66 | #if defined(__GNUC__) |
| 67 | __attribute__((__format__(printf, 4, 5))) |
| 68 | #endif |
| 69 | ; |
| 70 | |
| 71 | /* |
| 72 | * Simplified macro to send a verbose system log message using current LOG_TAG. |
| 73 | */ |
| 74 | #ifndef SLOGV |
| 75 | #define __SLOGV(...) \ |
| 76 | ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) |
| 77 | #if LOG_NDEBUG |
| 78 | #define SLOGV(...) do { if (0) { __SLOGV(__VA_ARGS__); } } while (0) |
| 79 | #else |
| 80 | #define SLOGV(...) __SLOGV(__VA_ARGS__) |
| 81 | #endif |
| 82 | #endif |
| 83 | |
| 84 | #ifndef SLOGV_IF |
| 85 | #if LOG_NDEBUG |
| 86 | #define SLOGV_IF(cond, ...) ((void)0) |
| 87 | #else |
| 88 | #define SLOGV_IF(cond, ...) \ |
| 89 | ( (__predict_false(cond)) \ |
| 90 | ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \ |
| 91 | : (void)0 ) |
| 92 | #endif |
| 93 | #endif |
| 94 | |
| 95 | /* |
| 96 | * Simplified macro to send a debug system log message using current LOG_TAG. |
| 97 | */ |
| 98 | #ifndef SLOGD |
| 99 | #define SLOGD(...) \ |
| 100 | ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) |
| 101 | #endif |
| 102 | |
| 103 | #ifndef SLOGD_IF |
| 104 | #define SLOGD_IF(cond, ...) \ |
| 105 | ( (__predict_false(cond)) \ |
| 106 | ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \ |
| 107 | : (void)0 ) |
| 108 | #endif |
| 109 | |
| 110 | /* |
| 111 | * Simplified macro to send an info system log message using current LOG_TAG. |
| 112 | */ |
| 113 | #ifndef SLOGI |
| 114 | #define SLOGI(...) \ |
| 115 | ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) |
| 116 | #endif |
| 117 | |
| 118 | #ifndef SLOGI_IF |
| 119 | #define SLOGI_IF(cond, ...) \ |
| 120 | ( (__predict_false(cond)) \ |
| 121 | ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \ |
| 122 | : (void)0 ) |
| 123 | #endif |
| 124 | |
| 125 | /* |
| 126 | * Simplified macro to send a warning system log message using current LOG_TAG. |
| 127 | */ |
| 128 | #ifndef SLOGW |
| 129 | #define SLOGW(...) \ |
| 130 | ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) |
| 131 | #endif |
| 132 | |
| 133 | #ifndef SLOGW_IF |
| 134 | #define SLOGW_IF(cond, ...) \ |
| 135 | ( (__predict_false(cond)) \ |
| 136 | ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \ |
| 137 | : (void)0 ) |
| 138 | #endif |
| 139 | |
| 140 | /* |
| 141 | * Simplified macro to send an error system log message using current LOG_TAG. |
| 142 | */ |
| 143 | #ifndef SLOGE |
| 144 | #define SLOGE(...) \ |
| 145 | ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) |
| 146 | #endif |
| 147 | |
| 148 | #ifndef SLOGE_IF |
| 149 | #define SLOGE_IF(cond, ...) \ |
| 150 | ( (__predict_false(cond)) \ |
| 151 | ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ |
| 152 | : (void)0 ) |
| 153 | #endif |
| 154 | |
| 155 | /* --------------------------------------------------------------------- */ |
| 156 | |
| 157 | /* |
| 158 | * Simplified macro to send a verbose radio log message using current LOG_TAG. |
| 159 | */ |
| 160 | #ifndef RLOGV |
| 161 | #define __RLOGV(...) \ |
| 162 | ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) |
| 163 | #if LOG_NDEBUG |
| 164 | #define RLOGV(...) do { if (0) { __RLOGV(__VA_ARGS__); } } while (0) |
| 165 | #else |
| 166 | #define RLOGV(...) __RLOGV(__VA_ARGS__) |
| 167 | #endif |
| 168 | #endif |
| 169 | |
| 170 | #ifndef RLOGV_IF |
| 171 | #if LOG_NDEBUG |
| 172 | #define RLOGV_IF(cond, ...) ((void)0) |
| 173 | #else |
| 174 | #define RLOGV_IF(cond, ...) \ |
| 175 | ( (__predict_false(cond)) \ |
| 176 | ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)) \ |
| 177 | : (void)0 ) |
| 178 | #endif |
| 179 | #endif |
| 180 | |
| 181 | /* |
| 182 | * Simplified macro to send a debug radio log message using current LOG_TAG. |
| 183 | */ |
| 184 | #ifndef RLOGD |
| 185 | #define RLOGD(...) \ |
| 186 | ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) |
| 187 | #endif |
| 188 | |
| 189 | #ifndef RLOGD_IF |
| 190 | #define RLOGD_IF(cond, ...) \ |
| 191 | ( (__predict_false(cond)) \ |
| 192 | ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)) \ |
| 193 | : (void)0 ) |
| 194 | #endif |
| 195 | |
| 196 | /* |
| 197 | * Simplified macro to send an info radio log message using current LOG_TAG. |
| 198 | */ |
| 199 | #ifndef RLOGI |
| 200 | #define RLOGI(...) \ |
| 201 | ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) |
| 202 | #endif |
| 203 | |
| 204 | #ifndef RLOGI_IF |
| 205 | #define RLOGI_IF(cond, ...) \ |
| 206 | ( (__predict_false(cond)) \ |
| 207 | ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)) \ |
| 208 | : (void)0 ) |
| 209 | #endif |
| 210 | |
| 211 | /* |
| 212 | * Simplified macro to send a warning radio log message using current LOG_TAG. |
| 213 | */ |
| 214 | #ifndef RLOGW |
| 215 | #define RLOGW(...) \ |
| 216 | ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) |
| 217 | #endif |
| 218 | |
| 219 | #ifndef RLOGW_IF |
| 220 | #define RLOGW_IF(cond, ...) \ |
| 221 | ( (__predict_false(cond)) \ |
| 222 | ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)) \ |
| 223 | : (void)0 ) |
| 224 | #endif |
| 225 | |
| 226 | /* |
| 227 | * Simplified macro to send an error radio log message using current LOG_TAG. |
| 228 | */ |
| 229 | #ifndef RLOGE |
| 230 | #define RLOGE(...) \ |
| 231 | ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) |
| 232 | #endif |
| 233 | |
| 234 | #ifndef RLOGE_IF |
| 235 | #define RLOGE_IF(cond, ...) \ |
| 236 | ( (__predict_false(cond)) \ |
| 237 | ? ((void)__android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)) \ |
| 238 | : (void)0 ) |
| 239 | #endif |
| 240 | |
| 241 | /* --------------------------------------------------------------------- */ |
| 242 | |
| 243 | /* |
| 244 | * Event logging. |
| 245 | */ |
| 246 | |
| 247 | /* |
| 248 | * The following should not be used directly. |
| 249 | */ |
| 250 | |
| 251 | int __android_log_bwrite(int32_t tag, const void* payload, size_t len); |
| 252 | int __android_log_btwrite(int32_t tag, char type, const void* payload, |
| 253 | size_t len); |
| 254 | int __android_log_bswrite(int32_t tag, const char* payload); |
| 255 | |
| 256 | #define android_bWriteLog(tag, payload, len) \ |
| 257 | __android_log_bwrite(tag, payload, len) |
| 258 | #define android_btWriteLog(tag, type, payload, len) \ |
| 259 | __android_log_btwrite(tag, type, payload, len) |
| 260 | |
| 261 | /* |
| 262 | * Event log entry types. |
| 263 | */ |
| 264 | #ifndef __AndroidEventLogType_defined |
| 265 | #define __AndroidEventLogType_defined |
| 266 | typedef enum { |
| 267 | /* Special markers for android_log_list_element type */ |
| 268 | EVENT_TYPE_LIST_STOP = '\n', /* declare end of list */ |
| 269 | EVENT_TYPE_UNKNOWN = '?', /* protocol error */ |
| 270 | |
| 271 | /* must match with declaration in java/android/android/util/EventLog.java */ |
| 272 | EVENT_TYPE_INT = 0, /* int32_t */ |
| 273 | EVENT_TYPE_LONG = 1, /* int64_t */ |
| 274 | EVENT_TYPE_STRING = 2, |
| 275 | EVENT_TYPE_LIST = 3, |
| 276 | EVENT_TYPE_FLOAT = 4, |
| 277 | } AndroidEventLogType; |
| 278 | #endif |
| 279 | #define sizeof_AndroidEventLogType sizeof(typeof_AndroidEventLogType) |
| 280 | #define typeof_AndroidEventLogType unsigned char |
| 281 | |
| 282 | #ifndef LOG_EVENT_INT |
| 283 | #define LOG_EVENT_INT(_tag, _value) { \ |
| 284 | int intBuf = _value; \ |
| 285 | (void) android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, \ |
| 286 | sizeof(intBuf)); \ |
| 287 | } |
| 288 | #endif |
| 289 | #ifndef LOG_EVENT_LONG |
| 290 | #define LOG_EVENT_LONG(_tag, _value) { \ |
| 291 | long long longBuf = _value; \ |
| 292 | (void) android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, \ |
| 293 | sizeof(longBuf)); \ |
| 294 | } |
| 295 | #endif |
| 296 | #ifndef LOG_EVENT_FLOAT |
| 297 | #define LOG_EVENT_FLOAT(_tag, _value) { \ |
| 298 | float floatBuf = _value; \ |
| 299 | (void) android_btWriteLog(_tag, EVENT_TYPE_FLOAT, &floatBuf, \ |
| 300 | sizeof(floatBuf)); \ |
| 301 | } |
| 302 | #endif |
| 303 | #ifndef LOG_EVENT_STRING |
| 304 | #define LOG_EVENT_STRING(_tag, _value) \ |
| 305 | (void) __android_log_bswrite(_tag, _value); |
| 306 | #endif |
| 307 | |
| 308 | #ifndef log_id_t_defined |
| 309 | #define log_id_t_defined |
| 310 | typedef enum log_id { |
| 311 | LOG_ID_MIN = 0, |
| 312 | |
| 313 | LOG_ID_MAIN = 0, |
| 314 | LOG_ID_RADIO = 1, |
| 315 | LOG_ID_EVENTS = 2, |
| 316 | LOG_ID_SYSTEM = 3, |
| 317 | LOG_ID_CRASH = 4, |
| 318 | LOG_ID_SECURITY = 5, |
| 319 | LOG_ID_KERNEL = 6, /* place last, third-parties can not use it */ |
| 320 | |
| 321 | LOG_ID_MAX |
| 322 | } log_id_t; |
| 323 | #endif |
| 324 | #define sizeof_log_id_t sizeof(typeof_log_id_t) |
| 325 | #define typeof_log_id_t unsigned char |
| 326 | |
| 327 | /* --------------------------------------------------------------------- */ |
| 328 | |
| 329 | #ifndef __ANDROID_USE_LIBLOG_EVENT_INTERFACE |
| 330 | #ifndef __ANDROID_API__ |
| 331 | #define __ANDROID_USE_LIBLOG_EVENT_INTERFACE 1 |
| 332 | #elif __ANDROID_API__ > 23 /* > Marshmallow */ |
| 333 | #define __ANDROID_USE_LIBLOG_EVENT_INTERFACE 1 |
| 334 | #else |
| 335 | #define __ANDROID_USE_LIBLOG_EVENT_INTERFACE 0 |
| 336 | #endif |
| 337 | #endif |
| 338 | |
| 339 | #if __ANDROID_USE_LIBLOG_EVENT_INTERFACE |
| 340 | |
| 341 | /* For manipulating lists of events. */ |
| 342 | |
| 343 | #define ANDROID_MAX_LIST_NEST_DEPTH 8 |
| 344 | |
| 345 | /* |
| 346 | * The opaque context used to manipulate lists of events. |
| 347 | */ |
| 348 | #ifndef __android_log_context_defined |
| 349 | #define __android_log_context_defined |
| 350 | typedef struct android_log_context_internal* android_log_context; |
| 351 | #endif |
| 352 | |
| 353 | /* |
| 354 | * Elements returned when reading a list of events. |
| 355 | */ |
| 356 | #ifndef __android_log_list_element_defined |
| 357 | #define __android_log_list_element_defined |
| 358 | typedef struct { |
| 359 | AndroidEventLogType type; |
| 360 | uint16_t complete; |
| 361 | uint16_t len; |
| 362 | union { |
| 363 | int32_t int32; |
| 364 | int64_t int64; |
| 365 | char* string; |
| 366 | float float32; |
| 367 | } data; |
| 368 | } android_log_list_element; |
| 369 | #endif |
| 370 | |
| 371 | /* |
| 372 | * Creates a context associated with an event tag to write elements to |
| 373 | * the list of events. |
| 374 | */ |
| 375 | android_log_context create_android_logger(uint32_t tag); |
| 376 | |
| 377 | /* All lists must be braced by a begin and end call */ |
| 378 | /* |
| 379 | * NB: If the first level braces are missing when specifying multiple |
| 380 | * elements, we will manufacturer a list to embrace it for your API |
| 381 | * convenience. For a single element, it will remain solitary. |
| 382 | */ |
| 383 | int android_log_write_list_begin(android_log_context ctx); |
| 384 | int android_log_write_list_end(android_log_context ctx); |
| 385 | |
| 386 | int android_log_write_int32(android_log_context ctx, int32_t value); |
| 387 | int android_log_write_int64(android_log_context ctx, int64_t value); |
| 388 | int android_log_write_string8(android_log_context ctx, const char* value); |
| 389 | int android_log_write_string8_len(android_log_context ctx, |
| 390 | const char* value, size_t maxlen); |
| 391 | int android_log_write_float32(android_log_context ctx, float value); |
| 392 | |
| 393 | /* Submit the composed list context to the specified logger id */ |
| 394 | /* NB: LOG_ID_EVENTS and LOG_ID_SECURITY only valid binary buffers */ |
| 395 | int android_log_write_list(android_log_context ctx, log_id_t id); |
| 396 | |
| 397 | /* |
| 398 | * Creates a context from a raw buffer representing a list of events to be read. |
| 399 | */ |
| 400 | android_log_context create_android_log_parser(const char* msg, size_t len); |
| 401 | |
| 402 | android_log_list_element android_log_read_next(android_log_context ctx); |
| 403 | android_log_list_element android_log_peek_next(android_log_context ctx); |
| 404 | |
| 405 | /* Finished with reader or writer context */ |
| 406 | int android_log_destroy(android_log_context* ctx); |
| 407 | |
| 408 | #endif /* __ANDROID_USE_LIBLOG_EVENT_INTERFACE */ |
| 409 | |
| 410 | /* --------------------------------------------------------------------- */ |
| 411 | |
| 412 | #ifndef _ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE |
| 413 | #ifndef __ANDROID_API__ |
| 414 | #define __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE 1 |
| 415 | #elif __ANDROID_API__ > 22 /* > Lollipop */ |
| 416 | #define __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE 1 |
| 417 | #else |
| 418 | #define __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE 0 |
| 419 | #endif |
| 420 | #endif |
| 421 | |
| 422 | #if __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE |
| 423 | |
| 424 | #define android_errorWriteLog(tag, subTag) \ |
| 425 | __android_log_error_write(tag, subTag, -1, NULL, 0) |
| 426 | |
| 427 | #define android_errorWriteWithInfoLog(tag, subTag, uid, data, dataLen) \ |
| 428 | __android_log_error_write(tag, subTag, uid, data, dataLen) |
| 429 | |
| 430 | int __android_log_error_write(int tag, const char* subTag, int32_t uid, |
| 431 | const char* data, uint32_t dataLen); |
| 432 | |
| 433 | #endif /* __ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE */ |
| 434 | |
| 435 | /* --------------------------------------------------------------------- */ |
| 436 | |
| 437 | #ifndef __ANDROID_USE_LIBLOG_CLOSE_INTERFACE |
| 438 | #ifndef __ANDROID_API__ |
| 439 | #define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 1 |
| 440 | #elif __ANDROID_API__ > 18 /* > JellyBean */ |
| 441 | #define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 1 |
| 442 | #else |
| 443 | #define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 0 |
| 444 | #endif |
| 445 | #endif |
| 446 | |
| 447 | #if __ANDROID_USE_LIBLOG_CLOSE_INTERFACE |
| 448 | /* |
| 449 | * Release any logger resources (a new log write will immediately re-acquire) |
| 450 | * |
| 451 | * May be used to clean up File descriptors after a Fork, the resources are |
| 452 | * all O_CLOEXEC so wil self clean on exec(). |
| 453 | */ |
| 454 | void __android_log_close(); |
| 455 | #endif |
| 456 | |
| 457 | #if defined(__clang__) |
| 458 | #pragma clang diagnostic pop |
| 459 | #endif |
| 460 | |
| 461 | #ifdef __cplusplus |
| 462 | } |
| 463 | #endif |
| 464 | |
| 465 | #endif /* _LIBS_LOG_LOG_H */ |