• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 */
15
16import HiLog from '@ohos.hilog';
17
18const DOMAIN: number = 0x0220;
19const TAG: string = 'Photos';
20const COLON = ': ';
21const SEPARATOR = ' ';
22
23export class Log {
24  static debug(className: string, message: string, ...args: string[]): boolean {
25    if (HiLog.isLoggable(DOMAIN, TAG, HiLog.LogLevel.DEBUG)) {
26      HiLog.debug(DOMAIN, TAG, className + COLON + message, args);
27      return true;
28    }
29    return false;
30  }
31
32  static info(className: string, message: string, ...args: string[]): boolean {
33    if (HiLog.isLoggable(DOMAIN, TAG, HiLog.LogLevel.INFO)) {
34      HiLog.info(DOMAIN, TAG, className + COLON + message, args);
35      return true;
36    }
37    return false;
38  }
39
40  static warn(className: string, message: string, ...args: string[]): boolean {
41    if (HiLog.isLoggable(DOMAIN, TAG, HiLog.LogLevel.WARN)) {
42      HiLog.warn(DOMAIN, TAG, className + COLON + message, args);
43      return true;
44    }
45    return false;
46  }
47
48  static error(className: string, message: string, ...args: string[]): boolean {
49    if (HiLog.isLoggable(DOMAIN, TAG, HiLog.LogLevel.ERROR)) {
50      HiLog.error(DOMAIN, TAG, className + COLON + message, args);
51      return true;
52    }
53    return false;
54  }
55
56  static fatal(className: string, message: string, ...args: string[]): boolean {
57    if (HiLog.isLoggable(DOMAIN, TAG, HiLog.LogLevel.FATAL)) {
58      HiLog.fatal(DOMAIN, TAG, className + COLON + message, args);
59      return true;
60    }
61    return false;
62  }
63
64  /**
65   * 使用方法 直接逗号分隔开:
66   ```
67   Log.debug(TAG, `params = , ${JSON.stringify(param1)},  ${JSON.stringify(param2)...`)
68   简化为 Log.d(TAG, 'params = ', param1, param2...)
69   Log.error(TAG, `${JSON.stringify(err)obj = key1: ${JSON.stringify(v1)}, key2: ${JSON.stringify(v2)...`)
70   简化为 Log.e(TAG, err, 'obj = ', { key1: v1, key2: v2 })
71   ```
72   */
73  static d(className: string, ...args): void {
74    return HiLog.debug(DOMAIN, TAG, className + COLON + this.join(...args));
75  }
76
77  static i(className: string, ...args): void {
78    return HiLog.info(DOMAIN, TAG, className + COLON + this.join(...args));
79  }
80
81  static w(className: string, ...args): void {
82    return HiLog.warn(DOMAIN, TAG, className + COLON + this.join(...args));
83  }
84
85  static e(className: string, ...args): void {
86    return HiLog.error(DOMAIN, TAG, className + COLON + this.join(...args));
87  }
88
89  static f(className: string, ...args): void {
90    return HiLog.fatal(DOMAIN, TAG, className + COLON + this.join(...args));
91  }
92
93  static stringify(a): string {
94    let res: string;
95    if (typeof a !== 'string') {
96      try {
97        res = JSON.stringify(a);
98        HiLog.debug(DOMAIN, TAG, a);
99      } catch (e) {
100        if (e) {
101          HiLog.error(DOMAIN, TAG, `${e} type: ${typeof a}, ${a}, catch error: ${JSON.stringify(e)}`);
102        }
103        res = e;
104      }
105    }
106    if (res === '{}') {
107      try {
108        res = String(a);
109      } catch (e) {
110        if (e) {
111          HiLog.warn(DOMAIN, TAG, `${e} type: ${typeof a}, ${a}, catch error: ${JSON.stringify(e)}`);
112        }
113      }
114    }
115    return res ?? a;
116  }
117
118  static join(...args): string {
119    for (let i = 0; i < args.length; i++) {
120      try {
121        args[i] = this.stringify(args[i]);
122      } catch (e) {
123        HiLog.warn(DOMAIN, TAG, `${e} type: ${typeof args[i]}, ${args[i]}, catch error: ${JSON.stringify(e)}`);
124      }
125    }
126    return args.join(SEPARATOR);
127  }
128}
129