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 = 0x001A; 18const TAG = "SystemUI_Default"; 19const SYMBOL = " --> "; 20const FILTER_KEYS = [ 21 new RegExp('hide', "gi") 22] 23 24export function filterKey(target: any, propKey: string, descriptor: PropertyDescriptor) { 25 const original = descriptor.value; 26 descriptor.value = function (...args: string[]) { 27 let filterResult = args.map((str) => { 28 let tempStr = str 29 FILTER_KEYS.forEach((filterKey) => tempStr = tempStr.replace(filterKey, "**")) 30 return tempStr 31 }); 32 const result = original.call(this, ...filterResult); 33 return result; 34 }; 35} 36 37/** 38 * Basic log class 39 */ 40export default class Log { 41 /** 42 * Outputs debug-level logs. 43 * 44 * @param tag Identifies the log tag. 45 * @param format Indicates the log format string. 46 * @param args Indicates the log parameters. 47 * @since 7 48 */ 49 static showDebug(tag: string, format: string, ...args: any[]) { 50 if (Log.isLoggable(HiLog.LogLevel.DEBUG)) { 51 HiLog.debug(DOMAIN, TAG, tag + SYMBOL + format, args); 52 } 53 } 54 55 /** 56 * Outputs info-level logs. 57 * 58 * @param tag Identifies the log tag. 59 * @param format Indicates the log format string. 60 * @param args Indicates the log parameters. 61 * @since 7 62 */ 63 static showInfo(tag: string, format: string, ...args: any[]) { 64 if (Log.isLoggable(HiLog.LogLevel.INFO)) { 65 HiLog.info(DOMAIN, TAG, tag + SYMBOL + format, args); 66 } 67 } 68 69 /** 70 * Outputs warning-level logs. 71 * 72 * @param tag Identifies the log tag. 73 * @param format Indicates the log format string. 74 * @param args Indicates the log parameters. 75 * @since 7 76 */ 77 static showWarn(tag: string, format: string, ...args: any[]) { 78 if (Log.isLoggable(HiLog.LogLevel.WARN)) { 79 HiLog.warn(DOMAIN, TAG, tag + SYMBOL + format, args); 80 } 81 } 82 83 /** 84 * Outputs error-level logs. 85 * 86 * @param tag Identifies the log tag. 87 * @param format Indicates the log format string. 88 * @param args Indicates the log parameters. 89 * @since 7 90 */ 91 static showError(tag: string, format: string, ...args: any[]) { 92 if (Log.isLoggable(HiLog.LogLevel.ERROR)) { 93 HiLog.error(DOMAIN, TAG, tag + SYMBOL + format, args); 94 } 95 } 96 97 /** 98 * Outputs fatal-level logs. 99 * 100 * @param tag Identifies the log tag. 101 * @param format Indicates the log format string. 102 * @param args Indicates the log parameters. 103 * @since 7 104 */ 105 static showFatal(tag: string, format: string, ...args: any[]) { 106 if (Log.isLoggable(HiLog.LogLevel.FATAL)) { 107 HiLog.fatal(DOMAIN, TAG, tag + SYMBOL + format, args); 108 } 109 } 110 111 /** 112 * Checks whether logs of the specified tag, and level can be printed. 113 * 114 * @param tag Identifies the log tag. 115 * @param level log level 116 * @since 7 117 */ 118 private static isLoggable(level: HiLog.LogLevel): boolean { 119 return HiLog.isLoggable(DOMAIN, TAG, level); 120 } 121} 122