1/** 2 * Copyright (c) 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 16/** 17 * Log Util 18 * 19 * standard: 20 * 1. define TAG, recommend class name. 21 * 2. switch IS_DEBUG_ON as true, when debugging. 22 * 3. msg should be short and valuable. 23 * 4. choose appropriate function. 24 * 5. the function execute many times can not print. 25 * 6. uniqueness. 26 */ 27 28import hiLog from '@ohos.hilog'; 29 30const DOMAIN: number = 0x0007; 31const TAG = 'useriam_auth_widget'; 32const SYMBOL = ' --> '; 33 34/** 35 * log package tool class 36 */ 37export class LogUtils { 38 /** 39 * Outputs debug-level logs. 40 * 41 * @param tag Identifies the log tag. 42 * @param format Indicates the log format string. 43 */ 44 debug(tag: string, format: string): void { 45 if (LogUtils.isLogGable(hiLog.LogLevel.DEBUG)) { 46 hiLog.debug(DOMAIN, TAG, tag + SYMBOL + format); 47 } 48 } 49 50 /** 51 * Outputs info-level logs. 52 * 53 * @param tag Identifies the log tag. 54 * @param format Indicates the log format string. 55 */ 56 info(tag: string, format: string): void { 57 if (LogUtils.isLogGable(hiLog.LogLevel.INFO)) { 58 hiLog.info(DOMAIN, TAG, tag + SYMBOL + format); 59 } 60 } 61 62 /** 63 * Outputs warning-level logs. 64 * 65 * @param tag Identifies the log tag. 66 * @param format Indicates the log format string. 67 */ 68 warn(tag: string, format: string): void { 69 if (LogUtils.isLogGable(hiLog.LogLevel.WARN)) { 70 hiLog.warn(DOMAIN, TAG, tag + SYMBOL + format); 71 } 72 } 73 74 /** 75 * Outputs error-level logs. 76 * 77 * @param tag Identifies the log tag. 78 * @param format Indicates the log format string. 79 */ 80 error(tag: string, format: string): void { 81 if (LogUtils.isLogGable(hiLog.LogLevel.ERROR)) { 82 hiLog.error(DOMAIN, TAG, tag + SYMBOL + format); 83 } 84 } 85 86 /** 87 * Outputs fatal-level logs. 88 * 89 * @param tag Identifies the log tag. 90 * @param format Indicates the log format string. 91 */ 92 fatal(tag: string, format: string): void { 93 if (LogUtils.isLogGable(hiLog.LogLevel.FATAL)) { 94 hiLog.fatal(DOMAIN, TAG, tag + SYMBOL + format); 95 } 96 } 97 98 /** 99 * Checks whether logs of the specified tag, and level can be printed. 100 * 101 * @param tag Identifies the log tag. 102 * @param level log level 103 */ 104 private static isLogGable(level: hiLog.LogLevel): boolean { 105 return hiLog.isLoggable(DOMAIN, TAG, level); 106 } 107} 108 109let mLogUtil = new LogUtils(); 110 111export default mLogUtil as LogUtils; 112 113