1/** 2 * Copyright (c) 2021-2023 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 16import hiLog from '@ohos.hilog'; 17 18const DOMAIN: number = 0x0500; 19const TAG = 'SettingsData'; 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 info(format: string, ...args: string[]): void { 34 if (Log.isLoggable(hiLog.LogLevel.INFO)) { 35 hiLog.info(DOMAIN, TAG, 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 debug(format: string, ...args: string[]): void { 48 if (Log.isLoggable(hiLog.LogLevel.DEBUG)) { 49 hiLog.debug(DOMAIN, TAG, 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 warn(format: string, ...args: string[]): void { 62 if (Log.isLoggable(hiLog.LogLevel.WARN)) { 63 hiLog.warn(DOMAIN, TAG, 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 error(format: string, ...args: string[]): void { 76 if (Log.isLoggable(hiLog.LogLevel.ERROR)) { 77 hiLog.error(DOMAIN, TAG, 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 fatal(format: string, ...args: string[]): void { 90 if (Log.isLoggable(hiLog.LogLevel.FATAL)) { 91 hiLog.fatal(DOMAIN, TAG, 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(level: hiLog.LogLevel): boolean { 103 return hiLog.isLoggable(DOMAIN, TAG, level); 104 } 105} 106