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 int __android_log_stats_bwrite(int32_t tag, const void* payload, size_t len);
     99 
    100 #define android_bWriteLog(tag, payload, len) \
    101   __android_log_bwrite(tag, payload, len)
    102 #define android_btWriteLog(tag, type, payload, len) \
    103   __android_log_btwrite(tag, type, payload, len)
    104 
    105 /*
    106  * Event log entry types.
    107  */
    108 #ifndef __AndroidEventLogType_defined
    109 #define __AndroidEventLogType_defined
    110 typedef enum {
    111   /* Special markers for android_log_list_element type */
    112   EVENT_TYPE_LIST_STOP = '\n', /* declare end of list  */
    113   EVENT_TYPE_UNKNOWN = '?',    /* protocol error       */
    114 
    115   /* must match with declaration in java/android/android/util/EventLog.java */
    116   EVENT_TYPE_INT = 0,  /* int32_t */
    117   EVENT_TYPE_LONG = 1, /* int64_t */
    118   EVENT_TYPE_STRING = 2,
    119   EVENT_TYPE_LIST = 3,
    120   EVENT_TYPE_FLOAT = 4,
    121 } AndroidEventLogType;
    122 #endif
    123 #define sizeof_AndroidEventLogType sizeof(typeof_AndroidEventLogType)
    124 #define typeof_AndroidEventLogType unsigned char
    125 
    126 #ifndef LOG_EVENT_INT
    127 #define LOG_EVENT_INT(_tag, _value)                                          \
    128   {                                                                          \
    129     int intBuf = _value;                                                     \
    130     (void)android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, sizeof(intBuf)); \
    131   }
    132 #endif
    133 #ifndef LOG_EVENT_LONG
    134 #define LOG_EVENT_LONG(_tag, _value)                                            \
    135   {                                                                             \
    136     long long longBuf = _value;                                                 \
    137     (void)android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, sizeof(longBuf)); \
    138   }
    139 #endif
    140 #ifndef LOG_EVENT_FLOAT
    141 #define LOG_EVENT_FLOAT(_tag, _value)                           \
    142   {                                                             \
    143     float floatBuf = _value;                                    \
    144     (void)android_btWriteLog(_tag, EVENT_TYPE_FLOAT, &floatBuf, \
    145                              sizeof(floatBuf));                 \
    146   }
    147 #endif
    148 #ifndef LOG_EVENT_STRING
    149 #define LOG_EVENT_STRING(_tag, _value) \
    150   (void)__android_log_bswrite(_tag, _value);
    151 #endif
    152 
    153 #ifdef __linux__
    154 
    155 #ifndef __ANDROID_USE_LIBLOG_CLOCK_INTERFACE
    156 #ifndef __ANDROID_API__
    157 #define __ANDROID_USE_LIBLOG_CLOCK_INTERFACE 1
    158 #elif __ANDROID_API__ > 22 /* > Lollipop */
    159 #define __ANDROID_USE_LIBLOG_CLOCK_INTERFACE 1
    160 #else
    161 #define __ANDROID_USE_LIBLOG_CLOCK_INTERFACE 0
    162 #endif
    163 #endif
    164 
    165 #if __ANDROID_USE_LIBLOG_CLOCK_INTERFACE
    166 clockid_t android_log_clockid(void);
    167 #endif
    168 
    169 #endif /* __linux__ */
    170 
    171 /* --------------------------------------------------------------------- */
    172 
    173 #ifndef __ANDROID_USE_LIBLOG_CLOSE_INTERFACE
    174 #ifndef __ANDROID_API__
    175 #define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 1
    176 #elif __ANDROID_API__ > 18 /* > JellyBean */
    177 #define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 1
    178 #else
    179 #define __ANDROID_USE_LIBLOG_CLOSE_INTERFACE 0
    180 #endif
    181 #endif
    182 
    183 #if __ANDROID_USE_LIBLOG_CLOSE_INTERFACE
    184 /*
    185  * Release any logger resources (a new log write will immediately re-acquire)
    186  *
    187  * May be used to clean up File descriptors after a Fork, the resources are
    188  * all O_CLOEXEC so wil self clean on exec().
    189  */
    190 void __android_log_close(void);
    191 #endif
    192 
    193 #ifndef __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE
    194 #ifndef __ANDROID_API__
    195 #define __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE 1
    196 #elif __ANDROID_API__ > 25 /* > OC */
    197 #define __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE 1
    198 #else
    199 #define __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE 0
    200 #endif
    201 #endif
    202 
    203 #if __ANDROID_USE_LIBLOG_RATELIMIT_INTERFACE
    204 
    205 /*
    206  * if last is NULL, caller _must_ provide a consistent value for seconds.
    207  *
    208  * Return -1 if we can not acquire a lock, which below will permit the logging,
    209  * error on allowing a log message through.
    210  */
    211 int __android_log_ratelimit(time_t seconds, time_t* last);
    212 
    213 /*
    214  * Usage:
    215  *
    216  *   // Global default and state
    217  *   IF_ALOG_RATELIMIT() {
    218  *      ALOG*(...);
    219  *   }
    220  *
    221  *   // local state, 10 seconds ratelimit
    222  *   static time_t local_state;
    223  *   IF_ALOG_RATELIMIT_LOCAL(10, &local_state) {
    224  *     ALOG*(...);
    225  *   }
    226  */
    227 
    228 #define IF_ALOG_RATELIMIT() if (__android_log_ratelimit(0, NULL) > 0)
    229 #define IF_ALOG_RATELIMIT_LOCAL(seconds, state) \
    230   if (__android_log_ratelimit(seconds, state) > 0)
    231 
    232 #else
    233 
    234 /* No ratelimiting as API unsupported */
    235 #define IF_ALOG_RATELIMIT() if (1)
    236 #define IF_ALOG_RATELIMIT_LOCAL(...) if (1)
    237 
    238 #endif
    239 
    240 #if defined(__clang__)
    241 #pragma clang diagnostic pop
    242 #endif
    243 
    244 #ifdef __cplusplus
    245 }
    246 #endif
    247 
    248 #endif /* _LIBS_LOG_LOG_H */
    249