Home | History | Annotate | Download | only in log
      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>
     28 #include <unistd.h>
     29 
     30 #include <android/log.h>
     31 #include <log/log_id.h>
     32 #include <log/log_main.h>
     33 #include <log/log_radio.h>
     34 #include <log/log_read.h>
     35 #include <log/log_safetynet.h>
     36 #include <log/log_system.h>
     37 #include <log/log_time.h>
     38 #include <log/uio.h> /* helper to define iovec for portability */
     39 
     40 #ifdef __cplusplus
     41 extern "C" {
     42 #endif
     43 
     44 /*
     45  * LOG_TAG is the local tag used for the following simplified
     46  * logging macros.  You can change this preprocessor definition
     47  * before using the other macros to change the tag.
     48  */
     49 
     50 #ifndef LOG_TAG
     51 #define LOG_TAG NULL
     52 #endif
     53 
     54 /*
     55  * Normally we strip the effects of ALOGV (VERBOSE messages),
     56  * LOG_FATAL and LOG_FATAL_IF (FATAL assert messages) from the
     57  * release builds be defining NDEBUG.  You can modify this (for
     58  * example with "#define LOG_NDEBUG 0" at the top of your source
     59  * file) to change that behavior.
     60  */
     61 
     62 #ifndef LOG_NDEBUG
     63 #ifdef NDEBUG
     64 #define LOG_NDEBUG 1
     65 #else
     66 #define LOG_NDEBUG 0
     67 #endif
     68 #endif
     69 
     70 /* --------------------------------------------------------------------- */
     71 
     72 /*
     73  * This file uses ", ## __VA_ARGS__" zero-argument token pasting to
     74  * work around issues with debug-only syntax errors in assertions
     75  * that are missing format strings.  See commit
     76  * 19299904343daf191267564fe32e6cd5c165cd42
     77  */
     78 #if defined(__clang__)
     79 #pragma clang diagnostic push
     80 #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
     81 #endif
     82 
     83 /* --------------------------------------------------------------------- */
     84 
     85 /*
     86  * Event logging.
     87  */
     88 
     89 /*
     90  * The following should not be used directly.
     91  */
     92 
     93 int __android_log_bwrite(int32_t tag, const void* payload, size_t len);
     94 int __android_log_btwrite(int32_t tag, char type, const void* payload,
     95                           size_t len);
     96 int __android_log_bswrite(int32_t tag, const char* payload);
     97 
     98 #define android_bWriteLog(tag, payload, len) \
     99   __android_log_bwrite(tag, payload, len)
    100 #define android_btWriteLog(tag, type, payload, len) \
    101   __android_log_btwrite(tag, type, payload, len)
    102 
    103 /*
    104  * Event log entry types.
    105  */
    106 #ifndef __AndroidEventLogType_defined
    107 #define __AndroidEventLogType_defined
    108 typedef enum {
    109   /* Special markers for android_log_list_element type */
    110   EVENT_TYPE_LIST_STOP = '\n', /* declare end of list  */
    111   EVENT_TYPE_UNKNOWN = '?',    /* protocol error       */
    112 
    113   /* must match with declaration in java/android/android/util/EventLog.java */
    114   EVENT_TYPE_INT = 0,  /* int32_t */
    115   EVENT_TYPE_LONG = 1, /* int64_t */
    116   EVENT_TYPE_STRING = 2,
    117   EVENT_TYPE_LIST = 3,
    118   EVENT_TYPE_FLOAT = 4,
    119 } AndroidEventLogType;
    120 #endif
    121 #define sizeof_AndroidEventLogType sizeof(typeof_AndroidEventLogType)
    122 #define typeof_AndroidEventLogType unsigned char
    123 
    124 #ifndef LOG_EVENT_INT
    125 #define LOG_EVENT_INT(_tag, _value)                                          \
    126   {                                                                          \
    127     int intBuf = _value;                                                     \
    128     (void)android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, sizeof(intBuf)); \
    129   }
    130 #endif
    131 #ifndef LOG_EVENT_LONG
    132 #define LOG_EVENT_LONG(_tag, _value)                                            \
    133   {                                                                             \
    134     long long longBuf = _value;                                                 \
    135     (void)android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, sizeof(longBuf)); \
    136   }
    137 #endif
    138 #ifndef LOG_EVENT_FLOAT
    139 #define LOG_EVENT_FLOAT(_tag, _value)                           \
    140   {                                                             \
    141     float floatBuf = _value;                                    \
    142     (void)android_btWriteLog(_tag, EVENT_TYPE_FLOAT, &floatBuf, \
    143                              sizeof(floatBuf));                 \
    144   }
    145 #endif
    146 #ifndef LOG_EVENT_STRING
    147 #define LOG_EVENT_STRING(_tag, _value) \
    148   (void)__android_log_bswrite(_tag, _value);
    149 #endif
    150 
    151 #ifdef __linux__
    152 
    153 #ifndef __ANDROID_USE_LIBLOG_CLOCK_INTERFACE
    154 #ifndef __ANDROID_API__
    155 #define __ANDROID_USE_LIBLOG_CLOCK_INTERFACE 1
    156 #elif __ANDROID_API__ > 22 /* > Lollipop */
    157 #define __ANDROID_USE_LIBLOG_CLOCK_INTERFACE 1
    158 #else
    159 #define __ANDROID_USE_LIBLOG_CLOCK_INTERFACE 0
    160 #endif
    161 #endif
    162 
    163 #if __ANDROID_USE_LIBLOG_CLOCK_INTERFACE
    164 clockid_t android_log_clockid(void);
    165 #endif
    166 
    167 #endif /* __linux__ */
    168 
    169 /* --------------------------------------------------------------------- */
    170 
    171 #ifndef __ANDROID_USE_LIBLOG_CLOSE_INTERFACE
    172 #ifndef __ANDROID_API__
    173 #define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 1
    174 #elif __ANDROID_API__ > 18 /* > JellyBean */
    175 #define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 1
    176 #else
    177 #define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 0
    178 #endif
    179 #endif
    180 
    181 #if __ANDROID_USE_LIBLOG_CLOSE_INTERFACE
    182 /*
    183  * Release any logger resources (a new log write will immediately re-acquire)
    184  *
    185  * May be used to clean up File descriptors after a Fork, the resources are
    186  * all O_CLOEXEC so wil self clean on exec().
    187  */
    188 void __android_log_close(void);
    189 #endif
    190 
    191 #ifndef __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE
    192 #ifndef __ANDROID_API__
    193 #define __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE 1
    194 #elif __ANDROID_API__ > 25 /* > OC */
    195 #define __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE 1
    196 #else
    197 #define __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE 0
    198 #endif
    199 #endif
    200 
    201 #if __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE
    202 
    203 /*
    204  * if last is NULL, caller _must_ provide a consistent value for seconds.
    205  *
    206  * Return -1 if we can not acquire a lock, which below will permit the logging,
    207  * error on allowing a log message through.
    208  */
    209 int __android_log_ratelimit(time_t seconds, time_t* last);
    210 
    211 /*
    212  * Usage:
    213  *
    214  *   // Global default and state
    215  *   IF_ALOG_RATELIMIT() {
    216  *      ALOG*(...);
    217  *   }
    218  *
    219  *   // local state, 10 seconds ratelimit
    220  *   static time_t local_state;
    221  *   IF_ALOG_RATELIMIT_LOCAL(10, &local_state) {
    222  *     ALOG*(...);
    223  *   }
    224  */
    225 
    226 #define IF_ALOG_RATELIMIT() if (__android_log_ratelimit(0, NULL) > 0)
    227 #define IF_ALOG_RATELIMIT_LOCAL(seconds, state) \
    228   if (__android_log_ratelimit(seconds, state) > 0)
    229 
    230 #else
    231 
    232 /* No ratelimiting as API unsupported */
    233 #define IF_ALOG_RATELIMIT() if (1)
    234 #define IF_ALOG_RATELIMIT_LOCAL(...) if (1)
    235 
    236 #endif
    237 
    238 #if defined(__clang__)
    239 #pragma clang diagnostic pop
    240 #endif
    241 
    242 #ifdef __cplusplus
    243 }
    244 #endif
    245 
    246 #endif /* _LIBS_LOG_LOG_H */
    247