/* * Copyright (C) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ export class PluginConvertUtils { private static crlf: string = '\n'; private static leftBrace: string = '{'; private static rightBrace: string = '}'; static pluginConfig: unknown[] = []; public static createHdcCmd(requestString: string, outputPath: string, time: number): string { return `hiprofiler_cmd \\${this.crlf} -c - \\${this.crlf} -o ${outputPath} \\${this.crlf} -t ${time} \\${this.crlf} -s \\${this.crlf} -k \\${this.crlf}<= 0 || value.startsWith('IO_REPORT')) { prefixText = `${prefixText + ' '.repeat(spacesNumber).repeat(indentation + 1) + //@ts-ignore this.humpToSnake(key)}: ${value.toString()}${this.crlf}`; } else { prefixText = `${prefixText + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)}: "${ //@ts-ignore value.toString() }"${this.crlf}`; } return prefixText; } private static handleArray( key: string, arr: Array, indentation: number, needColon: boolean, spacesNumber: number ): string { let text = ''; arr.forEach((arrValue): void => { switch (typeof arrValue) { case 'bigint': text = this.handleArrayByBigint(text, spacesNumber, indentation, key, arrValue); break; case 'boolean': text = this.handleArrayByBoolean(text, spacesNumber, indentation, key, arrValue); break; case 'number': text = this.handleArrayByNumber(text, spacesNumber, indentation, key, arrValue); break; case 'string': if (arrValue === '') { break; } text = this.handleArrayByStr(text, spacesNumber, indentation, key, arrValue); break; case 'object': default: text = this.handleArrayByDefault(text, spacesNumber, indentation, key, arrValue, needColon); } }); return text; } private static handleArrayByBigint( text: string, spacesNumber: number, indentation: number, key: string, arrValue: unknown ): string { //@ts-ignore return `${text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)}: ${arrValue.toString()}${ this.crlf }`; } private static handleArrayByBoolean( text: string, spacesNumber: number, indentation: number, key: string, arrValue: unknown ): string { //@ts-ignore return `${text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)}: ${arrValue.toString()}${ this.crlf }`; } private static handleArrayByNumber( text: string, spacesNumber: number, indentation: number, key: string, arrValue: unknown ): string { //@ts-ignore return `${text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)}: ${arrValue.toString()}${ this.crlf }`; } private static handleArrayByStr( text: string, spacesNumber: number, indentation: number, key: string, arrValue: unknown ): string { //@ts-ignore if (arrValue.startsWith('VMEMINFO') || arrValue.startsWith('PMEM')) { text = `${ text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key) //@ts-ignore }: ${arrValue.toString()}${this.crlf}`; //@ts-ignore } else if (arrValue.startsWith('REAL_BATTERY') || arrValue.startsWith('THERMAL_REPORT')) { text = `${text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key) //@ts-ignore }: ${arrValue.toString()}${this.crlf}`; } else { text = `${ text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key) //@ts-ignore }: "${arrValue.trim().toString()}"${this.crlf}`; } return text; } private static handleArrayByDefault( text: string, spacesNumber: number, indentation: number, key: string, arrValue: unknown, needColon: boolean ): string { if (needColon) { text = `${text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key)}: ${this.handleObj( //@ts-ignore arrValue, indentation + 1, needColon, spacesNumber )}${this.crlf}`; } else { text = `${ text + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key) + //@ts-ignore this.handleObj(arrValue, indentation + 1, needColon, spacesNumber) }${this.crlf}`; } return text; } // 驼峰转snake private static humpToSnake(humpString: string): string { return humpString.replace(/[A-Z]/g, (value) => `_${value.toLowerCase()}`); } private static getMontageStrings( prefixText: string, spacesNumber: number, indentation: number, key: string, value: T ): string { return `${ prefixText + ' '.repeat(spacesNumber).repeat(indentation + 1) + this.humpToSnake(key) }: ${value.toString()}${this.crlf}`; } } const LevelConfigEnumList: string[] = ['LEVEL_UNSPECIFIED', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL']; export const ffrtEnumList: string[] = ['BOOTTIME', 'REALTIME', 'REALTIME_COARSE', 'MONOTONIC', 'MONOTONIC_COARSE', 'MONOTONIC_RAW']; const configEnumList: string[] = [...LevelConfigEnumList, ...ffrtEnumList];