1/*
2 * Copyright (C) 2023 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 {TimeRange} from 'common/time';
18import {TimeDuration} from 'common/time_duration';
19import {TraceType} from 'trace/trace_type';
20import {UserWarning} from './user_warning';
21
22export class CorruptedArchive extends UserWarning {
23  constructor(private readonly file: File) {
24    super();
25  }
26
27  getDescriptor(): string {
28    return 'corrupted archive';
29  }
30
31  getMessage(): string {
32    return `${this.file.name}: corrupted archive`;
33  }
34}
35
36export class NoInputFiles extends UserWarning {
37  getDescriptor(): string {
38    return 'no input';
39  }
40
41  getMessage(): string {
42    return `Input has no valid trace files`;
43  }
44}
45
46export class TraceHasOldData extends UserWarning {
47  constructor(
48    private readonly descriptor: string,
49    private readonly timeGap?: TimeRange,
50  ) {
51    super();
52  }
53
54  getDescriptor(): string {
55    return 'old trace';
56  }
57
58  getMessage(): string {
59    const elapsedTime = this.timeGap
60      ? new TimeDuration(
61          this.timeGap.to.getValueNs() - this.timeGap.from.getValueNs(),
62        )
63      : undefined;
64    return (
65      `${this.descriptor}: discarded because data is old` +
66      (this.timeGap ? `er than ${elapsedTime?.format()}` : '')
67    );
68  }
69}
70
71export class TraceOverridden extends UserWarning {
72  constructor(
73    private readonly descriptor: string,
74    private readonly overridingType?: TraceType,
75  ) {
76    super();
77  }
78
79  getDescriptor(): string {
80    return 'trace overridden';
81  }
82
83  getMessage(): string {
84    if (this.overridingType !== undefined) {
85      return `${this.descriptor}: overridden by another trace of type ${
86        TraceType[this.overridingType]
87      }`;
88    }
89    return `${this.descriptor}: overridden by another trace of same type`;
90  }
91}
92
93export class UnsupportedFileFormat extends UserWarning {
94  constructor(private readonly descriptor: string) {
95    super();
96  }
97
98  getDescriptor(): string {
99    return 'unsupported format';
100  }
101
102  getMessage(): string {
103    return `${this.descriptor}: unsupported format`;
104  }
105}
106
107export class InvalidPerfettoTrace extends UserWarning {
108  constructor(
109    private readonly descriptor: string,
110    private readonly parserErrorMessages: string[],
111  ) {
112    super();
113  }
114
115  getDescriptor(): string {
116    return 'invalid perfetto trace';
117  }
118
119  getMessage(): string {
120    return `${this.descriptor}: ${this.parserErrorMessages.join(', ')}`;
121  }
122}
123