• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 Shenzhen Kaihong Digital Industry Development 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
16class NapiLog {
17  constructor() {}
18}
19NapiLog.LEV_NONE = 0;
20NapiLog.LEV_ERROR = 1;
21NapiLog.LEV_DEBUG = 2;
22NapiLog.LEV_INFO = 3;
23
24const LEV_STR = ['[NON]', '[ERR]', '[DBG]', '[INF]'];
25var logLevel = NapiLog.LEV_INFO;
26var logFileName = null;
27var logResultMessage = [true, ''];
28var errorCallBack = null;
29
30function getDateString() {
31  let nowDate = new Date();
32  return nowDate.toLocaleString();
33}
34
35function saveLog(dateStr, levStr, detail) {
36  if (logFileName) {
37    let logStr = dateStr + ' ' + levStr + ' ' + detail + '\n';
38  }
39}
40
41NapiLog.init = function (level, fileName) {
42  logLevel =
43    level in
44    [NapiLog.LEV_NONE, NapiLog.LEV_ERROR, NapiLog.LEV_DEBUG, NapiLog.LEV_INFO]
45      ? level
46      : NapiLog.LEV_ERROR;
47  logFileName = fileName ? fileName : 'napi_generator.log';
48};
49
50function recordLog(lev, ...args) {
51  let dataStr = getDateString();
52  let detail = args.join(' ');
53  saveLog(dataStr, LEV_STR[lev], detail);
54  if (lev == NapiLog.LEV_ERROR) {
55    logResultMessage = [false, detail];
56    if (errorCallBack != null) errorCallBack(detail);
57  }
58  if (logLevel < lev) return;
59  console.log(dataStr, LEV_STR[lev], detail);
60}
61
62NapiLog.logError = function (...args) {
63  recordLog(NapiLog.LEV_ERROR, args);
64};
65
66NapiLog.logDebug = function (...args) {
67  recordLog(NapiLog.LEV_DEBUG, args);
68};
69
70NapiLog.logInfo = function (...args) {
71  recordLog(NapiLog.LEV_INFO, args);
72};
73
74NapiLog.getResult = function () {
75  return logResultMessage;
76};
77
78NapiLog.clearError = function () {
79  logResultMessage = [true, ''];
80};
81
82NapiLog.registError = function (func) {
83  errorCallBack = func;
84};
85
86module.exports = {
87  NapiLog,
88};
89