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'); 16 17class VsPluginLog { 18 constructor() { 19 } 20} 21VsPluginLog.LEV_NONE = 0; 22VsPluginLog.LEV_ERROR = 1; 23VsPluginLog.LEV_DEBUG = 2; 24VsPluginLog.LEV_INFO = 3; 25 26const LEV_STR = ["[NON]", "[ERR]", "[DBG]", "[INF]"] 27var logLevel = VsPluginLog.LEV_ERROR; 28var logFileName = null; 29var logResultMessage = [true, ""] 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 fs.appendFileSync(logFileName, logStr); 40 } 41} 42 43VsPluginLog.init = function (level, fileName) { 44 logLevel = level in [VsPluginLog.LEV_NONE, VsPluginLog.LEV_ERROR, VsPluginLog.LEV_DEBUG, VsPluginLog.LEV_INFO] 45 ? level : VsPluginLog.LEV_ERROR; 46 logFileName = fileName ? fileName : "napi_generator.log"; 47} 48 49function recordLog(lev, ...args) { 50 let dataStr = getDateString(); 51 let detail = args.join(" "); 52 saveLog(dataStr, LEV_STR[lev], detail); 53 if (lev == VsPluginLog.LEV_ERROR) { 54 logResultMessage = [false, detail]; 55 } 56 if (logLevel < lev) return; 57 VsPluginLog.logInfo(dataStr + LEV_STR[lev] + detail); 58} 59 60VsPluginLog.logError = function (...args) { 61 recordLog(VsPluginLog.LEV_ERROR, args); 62} 63 64VsPluginLog.logDebug = function (...args) { 65 recordLog(VsPluginLog.LEV_DEBUG, args); 66} 67 68VsPluginLog.logInfo = function (...args) { 69 recordLog(VsPluginLog.LEV_INFO, args); 70} 71 72VsPluginLog.getResult = function () { 73 return logResultMessage; 74} 75 76module.exports = { 77 VsPluginLog 78}