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