mirror of
https://gitee.com/openharmony/napi_generator
synced 2025-02-17 07:37:46 +00:00
!35 Add the log information of the plug
Merge pull request !35 from bayanxing/master
This commit is contained in:
commit
578b4bc242
@ -19,6 +19,7 @@ const vscode = require('vscode');
|
||||
const xgen = require('./gen/main');
|
||||
const path = require("path");
|
||||
const { CheckFileError } = require("./gen/tools/common");
|
||||
const { NLog } = require("./gen/tools/NLog");
|
||||
// this method is called when your extension is activated
|
||||
// your extension is activated the very first time the command is executed
|
||||
|
||||
@ -37,8 +38,15 @@ function activate(context) {
|
||||
let result = CheckFileError(uri.fsPath);
|
||||
if (result[0]) {
|
||||
vscode.window.showInformationMessage("正在生成" + uri.fsPath);
|
||||
NLog.Init(1, path.join("" + path.dirname(uri.fsPath), "napi_gen.log"))
|
||||
xgen.doGenerate(uri.fsPath, path.dirname(uri.fsPath));
|
||||
vscode.window.showInformationMessage("生成成功");
|
||||
let ret=NLog.GetResult();
|
||||
if(ret[0]) {
|
||||
vscode.window.showInformationMessage("生成成功");
|
||||
}
|
||||
else {
|
||||
vscode.window.showInformationMessage(""+ret[1]);
|
||||
}
|
||||
}
|
||||
else {
|
||||
vscode.window.showErrorMessage("" + result[1]);
|
||||
|
@ -15,18 +15,22 @@
|
||||
const main = require("./main");
|
||||
const re = require("./tools/re");
|
||||
const { checkFileError } = require("./tools/common");
|
||||
const { NLog } = require("./tools/NLog");
|
||||
const path = require("path");
|
||||
|
||||
function print(...args) {
|
||||
console.log(...args)
|
||||
}
|
||||
//function print(...args) {
|
||||
// console.log(...args)
|
||||
//}
|
||||
|
||||
const stdio = require("stdio");
|
||||
|
||||
let ops = stdio.getopt({
|
||||
'filename': { key: 'f', args: 1, description: ".d.ts file" },
|
||||
'out': { key: 'o', args: 1, description: "output directory", default: "." }
|
||||
'out': { key: 'o', args: 1, description: "output directory", default: "." },
|
||||
'loglevel': { key: 'l', args: 1, description: "Log Level : 0~3", default: "1" }
|
||||
});
|
||||
|
||||
NLog.Init(ops.loglevel, path.join("" + ops.out, "napi_gen.log"))
|
||||
|
||||
let fn = re.getFileInPath(ops.filename)
|
||||
|
||||
@ -37,9 +41,11 @@ if (tt) {
|
||||
main.doGenerate(ops.filename, ops.out)
|
||||
}
|
||||
else {
|
||||
console.log(result[1])
|
||||
//console.log(result[1])
|
||||
NLog.LOGE(result[1]);
|
||||
}
|
||||
}
|
||||
else {
|
||||
print("\n文件名 " + fn + " 校验失败,需要符合 @ohos.xxx.d.ts")
|
||||
//print("\n文件名 " + fn + " 校验失败,需要符合 @ohos.xxx.d.ts")
|
||||
NLog.LOGE("file name " + fn + " format invalid, @ohos.xxx.d.ts");
|
||||
}
|
75
src/gen/tools/NLog.js
Executable file
75
src/gen/tools/NLog.js
Executable file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
const fs = require('fs');
|
||||
|
||||
class NLog {
|
||||
constructor() {
|
||||
}
|
||||
}
|
||||
NLog.LEV_NONE = 0;
|
||||
NLog.LEV_ERROR = 1;
|
||||
NLog.LEV_DEBUG = 2;
|
||||
NLog.LEV_INFO = 3;
|
||||
|
||||
const LEV_STR = ["[NON]", "[ERR]", "[DBG]", "[INF]"]
|
||||
var logLevel = NLog.LEV_ERROR;
|
||||
var logFileName = null;
|
||||
var logResultMessage = [true, ""]
|
||||
|
||||
function GetDateString() {
|
||||
let nowDate = new Date();
|
||||
return nowDate.toLocaleString();
|
||||
}
|
||||
|
||||
function SaveLog(dateStr, levStr, detail) {
|
||||
if (logFileName) {
|
||||
let logStr = dateStr + " " + levStr + " " + detail + "\n";
|
||||
fs.appendFileSync(logFileName, logStr);
|
||||
}
|
||||
}
|
||||
|
||||
NLog.Init = function (level, fileName) {
|
||||
logLevel = level in [NLog.LEV_NONE, NLog.LEV_ERROR, NLog.LEV_DEBUG, NLog.LEV_INFO] ? level : NLog.LEV_ERROR;
|
||||
logFileName = fileName ? fileName : "napi_generator.log";
|
||||
}
|
||||
|
||||
function RecordLog(lev, ...args) {
|
||||
let dataStr = GetDateString();
|
||||
let detail = args.join(" ");
|
||||
SaveLog(dataStr, LEV_STR[lev], detail);
|
||||
logResultMessage = [false, detail];
|
||||
if (logLevel < lev) return;
|
||||
console.log(dataStr, LEV_STR[lev], detail)
|
||||
}
|
||||
|
||||
NLog.LOGE = function (...args) {
|
||||
RecordLog(NLog.LEV_ERROR, args);
|
||||
}
|
||||
|
||||
NLog.LOGD = function (...args) {
|
||||
RecordLog(NLog.LEV_DEBUG, args);
|
||||
}
|
||||
|
||||
NLog.LOGI = function (...args) {
|
||||
RecordLog(NLog.LEV_INFO, args);
|
||||
}
|
||||
|
||||
NLog.GetResult = function () {
|
||||
return logResultMessage;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
NLog
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user