100aff185Sopenharmony_ci/* 200aff185Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 300aff185Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 400aff185Sopenharmony_ci * you may not use this file except in compliance with the License. 500aff185Sopenharmony_ci * You may obtain a copy of the License at 600aff185Sopenharmony_ci * 700aff185Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 800aff185Sopenharmony_ci * 900aff185Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1000aff185Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1100aff185Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1200aff185Sopenharmony_ci * See the License for the specific language governing permissions and 1300aff185Sopenharmony_ci * limitations under the License. 1400aff185Sopenharmony_ci */ 1500aff185Sopenharmony_ci 1600aff185Sopenharmony_ciimport HiLog from '@ohos.hilog'; 1700aff185Sopenharmony_ci 1800aff185Sopenharmony_ciconst DOMAIN: number = 0x0220; 1900aff185Sopenharmony_ciconst TAG: string = 'Photos'; 2000aff185Sopenharmony_ciconst COLON = ': '; 2100aff185Sopenharmony_ciconst SEPARATOR = ' '; 2200aff185Sopenharmony_ci 2300aff185Sopenharmony_ciexport class Log { 2400aff185Sopenharmony_ci static debug(className: string, message: string, ...args: string[]): boolean { 2500aff185Sopenharmony_ci if (HiLog.isLoggable(DOMAIN, TAG, HiLog.LogLevel.DEBUG)) { 2600aff185Sopenharmony_ci HiLog.debug(DOMAIN, TAG, className + COLON + message, args); 2700aff185Sopenharmony_ci return true; 2800aff185Sopenharmony_ci } 2900aff185Sopenharmony_ci return false; 3000aff185Sopenharmony_ci } 3100aff185Sopenharmony_ci 3200aff185Sopenharmony_ci static info(className: string, message: string, ...args: string[]): boolean { 3300aff185Sopenharmony_ci if (HiLog.isLoggable(DOMAIN, TAG, HiLog.LogLevel.INFO)) { 3400aff185Sopenharmony_ci HiLog.info(DOMAIN, TAG, className + COLON + message, args); 3500aff185Sopenharmony_ci return true; 3600aff185Sopenharmony_ci } 3700aff185Sopenharmony_ci return false; 3800aff185Sopenharmony_ci } 3900aff185Sopenharmony_ci 4000aff185Sopenharmony_ci static warn(className: string, message: string, ...args: string[]): boolean { 4100aff185Sopenharmony_ci if (HiLog.isLoggable(DOMAIN, TAG, HiLog.LogLevel.WARN)) { 4200aff185Sopenharmony_ci HiLog.warn(DOMAIN, TAG, className + COLON + message, args); 4300aff185Sopenharmony_ci return true; 4400aff185Sopenharmony_ci } 4500aff185Sopenharmony_ci return false; 4600aff185Sopenharmony_ci } 4700aff185Sopenharmony_ci 4800aff185Sopenharmony_ci static error(className: string, message: string, ...args: string[]): boolean { 4900aff185Sopenharmony_ci if (HiLog.isLoggable(DOMAIN, TAG, HiLog.LogLevel.ERROR)) { 5000aff185Sopenharmony_ci HiLog.error(DOMAIN, TAG, className + COLON + message, args); 5100aff185Sopenharmony_ci return true; 5200aff185Sopenharmony_ci } 5300aff185Sopenharmony_ci return false; 5400aff185Sopenharmony_ci } 5500aff185Sopenharmony_ci 5600aff185Sopenharmony_ci static fatal(className: string, message: string, ...args: string[]): boolean { 5700aff185Sopenharmony_ci if (HiLog.isLoggable(DOMAIN, TAG, HiLog.LogLevel.FATAL)) { 5800aff185Sopenharmony_ci HiLog.fatal(DOMAIN, TAG, className + COLON + message, args); 5900aff185Sopenharmony_ci return true; 6000aff185Sopenharmony_ci } 6100aff185Sopenharmony_ci return false; 6200aff185Sopenharmony_ci } 6300aff185Sopenharmony_ci 6400aff185Sopenharmony_ci /** 6500aff185Sopenharmony_ci * 使用方法 直接逗号分隔开: 6600aff185Sopenharmony_ci ``` 6700aff185Sopenharmony_ci Log.debug(TAG, `params = , ${JSON.stringify(param1)}, ${JSON.stringify(param2)...`) 6800aff185Sopenharmony_ci 简化为 Log.d(TAG, 'params = ', param1, param2...) 6900aff185Sopenharmony_ci Log.error(TAG, `${JSON.stringify(err)obj = key1: ${JSON.stringify(v1)}, key2: ${JSON.stringify(v2)...`) 7000aff185Sopenharmony_ci 简化为 Log.e(TAG, err, 'obj = ', { key1: v1, key2: v2 }) 7100aff185Sopenharmony_ci ``` 7200aff185Sopenharmony_ci */ 7300aff185Sopenharmony_ci static d(className: string, ...args): void { 7400aff185Sopenharmony_ci return HiLog.debug(DOMAIN, TAG, className + COLON + this.join(...args)); 7500aff185Sopenharmony_ci } 7600aff185Sopenharmony_ci 7700aff185Sopenharmony_ci static i(className: string, ...args): void { 7800aff185Sopenharmony_ci return HiLog.info(DOMAIN, TAG, className + COLON + this.join(...args)); 7900aff185Sopenharmony_ci } 8000aff185Sopenharmony_ci 8100aff185Sopenharmony_ci static w(className: string, ...args): void { 8200aff185Sopenharmony_ci return HiLog.warn(DOMAIN, TAG, className + COLON + this.join(...args)); 8300aff185Sopenharmony_ci } 8400aff185Sopenharmony_ci 8500aff185Sopenharmony_ci static e(className: string, ...args): void { 8600aff185Sopenharmony_ci return HiLog.error(DOMAIN, TAG, className + COLON + this.join(...args)); 8700aff185Sopenharmony_ci } 8800aff185Sopenharmony_ci 8900aff185Sopenharmony_ci static f(className: string, ...args): void { 9000aff185Sopenharmony_ci return HiLog.fatal(DOMAIN, TAG, className + COLON + this.join(...args)); 9100aff185Sopenharmony_ci } 9200aff185Sopenharmony_ci 9300aff185Sopenharmony_ci static stringify(a): string { 9400aff185Sopenharmony_ci let res: string; 9500aff185Sopenharmony_ci if (typeof a !== 'string') { 9600aff185Sopenharmony_ci try { 9700aff185Sopenharmony_ci res = JSON.stringify(a); 9800aff185Sopenharmony_ci HiLog.debug(DOMAIN, TAG, a); 9900aff185Sopenharmony_ci } catch (e) { 10000aff185Sopenharmony_ci if (e) { 10100aff185Sopenharmony_ci HiLog.error(DOMAIN, TAG, `${e} type: ${typeof a}, ${a}, catch error: ${JSON.stringify(e)}`); 10200aff185Sopenharmony_ci } 10300aff185Sopenharmony_ci res = e; 10400aff185Sopenharmony_ci } 10500aff185Sopenharmony_ci } 10600aff185Sopenharmony_ci if (res === '{}') { 10700aff185Sopenharmony_ci try { 10800aff185Sopenharmony_ci res = String(a); 10900aff185Sopenharmony_ci } catch (e) { 11000aff185Sopenharmony_ci if (e) { 11100aff185Sopenharmony_ci HiLog.warn(DOMAIN, TAG, `${e} type: ${typeof a}, ${a}, catch error: ${JSON.stringify(e)}`); 11200aff185Sopenharmony_ci } 11300aff185Sopenharmony_ci } 11400aff185Sopenharmony_ci } 11500aff185Sopenharmony_ci return res ?? a; 11600aff185Sopenharmony_ci } 11700aff185Sopenharmony_ci 11800aff185Sopenharmony_ci static join(...args): string { 11900aff185Sopenharmony_ci for (let i = 0; i < args.length; i++) { 12000aff185Sopenharmony_ci try { 12100aff185Sopenharmony_ci args[i] = this.stringify(args[i]); 12200aff185Sopenharmony_ci } catch (e) { 12300aff185Sopenharmony_ci HiLog.warn(DOMAIN, TAG, `${e} type: ${typeof args[i]}, ${args[i]}, catch error: ${JSON.stringify(e)}`); 12400aff185Sopenharmony_ci } 12500aff185Sopenharmony_ci } 12600aff185Sopenharmony_ci return args.join(SEPARATOR); 12700aff185Sopenharmony_ci } 12800aff185Sopenharmony_ci} 129