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]"] 35var logLevel = NapiLog.LEV_ERROR; 36var logFileName = null; 37var 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 57function getCallPath() { 58 let callPath = "" 59 let stackArray = new Error().stack.split('\n'); 60 for (let i = stackArray.length -1; i >=0 ; --i) { 61 if (stackArray[i].indexOf("NapiLog.log") > 0 || stackArray[i].indexOf("Function.log") > 0) { 62 let stackMsg = stackArray[i+1].trim() 63 let leftIndex = stackMsg.indexOf("(") 64 let rightIndex = stackMsg.indexOf(")") 65 66 if (leftIndex > 0 && rightIndex > 0) { 67 let funInfo = stackMsg.substring(0, leftIndex); 68 let srcPath = stackMsg.substring(leftIndex + 1, rightIndex) 69 let colNumIndex = srcPath.lastIndexOf(":") 70 let colNum = srcPath.substring(colNumIndex + 1, srcPath.length) 71 let lineNumIndex = srcPath.lastIndexOf(":", colNumIndex - 1) 72 let lineNum = srcPath.substring(lineNumIndex + 1, colNumIndex) 73 let filePath = srcPath.substring(0, lineNumIndex) 74 75 callPath = "%s[%s(%s:%s)]".format(funInfo,filePath,lineNum,colNum) 76 } 77 break; 78 } 79 } 80 81 return callPath; 82} 83 84function print(...args) { 85 if (vscode) { 86 vscode.window.showInformationMessage(...args); 87 } 88 console.log(args + ""); 89} 90 91function recordLog(lev, ...args) { 92 let origMsgInfo = args; 93 let callPath = getCallPath(); 94 let dataStr = getDateString(); 95 let detail = args.join(" "); 96 saveLog(dataStr + " " + callPath, LEV_STR[lev], detail); 97 if (lev == NapiLog.LEV_ERROR) { 98 logResultMessage = [false, detail]; 99 } 100 let logStr = callPath + " " + detail; 101 if (logLevel <= lev) return logStr; 102 NapiLog.logInfo(origMsgInfo[0]); 103 return logStr; 104} 105 106NapiLog.logError = function (...args) { 107 let logInfo = recordLog(NapiLog.LEV_ERROR, args); 108 print(logInfo); 109} 110 111NapiLog.logDebug = function (...args) { 112 recordLog(NapiLog.LEV_DEBUG, args); 113} 114 115NapiLog.logInfo = function (...args) { 116 recordLog(NapiLog.LEV_INFO, args); 117} 118 119NapiLog.getResult = function () { 120 return logResultMessage; 121} 122 123module.exports = { 124 NapiLog 125}