• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 SRC_TRACE_PROCESSOR_IMPORTERS_ANDROID_BUGREPORT_ANDROID_LOG_PARSER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_ANDROID_BUGREPORT_ANDROID_LOG_PARSER_H_
19 
20 #include <stdint.h>
21 
22 #include <vector>
23 
24 #include "perfetto/ext/base/string_view.h"
25 #include "src/trace_processor/storage/trace_storage.h"
26 
27 namespace perfetto {
28 namespace trace_processor {
29 
30 struct AndroidLogEvent {
31   int64_t ts;  // Nanoseconds since Epoch.
32   uint32_t pid;
33   uint32_t tid;
34   uint32_t prio;  // Refer to enum ::protos::pbzero::AndroidLogPriority.
35   StringId tag;
36   StringId msg;
37 
38   // For std::sort().
39   bool operator<(const AndroidLogEvent& o) const { return ts < o.ts; }
40 
41   // For gtest.
42   bool operator==(const AndroidLogEvent& o) const {
43     return std::tie(ts, pid, tid, prio, tag, msg) ==
44            std::tie(o.ts, o.pid, o.tid, o.prio, o.tag, o.msg);
45   }
46 };
47 
48 // Parses log lines coming from persistent logcat (FS/data/misc/logd), interns
49 // string in the TP string pools and populates a vector of AndroidLogEvent
50 // structs. Does NOT insert log events into any table (for testing isolation),
51 // the caller is in charge to do that.
52 // It supports the following formats (auto-detected):
53 // 1) 12-31 23:59:00.123456 <pid> <tid> I tag: message
54 //    This is typically found in persistent logcat (FS/data/misc/logd/)
55 // 2) 06-24 15:57:11.346 <uid> <pid> <tid> D Tag: Message
56 //    This is typically found in the recent logcat dump in bugreport-xxx.txt
57 class AndroidLogParser {
58  public:
AndroidLogParser(int year,TraceStorage * storage)59   explicit AndroidLogParser(int year, TraceStorage* storage)
60       : storage_(storage), year_(year) {}
61   ~AndroidLogParser() = default;
62 
63   // Decodes logcat events for the input `lines` and appends them into
64   // `log_events`. If `dedupe_idx` is != 0, it checks for duplicate entries
65   // before inserting and skips the insertion if a dupe is found. Dupes are
66   // searched in the first `dedupe_idx` entries of `log_events`. In practice
67   // `dedupe_idx` is the log_events.size() for the last std::sort() call.
68   // The de-duping logic truncates timestamps to millisecond resolution, to
69   // handle the mismatching resolution of dumpstate (ms) vs persistent log (us).
70   void ParseLogLines(std::vector<base::StringView> lines,
71                      std::vector<AndroidLogEvent>* log_events,
72                      size_t dedupe_idx = 0);
73 
74  private:
75   TraceStorage* const storage_;
76   int year_ = 0;
77 };
78 
79 }  // namespace trace_processor
80 }  // namespace perfetto
81 
82 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_ANDROID_BUGREPORT_ANDROID_LOG_PARSER_H_
83