• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {MakeTimestampStrategyType} from 'common/time/time';
18import {ParserTimestampConverter} from 'common/time/timestamp_converter';
19import {SetFormatters} from 'parsers/operations/set_formatters';
20import {TransformToTimestamp} from 'parsers/operations/transform_to_timestamp';
21import {TIMESTAMP_NODE_FORMATTER} from 'trace/tree_node/formatters';
22import {PropertyTreeBuilderFromProto} from 'trace/tree_node/property_tree_builder_from_proto';
23import {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
24import {LogMessage} from './log_message';
25
26export class ParserProtologUtils {
27  static makeMessagePropertiesTree(
28    logMessage: LogMessage,
29    timestampConverter: ParserTimestampConverter,
30    isMonotonic: boolean,
31  ): PropertyTreeNode {
32    const tree = new PropertyTreeBuilderFromProto()
33      .setData(logMessage)
34      .setRootId('ProtoLogTrace')
35      .setRootName('entry')
36      .setVisitPrototype(false)
37      .build();
38
39    const customFormatters = new Map([['timestamp', TIMESTAMP_NODE_FORMATTER]]);
40
41    const strategy: MakeTimestampStrategyType = (valueNs: bigint) => {
42      if (isMonotonic) {
43        return timestampConverter.makeTimestampFromMonotonicNs(valueNs);
44      }
45      return timestampConverter.makeTimestampFromBootTimeNs(valueNs);
46    };
47
48    new TransformToTimestamp(['timestamp'], strategy).apply(tree);
49    new SetFormatters(undefined, customFormatters).apply(tree);
50    return tree;
51  }
52}
53