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