1/** 2 * Copyright (c) 2021-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 */ 15import hilog from '@ohos.hilog'; 16 17const DOMAIN: number = 0x001b; 18const TAG = "Launcher_Default"; 19const SYMBOL = " --> "; 20 21/** 22 * Basic log class 23 */ 24export class Log { 25 /** 26 * Outputs info-level logs. 27 * 28 * @param tag Identifies the log tag. 29 * @param format Indicates the log format string. 30 * @param args Indicates the log parameters. 31 * @since 7 32 */ 33 static showInfo(tag: string, format: string, ...args: any[]) { 34 if (Log.isLoggable(tag, hilog.LogLevel.INFO)) { 35 hilog.info(DOMAIN, TAG, tag + SYMBOL + format, args); 36 } 37 } 38 39 /** 40 * Outputs debug-level logs. 41 * 42 * @param tag Identifies the log tag. 43 * @param format Indicates the log format string. 44 * @param args Indicates the log parameters. 45 * @since 7 46 */ 47 static showDebug(tag: string, format: string, ...args: any[]) { 48 if (Log.isLoggable(tag, hilog.LogLevel.DEBUG)) { 49 hilog.debug(DOMAIN, TAG, tag + SYMBOL + format, args); 50 } 51 } 52 53 /** 54 * Outputs warning-level logs. 55 * 56 * @param tag Identifies the log tag. 57 * @param format Indicates the log format string. 58 * @param args Indicates the log parameters. 59 * @since 7 60 */ 61 static showWarn(tag: string, format: string, ...args: any[]) { 62 if (Log.isLoggable(tag, hilog.LogLevel.WARN)) { 63 hilog.warn(DOMAIN, TAG, tag + SYMBOL + format, args); 64 } 65 } 66 67 /** 68 * Outputs error-level logs. 69 * 70 * @param tag Identifies the log tag. 71 * @param format Indicates the log format string. 72 * @param args Indicates the log parameters. 73 * @since 7 74 */ 75 static showError(tag: string, format: string, ...args: any[]) { 76 if (Log.isLoggable(tag, hilog.LogLevel.ERROR)) { 77 hilog.error(DOMAIN, TAG, tag + SYMBOL + format, args); 78 } 79 } 80 81 /** 82 * Outputs fatal-level logs. 83 * 84 * @param tag Identifies the log tag. 85 * @param format Indicates the log format string. 86 * @param args Indicates the log parameters. 87 * @since 7 88 */ 89 static showFatal(tag: string, format: string, ...args: any[]) { 90 if (Log.isLoggable(tag, hilog.LogLevel.FATAL)) { 91 hilog.fatal(DOMAIN, TAG, tag + SYMBOL + format, args); 92 } 93 } 94 95 /** 96 * Checks whether logs of the specified tag, and level can be printed. 97 * 98 * @param tag Identifies the log tag. 99 * @param level log level 100 * @since 7 101 */ 102 private static isLoggable(tag:string, level: hilog.LogLevel): boolean { 103 return hilog.isLoggable(DOMAIN, tag, level); 104 } 105} 106