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