• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2021 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 LogUtil from './LogUtil';
16import ConfigData from './ConfigData';
17
18/**
19 * Method log decorator
20 */
21const LogMethod = (target: Object, methodName: string, propertyDescriptor: PropertyDescriptor): PropertyDescriptor => {
22  const method = propertyDescriptor.value;
23
24  propertyDescriptor.value = function (...args: any[]) {
25    const params = args.map(a => JSON.stringify(a)).join();
26    LogUtil.info(ConfigData.TAG + `${target.constructor.name}#${methodName}(${params}) in `);
27
28    const result = method.apply(this, args);
29    const r = JSON.stringify(result);
30
31    LogUtil.info(ConfigData.TAG + `${target.constructor.name}#${methodName}(${params}) out => ${r}`);
32    return result;
33  }
34
35  return propertyDescriptor;
36};
37
38/**
39 * Class decorator to log all methods
40 */
41export const LogAll = (target: any) => {
42  Reflect.ownKeys(target.prototype).forEach(propertyKey => {
43    let propertyDescriptor: PropertyDescriptor = Object.getOwnPropertyDescriptor(target.prototype, propertyKey);
44    const method = propertyDescriptor.value;
45
46    if (method) {
47      propertyDescriptor.value = function (...args: any[]) {
48        const params = args.map(a => JSON.stringify(a)).join();
49        LogUtil.info(ConfigData.TAG + `${target.name}#${propertyKey.toString()}(${params}) in `);
50
51        const result = method.apply(this, args);
52        const r = JSON.stringify(result);
53
54        LogUtil.info(ConfigData.TAG + `${target.name}#${propertyKey.toString()}(${params}) out => ${r}`);
55        return result;
56      }
57
58      Object.defineProperty(target.prototype, propertyKey, propertyDescriptor);
59    }
60  });
61}
62
63export default LogMethod;
64