blob: 5d174ae03412d4d09a7c5471792e3623fba6f8be [file] [log] [blame]
Yao Chenb13a1022018-05-07 16:57:13 -07001/*
2 * Copyright (C) 2018, 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 ANDROID_STATS_LOG_STATS_EVENT_LIST_H
18#define ANDROID_STATS_LOG_STATS_EVENT_LIST_H
19
20#include <log/log_event_list.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25void reset_log_context(android_log_context ctx);
26int write_to_logger(android_log_context context, log_id_t id);
27
28#ifdef __cplusplus
29}
30#endif
31
32#ifdef __cplusplus
33/**
34 * A copy of android_log_event_list class.
35 *
36 * android_log_event_list is going to be deprecated soon, so copy it here to
37 * avoid creating dependency on upstream code. TODO(b/78304629): Rewrite this
38 * code.
39 */
40class stats_event_list {
41 private:
42 android_log_context ctx;
43 int ret;
44
45 stats_event_list(const stats_event_list&) = delete;
46 void operator=(const stats_event_list&) = delete;
47
48 public:
49 explicit stats_event_list(int tag) : ret(0) {
50 ctx = create_android_logger(static_cast<uint32_t>(tag));
51 }
52 explicit stats_event_list(log_msg& log_msg) : ret(0) {
53 ctx = create_android_log_parser(log_msg.msg() + sizeof(uint32_t),
54 log_msg.entry.len - sizeof(uint32_t));
55 }
56 ~stats_event_list() { android_log_destroy(&ctx); }
57
58 int close() {
59 int retval = android_log_destroy(&ctx);
60 if (retval < 0) {
61 ret = retval;
62 }
63 return retval;
64 }
65
66 /* To allow above C calls to use this class as parameter */
67 operator android_log_context() const { return ctx; }
68
69 /* return errors or transmit status */
70 int status() const { return ret; }
71
72 int begin() {
73 int retval = android_log_write_list_begin(ctx);
74 if (retval < 0) {
75 ret = retval;
76 }
77 return ret;
78 }
79 int end() {
80 int retval = android_log_write_list_end(ctx);
81 if (retval < 0) {
82 ret = retval;
83 }
84 return ret;
85 }
86
87 stats_event_list& operator<<(int32_t value) {
88 int retval = android_log_write_int32(ctx, value);
89 if (retval < 0) {
90 ret = retval;
91 }
92 return *this;
93 }
94
95 stats_event_list& operator<<(uint32_t value) {
96 int retval = android_log_write_int32(ctx, static_cast<int32_t>(value));
97 if (retval < 0) {
98 ret = retval;
99 }
100 return *this;
101 }
102
103 stats_event_list& operator<<(bool value) {
104 int retval = android_log_write_int32(ctx, value ? 1 : 0);
105 if (retval < 0) {
106 ret = retval;
107 }
108 return *this;
109 }
110
111 stats_event_list& operator<<(int64_t value) {
112 int retval = android_log_write_int64(ctx, value);
113 if (retval < 0) {
114 ret = retval;
115 }
116 return *this;
117 }
118
119 stats_event_list& operator<<(uint64_t value) {
120 int retval = android_log_write_int64(ctx, static_cast<int64_t>(value));
121 if (retval < 0) {
122 ret = retval;
123 }
124 return *this;
125 }
126
127 stats_event_list& operator<<(const char* value) {
128 int retval = android_log_write_string8(ctx, value);
129 if (retval < 0) {
130 ret = retval;
131 }
132 return *this;
133 }
134
135#if defined(_USING_LIBCXX)
136 stats_event_list& operator<<(const std::string& value) {
137 int retval = android_log_write_string8_len(ctx, value.data(), value.length());
138 if (retval < 0) {
139 ret = retval;
140 }
141 return *this;
142 }
143#endif
144
145 stats_event_list& operator<<(float value) {
146 int retval = android_log_write_float32(ctx, value);
147 if (retval < 0) {
148 ret = retval;
149 }
150 return *this;
151 }
152
153 int write(log_id_t id = LOG_ID_EVENTS) {
154 /* facilitate -EBUSY retry */
155 if ((ret == -EBUSY) || (ret > 0)) {
156 ret = 0;
157 }
158 int retval = write_to_logger(ctx, id);
159 /* existing errors trump transmission errors */
160 if (!ret) {
161 ret = retval;
162 }
163 return ret;
164 }
165
166 /*
167 * Append<Type> methods removes any integer promotion
168 * confusion, and adds access to string with length.
169 * Append methods are also added for all types for
170 * convenience.
171 */
172
173 bool AppendInt(int32_t value) {
174 int retval = android_log_write_int32(ctx, value);
175 if (retval < 0) {
176 ret = retval;
177 }
178 return ret >= 0;
179 }
180
181 bool AppendLong(int64_t value) {
182 int retval = android_log_write_int64(ctx, value);
183 if (retval < 0) {
184 ret = retval;
185 }
186 return ret >= 0;
187 }
188
189 bool AppendString(const char* value) {
190 int retval = android_log_write_string8(ctx, value);
191 if (retval < 0) {
192 ret = retval;
193 }
194 return ret >= 0;
195 }
196
197 bool AppendString(const char* value, size_t len) {
198 int retval = android_log_write_string8_len(ctx, value, len);
199 if (retval < 0) {
200 ret = retval;
201 }
202 return ret >= 0;
203 }
204
205#if defined(_USING_LIBCXX)
206 bool AppendString(const std::string& value) {
207 int retval = android_log_write_string8_len(ctx, value.data(), value.length());
208 if (retval < 0) {
209 ret = retval;
210 }
211 return ret;
212 }
213
214 bool Append(const std::string& value) {
215 int retval = android_log_write_string8_len(ctx, value.data(), value.length());
216 if (retval < 0) {
217 ret = retval;
218 }
219 return ret;
220 }
221#endif
222
223 bool AppendFloat(float value) {
224 int retval = android_log_write_float32(ctx, value);
225 if (retval < 0) {
226 ret = retval;
227 }
228 return ret >= 0;
229 }
230
231 template <typename Tvalue>
232 bool Append(Tvalue value) {
233 *this << value;
234 return ret >= 0;
235 }
236
237 bool Append(const char* value, size_t len) {
238 int retval = android_log_write_string8_len(ctx, value, len);
239 if (retval < 0) {
240 ret = retval;
241 }
242 return ret >= 0;
243 }
244
245 android_log_list_element read() { return android_log_read_next(ctx); }
246 android_log_list_element peek() { return android_log_peek_next(ctx); }
247};
248
249#endif
250#endif // ANDROID_STATS_LOG_STATS_EVENT_LIST_H