• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { ARKUI_ERROR_CODE_IN_TS_DIAGNOSTIC, ARKUI_SUBSYSTEM_CODE, TSC_SYSTEM_CODE } from "./common";
2
3/*
4 * Copyright (c) 2024 Huawei Device Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17enum LogType {
18	ERROR = 'ERROR',
19	WARN = 'WARN',
20	NOTE = 'NOTE'
21  }
22
23interface MoreInfo {
24    cn: string;
25    en: string;
26}
27
28interface HvigorLogInfo {
29	code: string;
30    description?: string;
31    cause: string;
32    position?: string;
33    solutions?: string[];
34    moreInfo?: MoreInfo;
35}
36
37interface LogInfo extends Partial<HvigorLogInfo> {
38  type?: LogType,
39  message: string,
40  pos?: number,
41  line?: number,
42  column?: number,
43  fileName?: string
44}
45
46class Logger {
47	private prefix: string;
48	private errorInfos: LogInfo[] = [];
49	private warnInfos: LogInfo[] = [];
50	private noteInfos: LogInfo[] = [];
51	static instances: Logger[] = [];
52
53	constructor(prefix: string) {
54		this.prefix = prefix;
55	}
56
57	public getErrorInfos(): LogInfo[] {
58		return this.errorInfos;
59	}
60
61	public getWarnInfos(): LogInfo[] {
62		return this.warnInfos;
63	}
64
65	public getNoteInfos(): LogInfo[] {
66		return this.noteInfos;
67	}
68
69	public pushErrorInfos(infos: LogInfo[]): void {
70		this.errorInfos = this.errorInfos.concat(infos);
71	}
72
73	public pushWarnInfos(infos: LogInfo[]): void {
74		this.warnInfos = this.warnInfos.concat(infos);
75	}
76
77	public pushNoteInfos(infos: LogInfo[]): void {
78		this.noteInfos = this.noteInfos.concat(infos);
79	}
80
81	public debug(msg: string) {
82		console.debug(msg);
83	}
84
85	public printError(info: LogInfo) {
86		if (this.prefix === ARKUI_SUBSYSTEM_CODE) {
87			this.errorInfos.push(info);
88		} else if (this.prefix === TSC_SYSTEM_CODE && ARKUI_ERROR_CODE_IN_TS_DIAGNOSTIC.includes(info.code)) {
89			this.errorInfos.push(info);
90		}
91	}
92
93	public printWarn(msg: string) {
94		if (this.prefix === ARKUI_SUBSYSTEM_CODE) {
95			this.warnInfos.push({ message: msg });
96		}
97	}
98
99	public printInfo(msg: string) {
100		if (this.prefix === ARKUI_SUBSYSTEM_CODE) {
101			this.noteInfos.push({ message: msg });
102		}
103	}
104
105	public error(msg: string) {
106		this.errorInfos.push({ message: msg });
107	}
108
109	public warn(msg: string) {
110		this.warnInfos.push({ message: msg });
111	}
112
113	public info(msg: string) {
114		this.noteInfos.push({ message: msg });
115	}
116
117	public getPrefix() {
118		return this.prefix;
119	}
120
121	public static getLogger(prefix: string): Logger {
122		for (const instance of Logger.instances) {
123			if (instance.getPrefix() == prefix) {
124				return instance;
125			}
126		}
127	}
128
129	public static createLogger(prefix: string): Logger {
130		const logger = new Logger(prefix);
131		Logger.instances.push(logger);
132		return logger;
133	}
134
135	public static flush(): void {
136		Logger.instances = [];
137	}
138
139	public static removeLogger(prefix: string): void {
140		Logger.instances = Logger.instances.filter(
141			(instance) => instance.getPrefix() !== prefix
142		);
143	}
144
145	public static mergeLoggers(...prefixes: string[]): Logger {
146		const logger = new Logger('__merged__');
147		for (const instance of Logger.instances) {
148			if (prefixes.includes(instance.getPrefix())) {
149				logger.pushErrorInfos(instance.getErrorInfos());
150				logger.pushWarnInfos(instance.getWarnInfos());
151				logger.pushNoteInfos(instance.getNoteInfos());
152			}
153		}
154		return logger;
155	}
156}
157
158export {
159	Logger,
160	HvigorLogInfo,
161	LogInfo
162}