• 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*/
15const fs = require('fs');
16const path = require('path');
17const util = require('util');
18
19let vscode = null;
20try {
21    vscode = require('vscode');
22}
23catch (err) {
24    vscode = null;
25}
26
27class NapiLog {
28    constructor() {
29    }
30}
31NapiLog.LEV_NONE = 0;
32NapiLog.LEV_ERROR = 1;
33NapiLog.LEV_DEBUG = 2;
34NapiLog.LEV_INFO = 3;
35
36const LEV_STR = ['[NON]', '[ERR]', '[DBG]', '[INF]'];
37let logLevel = NapiLog.LEV_ERROR;
38let logFileName = null;
39let logResultMessage = [true, ''];
40
41function getDateString() {
42    let nowDate = new Date();
43    return nowDate.toLocaleString();
44}
45
46function saveLog(dateStr, levStr, detail) {
47    if (logFileName) {
48        let logStr = dateStr + ' ' + levStr + ' ' + detail + '\n';
49        fs.appendFileSync(logFileName, logStr);
50    }
51}
52
53NapiLog.init = function (level, fileName) {
54    logLevel = level in [NapiLog.LEV_NONE, NapiLog.LEV_ERROR, NapiLog.LEV_DEBUG, NapiLog.LEV_INFO]
55        ? level : NapiLog.LEV_ERROR;
56    logFileName = fileName ? fileName : 'napi_generator.log';
57};
58
59/**
60 * 通过调用栈获取当前正在执行的方法名,代码行数及文件路径
61 * @param {} callerFuncName 指定取调用栈中哪个方法名所在的帧作为目标帧
62 * @returns
63 */
64NapiLog.getCallPath = function (callerFuncName = null) {
65    let callPath = '';
66    let stackArray = new Error().stack.split('\n');
67
68    // 如果没有指定目标方法,默认在调用栈中查找当前方法"getCallPath"所在的帧
69    let destFuncName = callerFuncName != null ? callerFuncName : 'getCallPath';
70
71    for (let i = stackArray.length - 1; i >= 0; --i) {
72        // debug模式和打包后的可执行程序调用栈函数名不同, 以NapiLog.log()方法为例:
73        // vscode debug模式下调用栈打印的方法名为NapiLog.log,而可执行程序的调用栈中显示为Function.log()
74        let callerMatch = (stackArray[i].indexOf('NapiLog.' + destFuncName) > 0 ||
75            stackArray[i].indexOf('Function.' + destFuncName) > 0);
76        if (callerMatch) {
77            let stackMsg = stackArray[i + 1].trim();
78            let leftIndex = stackMsg.indexOf('(');
79            let rightIndex = stackMsg.indexOf(')');
80
81            if (leftIndex > 0 && rightIndex > 0) {
82                let funInfo = stackMsg.substring(0, leftIndex);
83                let srcPath = stackMsg.substring(leftIndex + 1, rightIndex);
84                let colNumIndex = srcPath.lastIndexOf(':');
85                let colNum = srcPath.substring(colNumIndex + 1, srcPath.length);
86                let lineNumIndex = srcPath.lastIndexOf(':', colNumIndex - 1);
87                let lineNum = srcPath.substring(lineNumIndex + 1, colNumIndex);
88                let filePath = srcPath.substring(0, lineNumIndex);
89
90                callPath = util.format('%s[%s(%s:%s)]', funInfo, filePath, lineNum, colNum);
91            }
92            break;
93        }
94    }
95
96    return callPath;
97};
98
99function print(...args) {
100    if (vscode) {
101        vscode.window.showInformationMessage(...args);
102    }
103    console.log(args + '');
104}
105
106function recordLog(lev, ...args) {
107    let origMsgInfo = args;
108    let callPath = NapiLog.getCallPath('log');
109    let dataStr = getDateString();
110    let detail = args.join(' ');
111    saveLog(dataStr + ' ' + callPath, LEV_STR[lev], detail);
112    if (lev === NapiLog.LEV_ERROR) {
113        logResultMessage = [false, detail];
114    }
115    let logStr = callPath + ' ' + detail;
116    if (logLevel <= lev) {
117      return logStr;
118    }
119    NapiLog.logInfo(origMsgInfo[0]);
120    return logStr;
121}
122
123NapiLog.logError = function (...args) {
124    let logInfo = recordLog(NapiLog.LEV_ERROR, args);
125    print(logInfo);
126};
127
128NapiLog.logDebug = function (...args) {
129    recordLog(NapiLog.LEV_DEBUG, args);
130};
131
132NapiLog.logInfo = function (...args) {
133    recordLog(NapiLog.LEV_INFO, args);
134};
135
136NapiLog.getResult = function () {
137    return logResultMessage;
138};
139
140module.exports = {
141    NapiLog,
142};