xref: /aosp_15_r20/development/tools/winscope/src/parsers/protolog/perfetto/parser_protolog.ts (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
1/*
2 * Copyright (C) 2024 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
17import {AbstractParser} from 'parsers/perfetto/abstract_parser';
18import {LogMessage} from 'parsers/protolog/log_message';
19import {ParserProtologUtils} from 'parsers/protolog/parser_protolog_utils';
20import {TraceType} from 'trace/trace_type';
21import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
22
23class PerfettoLogMessageTableRow {
24  message = '<NO_MESSAGE>';
25  tag = '<NO_TAG>';
26  level = '<NO_LEVEL>';
27  location = '<NO_LOC>';
28  timestamp: bigint = 0n;
29
30  constructor(
31    timestamp: bigint,
32    tag: string,
33    level: string,
34    message: string,
35    location: string,
36  ) {
37    this.timestamp = timestamp ?? this.timestamp;
38    this.tag = tag ?? this.tag;
39    this.level = level ?? this.level;
40    this.message = message ?? this.message;
41    this.location = location ?? this.location;
42  }
43}
44
45export class ParserProtolog extends AbstractParser<PropertyTreeNode> {
46  override getTraceType(): TraceType {
47    return TraceType.PROTO_LOG;
48  }
49
50  override async getEntry(index: number): Promise<PropertyTreeNode> {
51    const protologEntry = await this.queryEntry(index);
52    const logMessage: LogMessage = {
53      text: protologEntry.message,
54      tag: protologEntry.tag,
55      level: protologEntry.level,
56      at: protologEntry.location,
57      timestamp: protologEntry.timestamp,
58    };
59
60    return ParserProtologUtils.makeMessagePropertiesTree(
61      logMessage,
62      this.timestampConverter,
63      this.getRealToMonotonicTimeOffsetNs() !== undefined,
64    );
65  }
66
67  protected override getTableName(): string {
68    return 'protolog';
69  }
70
71  private async queryEntry(index: number): Promise<PerfettoLogMessageTableRow> {
72    const sql = `
73      SELECT
74        ts, tag, level, location, message
75      FROM
76        protolog
77      WHERE protolog.id = ${this.entryIndexToRowIdMap[index]};
78    `;
79    const result = await this.traceProcessor.query(sql).waitAllRows();
80
81    if (result.numRows() !== 1) {
82      throw new Error(
83        `Expected exactly 1 protolog message with id ${index} but got ${result.numRows()}`,
84      );
85    }
86
87    const entry = result.iter({});
88
89    return new PerfettoLogMessageTableRow(
90      entry.get('ts') as bigint,
91      entry.get('tag') as string,
92      entry.get('level') as string,
93      entry.get('message') as string,
94      entry.get('location') as string,
95    );
96  }
97}
98