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