1/*
2 * Copyright 2020, 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 { FILE_TYPES, TRACE_TYPES } from '@/decode.js';
18import TraceBase from './TraceBase';
19
20import { nanos_to_string } from '@/transform.js';
21import viewerConfig from
22  '@/../../../../frameworks/base/data/etc/services.core.protolog.json';
23
24export default class ProtoLog extends TraceBase {
25  protoLogFile: any;
26
27  constructor(files) {
28    const protoLogFile = files[FILE_TYPES.PROTO_LOG];
29    super(protoLogFile.data, protoLogFile.timeline, files);
30
31    this.protoLogFile = protoLogFile;
32  }
33
34  get type() {
35    return TRACE_TYPES.PROTO_LOG;
36  }
37}
38
39export class LogMessage {
40  text: String;
41  time: String;
42  tag: String;
43  level: String;
44  at: String;
45  timestamp: Number;
46
47  constructor({ text, time, tag, level, at, timestamp }) {
48    this.text = text;
49    this.time = time;
50    this.tag = tag;
51    this.level = level;
52    this.at = at;
53    this.timestamp = timestamp;
54  }
55}
56
57export class FormattedLogMessage extends LogMessage {
58  constructor(entry) {
59    super({
60      text: (entry.messageHash.toString() +
61        ' - [' + entry.strParams.toString() +
62        '] [' + entry.sint64Params.toString() +
63        '] [' + entry.doubleParams.toString() +
64        '] [' + entry.booleanParams.toString() + ']'),
65      time: nanos_to_string(entry.elapsedRealtimeNanos),
66      tag: 'INVALID',
67      level: 'invalid',
68      at: '',
69      timestamp: entry.elapsedRealtimeNanos,
70    });
71  }
72}
73
74export class UnformattedLogMessage extends LogMessage {
75  constructor(entry, message) {
76    super({
77      text: formatText(message.message, entry),
78      time: nanos_to_string(entry.elapsedRealtimeNanos),
79      tag: viewerConfig.groups[message.group].tag,
80      level: message.level,
81      at: message.at,
82      timestamp: entry.elapsedRealtimeNanos,
83    });
84  }
85}
86
87function formatText(messageFormat, data) {
88  let out = '';
89  const strParams = data.strParams;
90  let strParamsIdx = 0;
91  const sint64Params = data.sint64Params;
92  let sint64ParamsIdx = 0;
93  const doubleParams = data.doubleParams;
94  let doubleParamsIdx = 0;
95  const booleanParams = data.booleanParams;
96  let booleanParamsIdx = 0;
97  for (let i = 0; i < messageFormat.length;) {
98    if (messageFormat[i] == '%') {
99      if (i + 1 >= messageFormat.length) {
100        // Should never happen - protologtool checks for that
101        throw new Error('Invalid format string');
102      }
103      switch (messageFormat[i + 1]) {
104        case '%':
105          out += '%';
106          break;
107        case 'd':
108          out += getParam(sint64Params, sint64ParamsIdx++).toString(10);
109          break;
110        case 'o':
111          out += getParam(sint64Params, sint64ParamsIdx++).toString(8);
112          break;
113        case 'x':
114          out += getParam(sint64Params, sint64ParamsIdx++).toString(16);
115          break;
116        case 'f':
117          out += getParam(doubleParams, doubleParamsIdx++).toFixed(6);
118          break;
119        case 'e':
120          out += getParam(doubleParams, doubleParamsIdx++).toExponential();
121          break;
122        case 'g':
123          out += getParam(doubleParams, doubleParamsIdx++).toString();
124          break;
125        case 's':
126          out += getParam(strParams, strParamsIdx++);
127          break;
128        case 'b':
129          out += getParam(booleanParams, booleanParamsIdx++).toString();
130          break;
131        default:
132          // Should never happen - protologtool checks for that
133          throw new Error('Invalid format string conversion: ' +
134            messageFormat[i + 1]);
135      }
136      i += 2;
137    } else {
138      out += messageFormat[i];
139      i += 1;
140    }
141  }
142  return out;
143}
144
145function getParam(arr, idx) {
146  if (arr.length <= idx) {
147    throw new Error('No param for format string conversion');
148  }
149  return arr[idx];
150}