1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import * as fs from 'fs'; 16import * as path from 'path'; 17import * as arkts from '@koalaui/libarkts'; 18 19const isDebugLog: boolean = false; 20const isDebugDump: boolean = false; 21const isPerformance: boolean = false; 22arkts.Performance.getInstance().skip(!isPerformance); 23 24export function getEnumName(enumType: any, value: number): string | undefined { 25 return enumType[value]; 26} 27 28function mkDir(filePath: string): void { 29 const parent = path.join(filePath, '..'); 30 if (!(fs.existsSync(parent) && !fs.statSync(parent).isFile())) { 31 mkDir(parent); 32 } 33 fs.mkdirSync(filePath); 34} 35 36export function debugDump( 37 content: string, 38 fileName: string, 39 isInit: boolean, 40 cachePath: string | undefined, 41 programFileName: string 42): void { 43 if (!isDebugDump) return; 44 const currentDirectory = process.cwd(); 45 const modifiedFileName = programFileName.replaceAll('.', '_'); 46 const outputDir: string = cachePath 47 ? path.resolve(currentDirectory, cachePath, modifiedFileName) 48 : path.resolve(currentDirectory, 'dist', 'cache', modifiedFileName); 49 const filePath: string = path.resolve(outputDir, fileName); 50 if (!fs.existsSync(outputDir)) { 51 mkDir(outputDir); 52 } 53 try { 54 if (!isInit && fs.existsSync(filePath)) { 55 const existingContent = fs.readFileSync(filePath, 'utf8'); 56 const newContent = 57 existingContent && !existingContent.endsWith('\n') 58 ? existingContent + '\n' + content 59 : existingContent + content; 60 fs.writeFileSync(filePath, newContent, 'utf8'); 61 } else { 62 fs.writeFileSync(filePath, content, 'utf8'); 63 } 64 } catch (error) { 65 console.error('文件操作失败:', error); 66 } 67} 68 69export function debugLog(message?: any, ...optionalParams: any[]): void { 70 if (!isDebugLog) return; 71 console.log(message, ...optionalParams); 72} 73 74export function getDumpFileName(state: number, prefix: string, index: number | undefined, suffix: string): string { 75 return `${state}_${prefix}_${index ?? ''}_${suffix}.sts`; 76} 77