xref: /developtools/smartperf_host/ide/src/log/Log.ts (revision fb726d48)
1/*
2 * Copyright (C) 2022 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 */
15
16export enum LogLevel {
17  OFF = Number.MAX_VALUE,
18  ERROR = 4000,
19  WARN = 3000,
20  INFO = 2000,
21  DEBUG = 1000,
22  TRACE = 500,
23  ALL = Number.MIN_VALUE,
24}
25
26export const error = (message?: unknown, ...optionalParams: unknown[]): void => {
27  SpLog.logger(LogLevel.ERROR, message, ...optionalParams);
28};
29export const warn = (message?: unknown, ...optionalParams: unknown[]): void => {
30  SpLog.logger(LogLevel.WARN, message, ...optionalParams);
31};
32export const info = (message?: unknown, ...optionalParams: unknown[]): void => {
33  SpLog.logger(LogLevel.INFO, message, ...optionalParams);
34};
35export const debug = (message?: unknown, ...optionalParams: unknown[]): void => {
36  SpLog.logger(LogLevel.DEBUG, message, ...optionalParams);
37};
38export const trace = (message?: unknown, ...optionalParams: unknown[]): void => {
39  SpLog.logger(LogLevel.TRACE, message, ...optionalParams);
40};
41export const log = (message?: unknown): void => {
42  SpLog.logger(LogLevel.TRACE, message);
43};
44
45class SpLog {
46  private static nowLogLevel: LogLevel = LogLevel.OFF;
47
48  public static getNowLogLevel(): LogLevel {
49    return this.nowLogLevel;
50  }
51
52  public static setLogLevel(logLevel: LogLevel): void {
53    SpLog.nowLogLevel = logLevel;
54  }
55  private static now(): string {
56    const now = new Date();
57    const timeString = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}:${now.getMilliseconds()}`;
58    return timeString;
59  }
60
61  public static logger(logLevel: LogLevel, message?: unknown, ...optionalParams: unknown[]): void {
62    if (logLevel >= SpLog.nowLogLevel) {
63      switch (logLevel) {
64        case LogLevel.ERROR:
65          console.error(message, ...optionalParams, this.now());
66          break;
67        case LogLevel.WARN:
68          console.warn(message, ...optionalParams, this.now());
69          break;
70        case LogLevel.INFO:
71          console.info(message, ...optionalParams, this.now());
72          break;
73        case LogLevel.DEBUG:
74          console.debug(message, ...optionalParams, this.now());
75          break;
76        case LogLevel.TRACE:
77          console.trace(message, ...optionalParams, this.now());
78          break;
79        default:
80          console.log(message, this.now());
81      }
82    }
83  }
84}
85