blob: 499f2549b474211f6e89908e24ac108f988c1908 [file] [log] [blame]
Yao Chend54f9dd2017-10-17 17:37:48 +00001
2
3#include "Collation.h"
4
Stefan Lafonae2df012017-11-14 09:17:21 -08005#include "frameworks/base/cmds/statsd/src/atoms.pb.h"
Yao Chend54f9dd2017-10-17 17:37:48 +00006
7#include <set>
8#include <vector>
9
10#include <getopt.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15using namespace google::protobuf;
16using namespace std;
17
18namespace android {
19namespace stats_log_api_gen {
20
Yao Chenb3561512017-11-21 18:07:17 -080021const int PULL_ATOM_START_ID = 1000;
22
23int maxPushedAtomId = 2;
24
Stefan Lafonae2df012017-11-14 09:17:21 -080025using android::os::statsd::Atom;
Yao Chend54f9dd2017-10-17 17:37:48 +000026
Yao Chend54f9dd2017-10-17 17:37:48 +000027/**
28 * Turn lower and camel case into upper case with underscores.
29 */
30static string
31make_constant_name(const string& str)
32{
33 string result;
34 const int N = str.size();
35 bool underscore_next = false;
36 for (int i=0; i<N; i++) {
37 char c = str[i];
38 if (c >= 'A' && c <= 'Z') {
39 if (underscore_next) {
40 result += '_';
41 underscore_next = false;
42 }
43 } else if (c >= 'a' && c <= 'z') {
44 c = 'A' + c - 'a';
45 underscore_next = true;
46 } else if (c == '_') {
47 underscore_next = false;
48 }
49 result += c;
50 }
51 return result;
52}
53
54static const char*
55cpp_type_name(java_type_t type)
56{
57 switch (type) {
58 case JAVA_TYPE_BOOLEAN:
59 return "bool";
60 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -070061 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +000062 return "int32_t";
63 case JAVA_TYPE_LONG:
64 return "int64_t";
65 case JAVA_TYPE_FLOAT:
66 return "float";
67 case JAVA_TYPE_DOUBLE:
68 return "double";
69 case JAVA_TYPE_STRING:
70 return "char const*";
71 default:
72 return "UNKNOWN";
73 }
74}
75
76static const char*
77java_type_name(java_type_t type)
78{
79 switch (type) {
80 case JAVA_TYPE_BOOLEAN:
81 return "boolean";
82 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -070083 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +000084 return "int";
85 case JAVA_TYPE_LONG:
86 return "long";
87 case JAVA_TYPE_FLOAT:
88 return "float";
89 case JAVA_TYPE_DOUBLE:
90 return "double";
91 case JAVA_TYPE_STRING:
92 return "java.lang.String";
93 default:
94 return "UNKNOWN";
95 }
96}
97
Yangster-mac7604aea2017-12-11 22:55:49 -080098static int write_stats_log_cpp(FILE *out, const Atoms &atoms,
99 const AtomDecl &attributionDecl) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000100 // Print prelude
101 fprintf(out, "// This file is autogenerated\n");
102 fprintf(out, "\n");
103
Yangster-mac7604aea2017-12-11 22:55:49 -0800104 fprintf(out, "#include <exception>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000105 fprintf(out, "#include <log/log_event_list.h>\n");
106 fprintf(out, "#include <log/log.h>\n");
107 fprintf(out, "#include <statslog.h>\n");
Yangster-mac330af582018-02-08 15:24:38 -0800108 fprintf(out, "#include <utils/SystemClock.h>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000109 fprintf(out, "\n");
110
111 fprintf(out, "namespace android {\n");
112 fprintf(out, "namespace util {\n");
Yao Chen80235402017-11-13 20:42:25 -0800113 fprintf(out, "// the single event tag id for all stats logs\n");
114 fprintf(out, "const static int kStatsEventTag = 1937006964;\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000115
Yao Chenc40a19d2018-03-15 16:48:25 -0700116 std::set<string> kTruncatingAtomNames = {"mobile_radio_power_state_changed",
117 "audio_state_changed",
118 "call_state_changed",
119 "phone_signal_strength_changed",
120 "mobile_bytes_transfer_by_fg_bg",
121 "mobile_bytes_transfer"};
122 fprintf(out,
123 "const std::set<int> "
124 "AtomsInfo::kNotTruncatingTimestampAtomWhiteList = {\n");
125 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
126 atom != atoms.decls.end(); atom++) {
127 if (kTruncatingAtomNames.find(atom->name) ==
128 kTruncatingAtomNames.end()) {
129 string constant = make_constant_name(atom->name);
130 fprintf(out, " %s,\n", constant.c_str());
131 }
132 }
133 fprintf(out, "};\n");
134 fprintf(out, "\n");
135
136 fprintf(out,
137 "const std::set<int> AtomsInfo::kAtomsWithAttributionChain = {\n");
138 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
139 atom != atoms.decls.end(); atom++) {
140 for (vector<AtomField>::const_iterator field = atom->fields.begin();
141 field != atom->fields.end(); field++) {
142 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
143 string constant = make_constant_name(atom->name);
144 fprintf(out, " %s,\n", constant.c_str());
145 break;
146 }
147 }
148 }
149 fprintf(out, "};\n");
150 fprintf(out, "\n");
151
152 fprintf(out,
153 "static std::map<int, int> "
154 "getAtomUidField() {\n");
155 fprintf(out, " std::map<int, int> uidField;\n");
156 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
157 atom != atoms.decls.end(); atom++) {
158 if (atom->uidField == 0) {
159 continue;
160 }
161 fprintf(out,
162 "\n // Adding uid field for atom "
163 "(%d)%s\n",
164 atom->code, atom->name.c_str());
165 fprintf(out, " uidField[static_cast<int>(%s)] = %d;\n",
166 make_constant_name(atom->name).c_str(), atom->uidField);
167 }
168
169 fprintf(out, " return uidField;\n");
170 fprintf(out, "};\n");
171
172 fprintf(out,
173 "const std::map<int, int> AtomsInfo::kAtomsWithUidField = "
174 "getAtomUidField();\n");
175
176 fprintf(out,
177 "static std::map<int, StateAtomFieldOptions> "
178 "getStateAtomFieldOptions() {\n");
179 fprintf(out, " std::map<int, StateAtomFieldOptions> options;\n");
180 fprintf(out, " StateAtomFieldOptions opt;\n");
181 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
182 atom != atoms.decls.end(); atom++) {
183 if (atom->primaryFields.size() == 0 && atom->exclusiveField == 0) {
184 continue;
185 }
186 fprintf(out,
187 "\n // Adding primary and exclusive fields for atom "
188 "(%d)%s\n",
189 atom->code, atom->name.c_str());
190 fprintf(out, " opt.primaryFields.clear();\n");
191 for (const auto& field : atom->primaryFields) {
192 fprintf(out, " opt.primaryFields.push_back(%d);\n", field);
193 }
194
195 fprintf(out, " opt.exclusiveField = %d;\n", atom->exclusiveField);
196 fprintf(out, " options[static_cast<int>(%s)] = opt;\n",
197 make_constant_name(atom->name).c_str());
198 }
199
200 fprintf(out, " return options;\n");
201 fprintf(out, " }\n");
202
203 fprintf(out,
204 "const std::map<int, StateAtomFieldOptions> "
205 "AtomsInfo::kStateAtomsFieldOptions = "
206 "getStateAtomFieldOptions();\n");
207
Yao Chend54f9dd2017-10-17 17:37:48 +0000208 // Print write methods
209 fprintf(out, "\n");
210 for (set<vector<java_type_t>>::const_iterator signature = atoms.signatures.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800211 signature != atoms.signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000212 int argIndex;
213
Yao Chen97e21ec2018-03-29 11:00:38 -0700214 fprintf(out, "int\n");
Yao Chen80235402017-11-13 20:42:25 -0800215 fprintf(out, "stats_write(int32_t code");
Yao Chend54f9dd2017-10-17 17:37:48 +0000216 argIndex = 1;
217 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800218 arg != signature->end(); arg++) {
219 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
220 for (auto chainField : attributionDecl.fields) {
221 if (chainField.javaType == JAVA_TYPE_STRING) {
222 fprintf(out, ", const std::vector<%s>& %s",
223 cpp_type_name(chainField.javaType),
224 chainField.name.c_str());
225 } else {
226 fprintf(out, ", const %s* %s, size_t %s_length",
227 cpp_type_name(chainField.javaType),
228 chainField.name.c_str(), chainField.name.c_str());
229 }
230 }
231 } else {
232 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
233 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000234 argIndex++;
235 }
236 fprintf(out, ")\n");
237
238 fprintf(out, "{\n");
239 argIndex = 1;
Yao Chen80235402017-11-13 20:42:25 -0800240 fprintf(out, " android_log_event_list event(kStatsEventTag);\n");
Yangster-mac330af582018-02-08 15:24:38 -0800241 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800242 fprintf(out, " event << code;\n\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000243 for (vector<java_type_t>::const_iterator arg = signature->begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800244 arg != signature->end(); arg++) {
245 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
246 for (const auto &chainField : attributionDecl.fields) {
247 if (chainField.javaType == JAVA_TYPE_STRING) {
248 fprintf(out, " if (%s_length != %s.size()) {\n",
249 attributionDecl.fields.front().name.c_str(), chainField.name.c_str());
250 fprintf(out, " throw std::invalid_argument(\"attribution fields with"
251 " diff length: %s vs %s\");\n",
252 attributionDecl.fields.front().name.c_str(),
253 chainField.name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -0700254 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800255 fprintf(out, " }\n");
256 }
257 }
258 fprintf(out, "\n event.begin();\n");
259 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
260 attributionDecl.fields.front().name.c_str());
261 fprintf(out, " event.begin();\n");
262 for (const auto &chainField : attributionDecl.fields) {
Yangster-mac20ac9442018-01-08 14:54:48 -0800263 if (chainField.javaType == JAVA_TYPE_STRING) {
264 fprintf(out, " if (%s[i] != NULL) {\n", chainField.name.c_str());
265 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
266 fprintf(out, " } else {\n");
267 fprintf(out, " event << \"\";\n");
268 fprintf(out, " }\n");
269 } else {
270 fprintf(out, " event << %s[i];\n", chainField.name.c_str());
271 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800272 }
273 fprintf(out, " event.end();\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000274 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800275 fprintf(out, " event.end();\n\n");
276 } else {
277 if (*arg == JAVA_TYPE_STRING) {
278 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
279 fprintf(out, " arg%d = \"\";\n", argIndex);
280 fprintf(out, " }\n");
281 }
282 fprintf(out, " event << arg%d;\n", argIndex);
Yao Chend54f9dd2017-10-17 17:37:48 +0000283 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000284 argIndex++;
285 }
286
Yao Chen97e21ec2018-03-29 11:00:38 -0700287 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000288 fprintf(out, "}\n");
289 fprintf(out, "\n");
290 }
291
Yangster-macba5b9e42018-01-10 21:31:59 -0800292 for (set<vector<java_type_t>>::const_iterator signature = atoms.non_chained_signatures.begin();
293 signature != atoms.non_chained_signatures.end(); signature++) {
294 int argIndex;
295
Yao Chen97e21ec2018-03-29 11:00:38 -0700296 fprintf(out, "int\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800297 fprintf(out, "stats_write_non_chained(int32_t code");
298 argIndex = 1;
299 for (vector<java_type_t>::const_iterator arg = signature->begin();
300 arg != signature->end(); arg++) {
301 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
302 argIndex++;
303 }
304 fprintf(out, ")\n");
305
306 fprintf(out, "{\n");
307 argIndex = 1;
308 fprintf(out, " android_log_event_list event(kStatsEventTag);\n");
Yangster-mac31ddcde2018-02-14 16:09:35 -0800309 fprintf(out, " event << android::elapsedRealtimeNano();\n\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800310 fprintf(out, " event << code;\n\n");
311 for (vector<java_type_t>::const_iterator arg = signature->begin();
312 arg != signature->end(); arg++) {
313 if (argIndex == 1) {
314 fprintf(out, " event.begin();\n\n");
315 fprintf(out, " event.begin();\n");
316 }
317 if (*arg == JAVA_TYPE_STRING) {
318 fprintf(out, " if (arg%d == NULL) {\n", argIndex);
319 fprintf(out, " arg%d = \"\";\n", argIndex);
320 fprintf(out, " }\n");
321 }
322 fprintf(out, " event << arg%d;\n", argIndex);
323 if (argIndex == 2) {
324 fprintf(out, " event.end();\n\n");
325 fprintf(out, " event.end();\n\n");
326 }
327 argIndex++;
328 }
329
Yao Chen97e21ec2018-03-29 11:00:38 -0700330 fprintf(out, " return event.write(LOG_ID_STATS);\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800331 fprintf(out, "}\n");
332 fprintf(out, "\n");
333 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000334 // Print footer
335 fprintf(out, "\n");
336 fprintf(out, "} // namespace util\n");
337 fprintf(out, "} // namespace android\n");
338
339 return 0;
340}
341
Yangster-macba5b9e42018-01-10 21:31:59 -0800342void build_non_chained_decl_map(const Atoms& atoms,
343 std::map<int, set<AtomDecl>::const_iterator>* decl_map){
344 for (set<AtomDecl>::const_iterator atom = atoms.non_chained_decls.begin();
345 atom != atoms.non_chained_decls.end(); atom++) {
346 decl_map->insert(std::make_pair(atom->code, atom));
347 }
348}
349
350static void write_cpp_usage(
351 FILE* out, const string& method_name, const string& atom_code_name,
352 const AtomDecl& atom, const AtomDecl &attributionDecl) {
353 fprintf(out, " * Usage: %s(StatsLog.%s", method_name.c_str(), atom_code_name.c_str());
354 for (vector<AtomField>::const_iterator field = atom.fields.begin();
355 field != atom.fields.end(); field++) {
356 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
357 for (auto chainField : attributionDecl.fields) {
358 if (chainField.javaType == JAVA_TYPE_STRING) {
359 fprintf(out, ", const std::vector<%s>& %s",
360 cpp_type_name(chainField.javaType),
361 chainField.name.c_str());
362 } else {
363 fprintf(out, ", const %s* %s, size_t %s_length",
364 cpp_type_name(chainField.javaType),
365 chainField.name.c_str(), chainField.name.c_str());
366 }
367 }
368 } else {
369 fprintf(out, ", %s %s", cpp_type_name(field->javaType), field->name.c_str());
370 }
371 }
372 fprintf(out, ");\n");
373}
374
375static void write_cpp_method_header(
376 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
377 const AtomDecl &attributionDecl) {
378 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
379 signature != signatures.end(); signature++) {
Yao Chen97e21ec2018-03-29 11:00:38 -0700380 fprintf(out, "int %s(int32_t code ", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800381 int argIndex = 1;
382 for (vector<java_type_t>::const_iterator arg = signature->begin();
383 arg != signature->end(); arg++) {
384 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
385 for (auto chainField : attributionDecl.fields) {
386 if (chainField.javaType == JAVA_TYPE_STRING) {
387 fprintf(out, ", const std::vector<%s>& %s",
388 cpp_type_name(chainField.javaType), chainField.name.c_str());
389 } else {
390 fprintf(out, ", const %s* %s, size_t %s_length",
391 cpp_type_name(chainField.javaType),
392 chainField.name.c_str(), chainField.name.c_str());
393 }
394 }
395 } else {
396 fprintf(out, ", %s arg%d", cpp_type_name(*arg), argIndex);
397 }
398 argIndex++;
399 }
400 fprintf(out, ");\n");
401
402 }
403}
Yao Chend54f9dd2017-10-17 17:37:48 +0000404
405static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800406write_stats_log_header(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000407{
Yao Chend54f9dd2017-10-17 17:37:48 +0000408 // Print prelude
409 fprintf(out, "// This file is autogenerated\n");
410 fprintf(out, "\n");
411 fprintf(out, "#pragma once\n");
412 fprintf(out, "\n");
413 fprintf(out, "#include <stdint.h>\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800414 fprintf(out, "#include <vector>\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800415 fprintf(out, "#include <map>\n");
Yangster-mac68985802018-01-21 10:05:09 -0800416 fprintf(out, "#include <set>\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000417 fprintf(out, "\n");
418
419 fprintf(out, "namespace android {\n");
420 fprintf(out, "namespace util {\n");
421 fprintf(out, "\n");
422 fprintf(out, "/*\n");
423 fprintf(out, " * API For logging statistics events.\n");
424 fprintf(out, " */\n");
425 fprintf(out, "\n");
426 fprintf(out, "/**\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700427 fprintf(out, " * Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000428 fprintf(out, " */\n");
429 fprintf(out, "enum {\n");
430
Yangster-macba5b9e42018-01-10 21:31:59 -0800431 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
432 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
433
Yao Chend54f9dd2017-10-17 17:37:48 +0000434 size_t i = 0;
435 // Print constants
436 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800437 atom != atoms.decls.end(); atom++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000438 string constant = make_constant_name(atom->name);
439 fprintf(out, "\n");
440 fprintf(out, " /**\n");
441 fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800442 write_cpp_usage(out, "stats_write", constant, *atom, attributionDecl);
443
444 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
445 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
446 write_cpp_usage(out, "stats_write_non_chained", constant, *non_chained_decl->second,
447 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000448 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000449 fprintf(out, " */\n");
450 char const* const comma = (i == atoms.decls.size() - 1) ? "" : ",";
451 fprintf(out, " %s = %d%s\n", constant.c_str(), atom->code, comma);
Yao Chenb3561512017-11-21 18:07:17 -0800452 if (atom->code < PULL_ATOM_START_ID && atom->code > maxPushedAtomId) {
453 maxPushedAtomId = atom->code;
454 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000455 i++;
456 }
457 fprintf(out, "\n");
458 fprintf(out, "};\n");
459 fprintf(out, "\n");
460
Yao Chen9c1debe2018-02-19 14:39:19 -0800461 fprintf(out, "struct StateAtomFieldOptions {\n");
462 fprintf(out, " std::vector<int> primaryFields;\n");
463 fprintf(out, " int exclusiveField;\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700464 fprintf(out, "};\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800465 fprintf(out, "\n");
Yao Chenc40a19d2018-03-15 16:48:25 -0700466
467 fprintf(out, "struct AtomsInfo {\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800468 fprintf(out,
Yao Chenc40a19d2018-03-15 16:48:25 -0700469 " const static std::set<int> "
470 "kNotTruncatingTimestampAtomWhiteList;\n");
471 fprintf(out, " const static std::map<int, int> kAtomsWithUidField;\n");
472 fprintf(out,
473 " const static std::set<int> kAtomsWithAttributionChain;\n");
474 fprintf(out,
475 " const static std::map<int, StateAtomFieldOptions> "
476 "kStateAtomsFieldOptions;\n");
Yao Chen9c1debe2018-02-19 14:39:19 -0800477 fprintf(out, "};\n");
478
Yao Chenc40a19d2018-03-15 16:48:25 -0700479 fprintf(out, "const static int kMaxPushedAtomId = %d;\n\n",
480 maxPushedAtomId);
Yao Chen9c1debe2018-02-19 14:39:19 -0800481
Yao Chend54f9dd2017-10-17 17:37:48 +0000482 // Print write methods
483 fprintf(out, "//\n");
484 fprintf(out, "// Write methods\n");
485 fprintf(out, "//\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800486 write_cpp_method_header(out, "stats_write", atoms.signatures, attributionDecl);
487
488 fprintf(out, "//\n");
489 fprintf(out, "// Write flattened methods\n");
490 fprintf(out, "//\n");
491 write_cpp_method_header(out, "stats_write_non_chained", atoms.non_chained_signatures,
492 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000493
494 fprintf(out, "\n");
495 fprintf(out, "} // namespace util\n");
496 fprintf(out, "} // namespace android\n");
497
498 return 0;
499}
500
Yangster-macba5b9e42018-01-10 21:31:59 -0800501static void write_java_usage(
502 FILE* out, const string& method_name, const string& atom_code_name,
503 const AtomDecl& atom, const AtomDecl &attributionDecl) {
504 fprintf(out, " * Usage: StatsLog.%s(StatsLog.%s",
505 method_name.c_str(), atom_code_name.c_str());
506 for (vector<AtomField>::const_iterator field = atom.fields.begin();
507 field != atom.fields.end(); field++) {
508 if (field->javaType == JAVA_TYPE_ATTRIBUTION_CHAIN) {
509 for (auto chainField : attributionDecl.fields) {
510 fprintf(out, ", %s[] %s",
511 java_type_name(chainField.javaType), chainField.name.c_str());
512 }
513 } else {
514 fprintf(out, ", %s %s", java_type_name(field->javaType), field->name.c_str());
515 }
516 }
517 fprintf(out, ");\n");
518}
519
520static void write_java_method(
521 FILE* out, const string& method_name, const set<vector<java_type_t>>& signatures,
522 const AtomDecl &attributionDecl) {
523 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
524 signature != signatures.end(); signature++) {
Yao Chen97e21ec2018-03-29 11:00:38 -0700525 fprintf(out, " public static native int %s(int code", method_name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800526 int argIndex = 1;
527 for (vector<java_type_t>::const_iterator arg = signature->begin();
528 arg != signature->end(); arg++) {
529 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
530 for (auto chainField : attributionDecl.fields) {
531 fprintf(out, ", %s[] %s",
532 java_type_name(chainField.javaType), chainField.name.c_str());
533 }
534 } else {
535 fprintf(out, ", %s arg%d", java_type_name(*arg), argIndex);
536 }
537 argIndex++;
538 }
539 fprintf(out, ");\n");
540 }
541}
542
543
Yao Chend54f9dd2017-10-17 17:37:48 +0000544static int
Yangster-mac7604aea2017-12-11 22:55:49 -0800545write_stats_log_java(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000546{
Yao Chend54f9dd2017-10-17 17:37:48 +0000547 // Print prelude
548 fprintf(out, "// This file is autogenerated\n");
549 fprintf(out, "\n");
550 fprintf(out, "package android.util;\n");
551 fprintf(out, "\n");
552 fprintf(out, "\n");
553 fprintf(out, "/**\n");
554 fprintf(out, " * API For logging statistics events.\n");
555 fprintf(out, " * @hide\n");
556 fprintf(out, " */\n");
David Chen0a368b22017-12-06 16:28:16 -0800557 fprintf(out, "public class StatsLogInternal {\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700558 fprintf(out, " // Constants for atom codes.\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000559
Yangster-macba5b9e42018-01-10 21:31:59 -0800560 std::map<int, set<AtomDecl>::const_iterator> atom_code_to_non_chained_decl_map;
561 build_non_chained_decl_map(atoms, &atom_code_to_non_chained_decl_map);
562
Stefan Lafon9478f352017-10-30 21:20:20 -0700563 // Print constants for the atom codes.
Yao Chend54f9dd2017-10-17 17:37:48 +0000564 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
565 atom != atoms.decls.end(); atom++) {
566 string constant = make_constant_name(atom->name);
567 fprintf(out, "\n");
568 fprintf(out, " /**\n");
569 fprintf(out, " * %s %s\n", atom->message.c_str(), atom->name.c_str());
Yangster-macba5b9e42018-01-10 21:31:59 -0800570 write_java_usage(out, "write", constant, *atom, attributionDecl);
571 auto non_chained_decl = atom_code_to_non_chained_decl_map.find(atom->code);
572 if (non_chained_decl != atom_code_to_non_chained_decl_map.end()) {
573 write_java_usage(out, "write_non_chained", constant, *non_chained_decl->second,
574 attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000575 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000576 fprintf(out, " */\n");
577 fprintf(out, " public static final int %s = %d;\n", constant.c_str(), atom->code);
578 }
579 fprintf(out, "\n");
580
Stefan Lafon9478f352017-10-30 21:20:20 -0700581 // Print constants for the enum values.
582 fprintf(out, " // Constants for enum values.\n\n");
583 for (set<AtomDecl>::const_iterator atom = atoms.decls.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800584 atom != atoms.decls.end(); atom++) {
Stefan Lafon9478f352017-10-30 21:20:20 -0700585 for (vector<AtomField>::const_iterator field = atom->fields.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800586 field != atom->fields.end(); field++) {
587 if (field->javaType == JAVA_TYPE_ENUM) {
588 fprintf(out, " // Values for %s.%s\n", atom->message.c_str(),
589 field->name.c_str());
590 for (map<int, string>::const_iterator value = field->enumValues.begin();
591 value != field->enumValues.end(); value++) {
592 fprintf(out, " public static final int %s__%s__%s = %d;\n",
593 make_constant_name(atom->message).c_str(),
594 make_constant_name(field->name).c_str(),
595 make_constant_name(value->second).c_str(),
596 value->first);
597 }
598 fprintf(out, "\n");
Stefan Lafon9478f352017-10-30 21:20:20 -0700599 }
Stefan Lafon9478f352017-10-30 21:20:20 -0700600 }
601 }
602
Yao Chend54f9dd2017-10-17 17:37:48 +0000603 // Print write methods
604 fprintf(out, " // Write methods\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800605 write_java_method(out, "write", atoms.signatures, attributionDecl);
606 write_java_method(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000607
608 fprintf(out, "}\n");
609
610 return 0;
611}
612
613static const char*
614jni_type_name(java_type_t type)
615{
616 switch (type) {
617 case JAVA_TYPE_BOOLEAN:
618 return "jboolean";
619 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700620 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000621 return "jint";
622 case JAVA_TYPE_LONG:
623 return "jlong";
624 case JAVA_TYPE_FLOAT:
625 return "jfloat";
626 case JAVA_TYPE_DOUBLE:
627 return "jdouble";
628 case JAVA_TYPE_STRING:
629 return "jstring";
630 default:
631 return "UNKNOWN";
632 }
633}
634
Yangster-mac7604aea2017-12-11 22:55:49 -0800635static const char*
636jni_array_type_name(java_type_t type)
637{
638 switch (type) {
639 case JAVA_TYPE_INT:
640 return "jintArray";
641 case JAVA_TYPE_STRING:
642 return "jobjectArray";
643 default:
644 return "UNKNOWN";
645 }
646}
647
Yao Chend54f9dd2017-10-17 17:37:48 +0000648static string
Yangster-macba5b9e42018-01-10 21:31:59 -0800649jni_function_name(const string& method_name, const vector<java_type_t>& signature)
Yao Chend54f9dd2017-10-17 17:37:48 +0000650{
Yangster-macba5b9e42018-01-10 21:31:59 -0800651 string result("StatsLog_" + method_name);
Yao Chend54f9dd2017-10-17 17:37:48 +0000652 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800653 arg != signature.end(); arg++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000654 switch (*arg) {
655 case JAVA_TYPE_BOOLEAN:
656 result += "_boolean";
657 break;
658 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700659 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000660 result += "_int";
661 break;
662 case JAVA_TYPE_LONG:
663 result += "_long";
664 break;
665 case JAVA_TYPE_FLOAT:
666 result += "_float";
667 break;
668 case JAVA_TYPE_DOUBLE:
669 result += "_double";
670 break;
671 case JAVA_TYPE_STRING:
672 result += "_String";
673 break;
Yangster-mac7604aea2017-12-11 22:55:49 -0800674 case JAVA_TYPE_ATTRIBUTION_CHAIN:
675 result += "_AttributionChain";
676 break;
Yao Chend54f9dd2017-10-17 17:37:48 +0000677 default:
678 result += "_UNKNOWN";
679 break;
680 }
681 }
682 return result;
683}
684
685static const char*
686java_type_signature(java_type_t type)
687{
688 switch (type) {
689 case JAVA_TYPE_BOOLEAN:
690 return "Z";
691 case JAVA_TYPE_INT:
Stefan Lafon9478f352017-10-30 21:20:20 -0700692 case JAVA_TYPE_ENUM:
Yao Chend54f9dd2017-10-17 17:37:48 +0000693 return "I";
694 case JAVA_TYPE_LONG:
695 return "J";
696 case JAVA_TYPE_FLOAT:
697 return "F";
698 case JAVA_TYPE_DOUBLE:
699 return "D";
700 case JAVA_TYPE_STRING:
701 return "Ljava/lang/String;";
702 default:
703 return "UNKNOWN";
704 }
705}
706
707static string
Yangster-mac7604aea2017-12-11 22:55:49 -0800708jni_function_signature(const vector<java_type_t>& signature, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000709{
710 string result("(I");
711 for (vector<java_type_t>::const_iterator arg = signature.begin();
Yangster-mac7604aea2017-12-11 22:55:49 -0800712 arg != signature.end(); arg++) {
713 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
714 for (auto chainField : attributionDecl.fields) {
715 result += "[";
716 result += java_type_signature(chainField.javaType);
717 }
718 } else {
719 result += java_type_signature(*arg);
720 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000721 }
Yao Chen97e21ec2018-03-29 11:00:38 -0700722 result += ")I";
Yao Chend54f9dd2017-10-17 17:37:48 +0000723 return result;
724}
725
726static int
Yangster-macba5b9e42018-01-10 21:31:59 -0800727write_stats_log_jni(FILE* out, const string& java_method_name, const string& cpp_method_name,
728 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl)
Yao Chend54f9dd2017-10-17 17:37:48 +0000729{
Yao Chend54f9dd2017-10-17 17:37:48 +0000730 // Print write methods
Yangster-macba5b9e42018-01-10 21:31:59 -0800731 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
732 signature != signatures.end(); signature++) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000733 int argIndex;
734
Yao Chen97e21ec2018-03-29 11:00:38 -0700735 fprintf(out, "static int\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000736 fprintf(out, "%s(JNIEnv* env, jobject clazz UNUSED, jint code",
Yangster-macba5b9e42018-01-10 21:31:59 -0800737 jni_function_name(java_method_name, *signature).c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +0000738 argIndex = 1;
739 for (vector<java_type_t>::const_iterator arg = signature->begin();
740 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800741 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
742 for (auto chainField : attributionDecl.fields) {
743 fprintf(out, ", %s %s", jni_array_type_name(chainField.javaType),
744 chainField.name.c_str());
745 }
746 } else {
747 fprintf(out, ", %s arg%d", jni_type_name(*arg), argIndex);
748 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000749 argIndex++;
750 }
751 fprintf(out, ")\n");
752
753 fprintf(out, "{\n");
754
755 // Prepare strings
756 argIndex = 1;
Yangster-mac7604aea2017-12-11 22:55:49 -0800757 bool hadStringOrChain = false;
Yao Chend54f9dd2017-10-17 17:37:48 +0000758 for (vector<java_type_t>::const_iterator arg = signature->begin();
759 arg != signature->end(); arg++) {
760 if (*arg == JAVA_TYPE_STRING) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800761 hadStringOrChain = true;
Yao Chend54f9dd2017-10-17 17:37:48 +0000762 fprintf(out, " const char* str%d;\n", argIndex);
763 fprintf(out, " if (arg%d != NULL) {\n", argIndex);
764 fprintf(out, " str%d = env->GetStringUTFChars(arg%d, NULL);\n",
765 argIndex, argIndex);
766 fprintf(out, " } else {\n");
767 fprintf(out, " str%d = NULL;\n", argIndex);
768 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800769 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
770 hadStringOrChain = true;
771 for (auto chainField : attributionDecl.fields) {
772 fprintf(out, " size_t %s_length = env->GetArrayLength(%s);\n",
773 chainField.name.c_str(), chainField.name.c_str());
774 if (chainField.name != attributionDecl.fields.front().name) {
775 fprintf(out, " if (%s_length != %s_length) {\n",
776 chainField.name.c_str(),
777 attributionDecl.fields.front().name.c_str());
778 fprintf(out, " jniThrowException(env, "
779 "\"java/lang/IllegalArgumentException\", "
780 "\"invalid attribution field(%s) length.\");\n",
781 chainField.name.c_str());
Yao Chen97e21ec2018-03-29 11:00:38 -0700782 fprintf(out, " return -EINVAL;\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800783 fprintf(out, " }\n");
784 }
785 if (chainField.javaType == JAVA_TYPE_INT) {
786 fprintf(out, " jint* %s_array = env->GetIntArrayElements(%s, NULL);\n",
787 chainField.name.c_str(), chainField.name.c_str());
788 } else if (chainField.javaType == JAVA_TYPE_STRING) {
789 fprintf(out, " std::vector<%s> %s_vec;\n",
790 cpp_type_name(chainField.javaType), chainField.name.c_str());
791 fprintf(out, " std::vector<ScopedUtfChars*> scoped_%s_vec;\n",
792 chainField.name.c_str());
793 fprintf(out, " for (size_t i = 0; i < %s_length; ++i) {\n",
794 chainField.name.c_str());
795 fprintf(out, " jstring jstr = "
796 "(jstring)env->GetObjectArrayElement(%s, i);\n",
797 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800798 fprintf(out, " if (jstr == NULL) {\n");
799 fprintf(out, " %s_vec.push_back(NULL);\n",
800 chainField.name.c_str());
801 fprintf(out, " } else {\n");
802 fprintf(out, " ScopedUtfChars* scoped_%s = "
Yangster-mac7604aea2017-12-11 22:55:49 -0800803 "new ScopedUtfChars(env, jstr);\n",
804 chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800805 fprintf(out, " %s_vec.push_back(scoped_%s->c_str());\n",
Yangster-mac7604aea2017-12-11 22:55:49 -0800806 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800807 fprintf(out, " scoped_%s_vec.push_back(scoped_%s);\n",
Yangster-mac7604aea2017-12-11 22:55:49 -0800808 chainField.name.c_str(), chainField.name.c_str());
Yangster-mac20ac9442018-01-08 14:54:48 -0800809 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800810 fprintf(out, " }\n");
811 }
812 fprintf(out, "\n");
813 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000814 }
815 argIndex++;
816 }
Yangster-mac7604aea2017-12-11 22:55:49 -0800817 // Emit this to quiet the unused parameter warning if there were no strings or attribution
818 // chains.
819 if (!hadStringOrChain) {
Yao Chend54f9dd2017-10-17 17:37:48 +0000820 fprintf(out, " (void)env;\n");
821 }
822
823 // stats_write call
824 argIndex = 1;
Yao Chen97e21ec2018-03-29 11:00:38 -0700825 fprintf(out, " int ret = android::util::%s(code", cpp_method_name.c_str());
Yao Chend54f9dd2017-10-17 17:37:48 +0000826 for (vector<java_type_t>::const_iterator arg = signature->begin();
827 arg != signature->end(); arg++) {
Yangster-mac7604aea2017-12-11 22:55:49 -0800828 if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
829 for (auto chainField : attributionDecl.fields) {
830 if (chainField.javaType == JAVA_TYPE_INT) {
831 fprintf(out, ", (const %s*)%s_array, %s_length",
832 cpp_type_name(chainField.javaType),
833 chainField.name.c_str(), chainField.name.c_str());
834 } else if (chainField.javaType == JAVA_TYPE_STRING) {
835 fprintf(out, ", %s_vec", chainField.name.c_str());
836 }
837 }
838 } else {
839 const char *argName = (*arg == JAVA_TYPE_STRING) ? "str" : "arg";
840 fprintf(out, ", (%s)%s%d", cpp_type_name(*arg), argName, argIndex);
841 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000842 argIndex++;
843 }
844 fprintf(out, ");\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800845 fprintf(out, "\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000846
847 // Clean up strings
848 argIndex = 1;
849 for (vector<java_type_t>::const_iterator arg = signature->begin();
850 arg != signature->end(); arg++) {
851 if (*arg == JAVA_TYPE_STRING) {
852 fprintf(out, " if (str%d != NULL) {\n", argIndex);
853 fprintf(out, " env->ReleaseStringUTFChars(arg%d, str%d);\n",
854 argIndex, argIndex);
855 fprintf(out, " }\n");
Yangster-mac7604aea2017-12-11 22:55:49 -0800856 } else if (*arg == JAVA_TYPE_ATTRIBUTION_CHAIN) {
857 for (auto chainField : attributionDecl.fields) {
858 if (chainField.javaType == JAVA_TYPE_INT) {
859 fprintf(out, " env->ReleaseIntArrayElements(%s, %s_array, 0);\n",
860 chainField.name.c_str(), chainField.name.c_str());
861 } else if (chainField.javaType == JAVA_TYPE_STRING) {
Yangster-mac20ac9442018-01-08 14:54:48 -0800862 fprintf(out, " for (size_t i = 0; i < scoped_%s_vec.size(); ++i) {\n",
Yangster-mac7604aea2017-12-11 22:55:49 -0800863 chainField.name.c_str());
864 fprintf(out, " delete scoped_%s_vec[i];\n", chainField.name.c_str());
865 fprintf(out, " }\n");
866 }
867 }
Yao Chend54f9dd2017-10-17 17:37:48 +0000868 }
869 argIndex++;
870 }
Yao Chen97e21ec2018-03-29 11:00:38 -0700871 fprintf(out, " return ret;\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000872
873 fprintf(out, "}\n");
874 fprintf(out, "\n");
875 }
876
Yangster-macba5b9e42018-01-10 21:31:59 -0800877
878 return 0;
879}
880
881void write_jni_registration(FILE* out, const string& java_method_name,
882 const set<vector<java_type_t>>& signatures, const AtomDecl &attributionDecl) {
883 for (set<vector<java_type_t>>::const_iterator signature = signatures.begin();
884 signature != signatures.end(); signature++) {
885 fprintf(out, " { \"%s\", \"%s\", (void*)%s },\n",
886 java_method_name.c_str(),
887 jni_function_signature(*signature, attributionDecl).c_str(),
888 jni_function_name(java_method_name, *signature).c_str());
889 }
890}
891
892static int
893write_stats_log_jni(FILE* out, const Atoms& atoms, const AtomDecl &attributionDecl)
894{
895 // Print prelude
896 fprintf(out, "// This file is autogenerated\n");
897 fprintf(out, "\n");
898
899 fprintf(out, "#include <statslog.h>\n");
900 fprintf(out, "\n");
901 fprintf(out, "#include <nativehelper/JNIHelp.h>\n");
902 fprintf(out, "#include <nativehelper/ScopedUtfChars.h>\n");
903 fprintf(out, "#include <utils/Vector.h>\n");
904 fprintf(out, "#include \"core_jni_helpers.h\"\n");
905 fprintf(out, "#include \"jni.h\"\n");
906 fprintf(out, "\n");
907 fprintf(out, "#define UNUSED __attribute__((__unused__))\n");
908 fprintf(out, "\n");
909
910 fprintf(out, "namespace android {\n");
911 fprintf(out, "\n");
912
913 write_stats_log_jni(out, "write", "stats_write", atoms.signatures, attributionDecl);
914 write_stats_log_jni(out, "write_non_chained", "stats_write_non_chained",
915 atoms.non_chained_signatures, attributionDecl);
916
Yao Chend54f9dd2017-10-17 17:37:48 +0000917 // Print registration function table
918 fprintf(out, "/*\n");
919 fprintf(out, " * JNI registration.\n");
920 fprintf(out, " */\n");
921 fprintf(out, "static const JNINativeMethod gRegisterMethods[] = {\n");
Yangster-macba5b9e42018-01-10 21:31:59 -0800922 write_jni_registration(out, "write", atoms.signatures, attributionDecl);
923 write_jni_registration(out, "write_non_chained", atoms.non_chained_signatures, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +0000924 fprintf(out, "};\n");
925 fprintf(out, "\n");
926
927 // Print registration function
928 fprintf(out, "int register_android_util_StatsLog(JNIEnv* env) {\n");
929 fprintf(out, " return RegisterMethodsOrDie(\n");
930 fprintf(out, " env,\n");
931 fprintf(out, " \"android/util/StatsLog\",\n");
932 fprintf(out, " gRegisterMethods, NELEM(gRegisterMethods));\n");
933 fprintf(out, "}\n");
934
935 fprintf(out, "\n");
936 fprintf(out, "} // namespace android\n");
Yao Chend54f9dd2017-10-17 17:37:48 +0000937 return 0;
938}
939
Yao Chend54f9dd2017-10-17 17:37:48 +0000940static void
941print_usage()
942{
943 fprintf(stderr, "usage: stats-log-api-gen OPTIONS\n");
944 fprintf(stderr, "\n");
945 fprintf(stderr, "OPTIONS\n");
946 fprintf(stderr, " --cpp FILENAME the header file to output\n");
947 fprintf(stderr, " --header FILENAME the cpp file to output\n");
948 fprintf(stderr, " --help this message\n");
949 fprintf(stderr, " --java FILENAME the java file to output\n");
950 fprintf(stderr, " --jni FILENAME the jni file to output\n");
951}
952
953/**
954 * Do the argument parsing and execute the tasks.
955 */
956static int
957run(int argc, char const*const* argv)
958{
959 string cppFilename;
960 string headerFilename;
961 string javaFilename;
962 string jniFilename;
963
964 int index = 1;
965 while (index < argc) {
966 if (0 == strcmp("--help", argv[index])) {
967 print_usage();
968 return 0;
969 } else if (0 == strcmp("--cpp", argv[index])) {
970 index++;
971 if (index >= argc) {
972 print_usage();
973 return 1;
974 }
975 cppFilename = argv[index];
976 } else if (0 == strcmp("--header", argv[index])) {
977 index++;
978 if (index >= argc) {
979 print_usage();
980 return 1;
981 }
982 headerFilename = argv[index];
983 } else if (0 == strcmp("--java", argv[index])) {
984 index++;
985 if (index >= argc) {
986 print_usage();
987 return 1;
988 }
989 javaFilename = argv[index];
990 } else if (0 == strcmp("--jni", argv[index])) {
991 index++;
992 if (index >= argc) {
993 print_usage();
994 return 1;
995 }
996 jniFilename = argv[index];
997 }
998 index++;
999 }
1000
1001 if (cppFilename.size() == 0
1002 && headerFilename.size() == 0
1003 && javaFilename.size() == 0
1004 && jniFilename.size() == 0) {
1005 print_usage();
1006 return 1;
1007 }
1008
1009 // Collate the parameters
1010 Atoms atoms;
Stefan Lafonae2df012017-11-14 09:17:21 -08001011 int errorCount = collate_atoms(Atom::descriptor(), &atoms);
Yao Chend54f9dd2017-10-17 17:37:48 +00001012 if (errorCount != 0) {
1013 return 1;
1014 }
1015
Yangster-mac7604aea2017-12-11 22:55:49 -08001016 AtomDecl attributionDecl;
1017 vector<java_type_t> attributionSignature;
Yangster-mac20877162017-12-22 17:19:39 -08001018 collate_atom(android::os::statsd::AttributionNode::descriptor(),
Yangster-mac7604aea2017-12-11 22:55:49 -08001019 &attributionDecl, &attributionSignature);
1020
Yao Chend54f9dd2017-10-17 17:37:48 +00001021 // Write the .cpp file
1022 if (cppFilename.size() != 0) {
1023 FILE* out = fopen(cppFilename.c_str(), "w");
1024 if (out == NULL) {
1025 fprintf(stderr, "Unable to open file for write: %s\n", cppFilename.c_str());
1026 return 1;
1027 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001028 errorCount = android::stats_log_api_gen::write_stats_log_cpp(
1029 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001030 fclose(out);
1031 }
1032
1033 // Write the .h file
1034 if (headerFilename.size() != 0) {
1035 FILE* out = fopen(headerFilename.c_str(), "w");
1036 if (out == NULL) {
1037 fprintf(stderr, "Unable to open file for write: %s\n", headerFilename.c_str());
1038 return 1;
1039 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001040 errorCount = android::stats_log_api_gen::write_stats_log_header(
1041 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001042 fclose(out);
1043 }
1044
1045 // Write the .java file
1046 if (javaFilename.size() != 0) {
1047 FILE* out = fopen(javaFilename.c_str(), "w");
1048 if (out == NULL) {
1049 fprintf(stderr, "Unable to open file for write: %s\n", javaFilename.c_str());
1050 return 1;
1051 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001052 errorCount = android::stats_log_api_gen::write_stats_log_java(
1053 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001054 fclose(out);
1055 }
1056
1057 // Write the jni file
1058 if (jniFilename.size() != 0) {
1059 FILE* out = fopen(jniFilename.c_str(), "w");
1060 if (out == NULL) {
1061 fprintf(stderr, "Unable to open file for write: %s\n", jniFilename.c_str());
1062 return 1;
1063 }
Yangster-mac7604aea2017-12-11 22:55:49 -08001064 errorCount = android::stats_log_api_gen::write_stats_log_jni(
1065 out, atoms, attributionDecl);
Yao Chend54f9dd2017-10-17 17:37:48 +00001066 fclose(out);
1067 }
1068
1069 return 0;
1070}
1071
1072}
1073}
1074
1075/**
1076 * Main.
1077 */
1078int
1079main(int argc, char const*const* argv)
1080{
1081 GOOGLE_PROTOBUF_VERIFY_VERSION;
1082
1083 return android::stats_log_api_gen::run(argc, argv);
Yangster-mac7604aea2017-12-11 22:55:49 -08001084}