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 #pragma once
18 
19 /* Too many in the ecosystem assume these are included */
20 #if !defined(_WIN32)
21 #include <pthread.h>
22 #endif
23 #include <stdint.h> /* uint16_t, int32_t */
24 #include <stdio.h>
25 #include <time.h>
26 #include <unistd.h>
27 
28 #include <android/log.h>
29 #include <log/log_id.h>
30 #include <log/log_main.h>
31 #include <log/log_radio.h>
32 #include <log/log_safetynet.h>
33 #include <log/log_system.h>
34 #include <log/log_time.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  * LOG_TAG is the local tag used for the following simplified
42  * logging macros.  You can change this preprocessor definition
43  * before using the other macros to change the tag.
44  */
45 
46 #ifndef LOG_TAG
47 #define LOG_TAG NULL
48 #endif
49 
50 /*
51  * Normally we strip the effects of ALOGV (VERBOSE messages),
52  * LOG_FATAL and LOG_FATAL_IF (FATAL assert messages) from the
53  * release builds be defining NDEBUG.  You can modify this (for
54  * example with "#define LOG_NDEBUG 0" at the top of your source
55  * file) to change that behavior.
56  */
57 
58 #ifndef LOG_NDEBUG
59 #ifdef NDEBUG
60 #define LOG_NDEBUG 1
61 #else
62 #define LOG_NDEBUG 0
63 #endif
64 #endif
65 
66 /*
67  * The maximum size of the log entry payload that can be
68  * written to the logger. An attempt to write more than
69  * this amount will result in a truncated log entry.
70  */
71 #define LOGGER_ENTRY_MAX_PAYLOAD 4068
72 
73 /*
74  * Event logging.
75  */
76 
77 /*
78  * The following should not be used directly.
79  */
80 
81 int __android_log_bwrite(int32_t tag, const void* payload, size_t len);
82 int __android_log_btwrite(int32_t tag, char type, const void* payload,
83                           size_t len);
84 int __android_log_bswrite(int32_t tag, const char* payload);
85 
86 int __android_log_stats_bwrite(int32_t tag, const void* payload, size_t len);
87 
88 #define android_bWriteLog(tag, payload, len) \
89   __android_log_bwrite(tag, payload, len)
90 #define android_btWriteLog(tag, type, payload, len) \
91   __android_log_btwrite(tag, type, payload, len)
92 
93 /*
94  * Event log entry types.
95  */
96 typedef enum {
97   /* Special markers for android_log_list_element type */
98   EVENT_TYPE_LIST_STOP = '\n', /* declare end of list  */
99   EVENT_TYPE_UNKNOWN = '?',    /* protocol error       */
100 
101   /* must match with declaration in java/android/android/util/EventLog.java */
102   EVENT_TYPE_INT = 0,  /* int32_t */
103   EVENT_TYPE_LONG = 1, /* int64_t */
104   EVENT_TYPE_STRING = 2,
105   EVENT_TYPE_LIST = 3,
106   EVENT_TYPE_FLOAT = 4,
107 } AndroidEventLogType;
108 
109 #ifndef LOG_EVENT_INT
110 #define LOG_EVENT_INT(_tag, _value)                                          \
111   {                                                                          \
112     int intBuf = _value;                                                     \
113     (void)android_btWriteLog(_tag, EVENT_TYPE_INT, &intBuf, sizeof(intBuf)); \
114   }
115 #endif
116 #ifndef LOG_EVENT_LONG
117 #define LOG_EVENT_LONG(_tag, _value)                                            \
118   {                                                                             \
119     long long longBuf = _value;                                                 \
120     (void)android_btWriteLog(_tag, EVENT_TYPE_LONG, &longBuf, sizeof(longBuf)); \
121   }
122 #endif
123 #ifndef LOG_EVENT_FLOAT
124 #define LOG_EVENT_FLOAT(_tag, _value)                           \
125   {                                                             \
126     float floatBuf = _value;                                    \
127     (void)android_btWriteLog(_tag, EVENT_TYPE_FLOAT, &floatBuf, \
128                              sizeof(floatBuf));                 \
129   }
130 #endif
131 #ifndef LOG_EVENT_STRING
132 #define LOG_EVENT_STRING(_tag, _value) \
133   (void)__android_log_bswrite(_tag, _value);
134 #endif
135 
136 /* --------------------------------------------------------------------- */
137 
138 /*
139  * Release any logger resources (a new log write will immediately re-acquire)
140  *
141  * This is specifically meant to be used by Zygote to close open file descriptors after fork()
142  * and before specialization.  O_CLOEXEC is used on file descriptors, so they will be closed upon
143  * exec() in normal use cases.
144  *
145  * Note that this is not safe to call from a multi-threaded program.
146  */
147 void __android_log_close(void);
148 
149 #ifdef __cplusplus
150 }
151 #endif
152