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