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 {assertTrue} from 'common/assert_utils'; 18import {ParserTimestampConverter} from 'common/timestamp_converter'; 19import {UserNotifier} from 'common/user_notifier'; 20import {FailedToCreateTracesParser} from 'messaging/user_warnings'; 21import {TracesParserCujs} from 'parsers/events/traces_parser_cujs'; 22import {TracesParserInput} from 'parsers/input/perfetto/traces_parser_input'; 23import {TracesParserTransitions} from 'parsers/transitions/legacy/traces_parser_transitions'; 24import {Parser} from 'trace/parser'; 25import {Traces} from 'trace/traces'; 26 27export class TracesParserFactory { 28 static readonly PARSERS = [ 29 TracesParserCujs, 30 TracesParserTransitions, 31 TracesParserInput, 32 ]; 33 34 async createParsers( 35 traces: Traces, 36 timestampConverter: ParserTimestampConverter, 37 ): Promise<Array<Parser<object>>> { 38 const parsers: Array<Parser<object>> = []; 39 40 for (const ParserType of TracesParserFactory.PARSERS) { 41 let hasFoundParser = false; 42 const parser = new ParserType(traces, timestampConverter); 43 try { 44 await parser.parse(); 45 hasFoundParser = true; 46 assertTrue(parser.getLengthEntries() > 0, () => { 47 const descriptors = parser.getDescriptors(); 48 return `${descriptors.join(', ')} ${ 49 descriptors.length > 1 ? 'files have' : 'has' 50 } no relevant entries`; 51 }); 52 parsers.push(parser); 53 } catch (error) { 54 if (hasFoundParser) { 55 UserNotifier.add( 56 new FailedToCreateTracesParser( 57 parser.getTraceType(), 58 (error as Error).message, 59 ), 60 ); 61 } 62 } 63 } 64 65 return parsers; 66 } 67} 68