• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 #define LOG_TAG "incident_helper"
17 
18 #include <android/util/ProtoOutputStream.h>
19 
20 #include "frameworks/base/core/proto/android/util/event_log_tags.proto.h"
21 #include "ih_util.h"
22 #include "EventLogTagsParser.h"
23 
24 status_t
Parse(const int in,const int out) const25 EventLogTagsParser::Parse(const int in, const int out) const
26 {
27     Reader reader(in);
28     string line;
29 
30     ProtoOutputStream proto;
31 
32     // parse line by line
33     while (reader.readLine(&line)) {
34         if (line.empty()) continue;
35         string debug = line;
36         string tagNumber = behead(&line, ' ');
37         string tagName = behead(&line, ' ');
38         if (tagNumber == "" || tagName == "") {
39             fprintf(stderr, "Bad line, expect at least two parts: %s[%s, %s]\n",
40                 debug.c_str(), tagNumber.c_str(), tagName.c_str());
41             continue;
42         }
43 
44         uint64_t token = proto.start(EventLogTagMapProto::EVENT_LOG_TAGS);
45         proto.write(EventLogTag::TAG_NUMBER, toInt(tagNumber));
46         proto.write(EventLogTag::TAG_NAME, tagName);
47 
48         record_t valueDescriptors = parseRecord(line, PARENTHESES_DELIMITER);
49         for (size_t i = 0; i < valueDescriptors.size(); i++) {
50             record_t valueDescriptor = parseRecord(valueDescriptors[i], PIPE_DELIMITER);
51             if (valueDescriptor.size() != 2 && valueDescriptor.size() != 3) {
52                 // If the parts doesn't contains pipe, then skips it.
53                 continue;
54             }
55             uint64_t descriptorToken = proto.start(EventLogTag::VALUE_DESCRIPTORS);
56             proto.write(EventLogTag::ValueDescriptor::NAME, valueDescriptor[0]);
57             proto.write(EventLogTag::ValueDescriptor::TYPE, toInt(valueDescriptor[1]));
58             if (valueDescriptor.size() == 3) {
59                 char c = valueDescriptor[2][0];
60                 int unit = 0;
61                 if (c < '0' || c > '9') {
62                     unit = (int) c;
63                 } else {
64                     unit = toInt(valueDescriptor[2]);
65                 }
66                 proto.write(EventLogTag::ValueDescriptor::UNIT, unit);
67             }
68             proto.end(descriptorToken);
69         }
70         proto.end(token);
71     }
72 
73     if (!reader.ok(&line)) {
74         fprintf(stderr, "Bad read from fd %d: %s\n", in, line.c_str());
75         return -1;
76     }
77 
78     if (!proto.flush(out)) {
79         fprintf(stderr, "[%s]Error writing proto back\n", this->name.c_str());
80         return -1;
81     }
82     fprintf(stderr, "[%s]Proto size: %zu bytes\n", this->name.c_str(), proto.size());
83     return NO_ERROR;
84 }
85