node-opcua-debug
Advanced tools
Comparing version 2.110.0 to 2.113.0
@@ -10,1 +10,2 @@ /** | ||
export { dump, dumpIf } from "./dump_if"; | ||
export { LogLevel, setLogLevel, setDebugLogger, setWarningLogger, setErrorLogger } from "./make_loggers"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.dumpIf = exports.dump = exports.hexDump = exports.inlineText = exports.makeBufferFromTrace = exports.messageLogger = exports.make_warningLog = exports.make_errorLog = exports.make_debugLog = exports.setDebugFlag = exports.checkDebugFlag = exports.displayTraceFromThisProjectOnly = exports.traceFromThisProjectOnly = exports.removeDecoration = void 0; | ||
exports.setErrorLogger = exports.setWarningLogger = exports.setDebugLogger = exports.setLogLevel = exports.LogLevel = exports.dumpIf = exports.dump = exports.hexDump = exports.inlineText = exports.makeBufferFromTrace = exports.messageLogger = exports.make_warningLog = exports.make_errorLog = exports.make_debugLog = exports.setDebugFlag = exports.checkDebugFlag = exports.displayTraceFromThisProjectOnly = exports.traceFromThisProjectOnly = exports.removeDecoration = void 0; | ||
/** | ||
@@ -27,2 +27,8 @@ * @module node-opcua-debug | ||
Object.defineProperty(exports, "dumpIf", { enumerable: true, get: function () { return dump_if_1.dumpIf; } }); | ||
var make_loggers_2 = require("./make_loggers"); | ||
Object.defineProperty(exports, "LogLevel", { enumerable: true, get: function () { return make_loggers_2.LogLevel; } }); | ||
Object.defineProperty(exports, "setLogLevel", { enumerable: true, get: function () { return make_loggers_2.setLogLevel; } }); | ||
Object.defineProperty(exports, "setDebugLogger", { enumerable: true, get: function () { return make_loggers_2.setDebugLogger; } }); | ||
Object.defineProperty(exports, "setWarningLogger", { enumerable: true, get: function () { return make_loggers_2.setWarningLogger; } }); | ||
Object.defineProperty(exports, "setErrorLogger", { enumerable: true, get: function () { return make_loggers_2.setErrorLogger; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -6,4 +6,24 @@ /// <reference types="node" /> | ||
import { EventEmitter } from "events"; | ||
export declare enum LogLevel { | ||
Emergency = 0, | ||
Alert = 1, | ||
Critic = 2, | ||
Error = 3, | ||
Warning = 4, | ||
Notice = 5, | ||
Info = 6, | ||
Debug = 7 | ||
} | ||
export declare function setLogLevel(level: LogLevel): void; | ||
type PrintFunc = (data?: any, ...argN: any[]) => void; | ||
export declare function setDebugLogger(log: PrintFunc): void; | ||
export declare function setWarningLogger(log: PrintFunc): void; | ||
export declare function setErrorLogger(log: PrintFunc): void; | ||
export declare function setDebugFlag(scriptFullPath: string, flag: boolean): void; | ||
export declare function checkDebugFlag(scriptFullPath: string): boolean; | ||
export declare class MessageLogger extends EventEmitter { | ||
constructor(); | ||
on(eventName: "warningMessage" | "errorMessage", eventHandler: () => void): this; | ||
} | ||
export declare const messageLogger: MessageLogger; | ||
/** | ||
@@ -17,8 +37,4 @@ * @method make_debugLog | ||
export declare function make_debugLog(scriptFullPath: string): (...arg: any[]) => void; | ||
export declare class MessageLogger extends EventEmitter { | ||
constructor(); | ||
on(eventName: "warningMessage" | "errorMessage", eventHandler: () => void): this; | ||
} | ||
export declare const messageLogger: MessageLogger; | ||
export declare function make_errorLog(context: string): (...arg: any[]) => void; | ||
export declare function make_warningLog(context: string): (...arg: any[]) => void; | ||
export declare function make_errorLog(context: string): PrintFunc; | ||
export declare function make_warningLog(context: string): PrintFunc; | ||
export {}; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.make_warningLog = exports.make_errorLog = exports.messageLogger = exports.MessageLogger = exports.make_debugLog = exports.checkDebugFlag = exports.setDebugFlag = void 0; | ||
exports.make_warningLog = exports.make_errorLog = exports.make_debugLog = exports.messageLogger = exports.MessageLogger = exports.checkDebugFlag = exports.setDebugFlag = exports.setErrorLogger = exports.setWarningLogger = exports.setDebugLogger = exports.setLogLevel = exports.LogLevel = void 0; | ||
/** | ||
@@ -18,2 +18,13 @@ * @module node-opcua-debug | ||
const sTraceFlag = _process.env && _process.env.DEBUG; | ||
var LogLevel; | ||
(function (LogLevel) { | ||
LogLevel[LogLevel["Emergency"] = 0] = "Emergency"; | ||
LogLevel[LogLevel["Alert"] = 1] = "Alert"; | ||
LogLevel[LogLevel["Critic"] = 2] = "Critic"; | ||
LogLevel[LogLevel["Error"] = 3] = "Error"; | ||
LogLevel[LogLevel["Warning"] = 4] = "Warning"; | ||
LogLevel[LogLevel["Notice"] = 5] = "Notice"; | ||
LogLevel[LogLevel["Info"] = 6] = "Info"; | ||
LogLevel[LogLevel["Debug"] = 7] = "Debug"; | ||
})(LogLevel || (exports.LogLevel = LogLevel = {})); | ||
// istanbul ignore next | ||
@@ -40,2 +51,9 @@ if (_process.env && false) { | ||
: 25; | ||
let g_logLevel = process.env.NODEOPCUA_LOG_LEVEL | ||
? parseInt(process.env.NODEOPCUA_LOG_LEVEL) | ||
: LogLevel.Warning; | ||
function setLogLevel(level) { | ||
g_logLevel = level; | ||
} | ||
exports.setLogLevel = setLogLevel; | ||
function extractBasename(name) { | ||
@@ -51,2 +69,56 @@ if (!name) { | ||
} | ||
const contextCounter = {}; | ||
const increaseCounter = (ctxt) => { | ||
const { filename, callerline } = ctxt; | ||
const key = `${filename}:${callerline}};`; | ||
const bucket = contextCounter[key]; | ||
if (!bucket) { | ||
contextCounter[key] = 1; | ||
return 1; | ||
} | ||
contextCounter[key] = contextCounter[key] + 1; | ||
return contextCounter[key]; | ||
}; | ||
const threshold = 100; | ||
const loggers = { | ||
errorLogger: (ctxt, ...args) => { | ||
const occurenceCount = increaseCounter(ctxt); | ||
if (occurenceCount > threshold) { | ||
return; | ||
} | ||
const output = dump(ctxt, "E", args); | ||
exports.messageLogger.emit("errorMessage", output); | ||
if (occurenceCount === threshold) { | ||
dump(ctxt, "E", [`This error occured more than ${threshold} times, no more error will be logged for this context`]); | ||
return; | ||
} | ||
}, | ||
warningLogger: (ctxt, ...args) => { | ||
const occurenceCount = increaseCounter(ctxt); | ||
if (occurenceCount > threshold) { | ||
return; | ||
} | ||
const output = dump(ctxt, "W", args); | ||
exports.messageLogger.emit("warningMessage", output); | ||
if (occurenceCount === threshold) { | ||
dump(ctxt, "W", [`This warning occured more than ${threshold} times, no more warning will be logged for this context`]); | ||
return; | ||
} | ||
}, | ||
debugLogger: (ctxt, ...args) => { | ||
const output = dump(ctxt, "D", args); | ||
} | ||
}; | ||
function setDebugLogger(log) { | ||
loggers.debugLogger = log; | ||
} | ||
exports.setDebugLogger = setDebugLogger; | ||
function setWarningLogger(log) { | ||
loggers.warningLogger = log; | ||
} | ||
exports.setWarningLogger = setWarningLogger; | ||
function setErrorLogger(log) { | ||
loggers.errorLogger = log; | ||
} | ||
exports.setErrorLogger = setErrorLogger; | ||
function setDebugFlag(scriptFullPath, flag) { | ||
@@ -56,3 +128,7 @@ const filename = extractBasename(scriptFullPath); | ||
const decoratedFilename = chalk_1.default.yellow(w(filename, 60)); | ||
console.log(" Setting debug for ", decoratedFilename, " to ", (flag ? chalk_1.default.cyan : chalk_1.default.red)(flag.toString(), sTraceFlag)); | ||
loggers.debugLogger({ | ||
filename: __filename, | ||
callerline: -1 | ||
}, " Setting debug for ", decoratedFilename, " to ", (flag ? chalk_1.default.cyan : chalk_1.default.red)(flag.toString(), sTraceFlag)); | ||
g_logLevel = LogLevel.Debug; | ||
} | ||
@@ -90,17 +166,19 @@ debugFlags[filename] = flag; | ||
const continuation = w(" ... ", 51); | ||
function buildPrefix(mode) { | ||
function getCallerContext(level) { | ||
const stack = new Error("").stack || ""; | ||
// caller line number | ||
const l = stack.split("\n")[4].split(":"); | ||
const callerLine = parseInt(l[l.length - 2], 10); | ||
const l = stack.split("\n")[level].split(":"); | ||
const callerline = parseInt(l[l.length - 2], 10); | ||
const filename = extractBasename(l[l.length - 3]); | ||
return file_line(mode, filename, callerLine); | ||
return { filename, callerline }; | ||
} | ||
function dump(mode, args1) { | ||
function dump(ctx, mode, args1) { | ||
const a2 = Object.values(args1); | ||
const output = (0, util_1.format)(...a2); | ||
let a1 = [buildPrefix(mode)]; | ||
const { filename, callerline } = ctx; | ||
let a1 = [file_line(mode, filename, callerline)]; | ||
let i = 0; | ||
for (const line of output.split("\n")) { | ||
const lineArguments = [].concat(a1, [line]); | ||
// eslint-disable-next-line prefer-spread | ||
console.log(...lineArguments); | ||
@@ -111,2 +189,3 @@ a1 = [continuation]; | ||
const a3 = a1.concat([` .... TRUNCATED ..... (NODEOPCUA_DEBUG_MAXLINE_PER_MESSAGE=${maxLines}`]); | ||
// eslint-disable-next-line prefer-spread | ||
console.log(...a3); | ||
@@ -118,2 +197,12 @@ break; | ||
} | ||
class MessageLogger extends events_1.EventEmitter { | ||
constructor() { | ||
super(); | ||
} | ||
on(eventName, eventHandler) { | ||
return super.on(eventName, eventHandler); | ||
} | ||
} | ||
exports.MessageLogger = MessageLogger; | ||
exports.messageLogger = new MessageLogger(); | ||
/** | ||
@@ -129,4 +218,5 @@ * @method make_debugLog | ||
function debugLogFunc(...args) { | ||
if (debugFlags[filename]) { | ||
dump("D", args); | ||
if (debugFlags[filename] && g_logLevel >= LogLevel.Debug) { | ||
const ctxt = getCallerContext(3); | ||
loggers.debugLogger(ctxt, ...args); | ||
} | ||
@@ -137,28 +227,22 @@ } | ||
exports.make_debugLog = make_debugLog; | ||
class MessageLogger extends events_1.EventEmitter { | ||
constructor() { | ||
super(); | ||
function errorLogFunc(...args) { | ||
if (g_logLevel >= LogLevel.Error) { | ||
const ctxt = getCallerContext(3); | ||
loggers.errorLogger(ctxt, ...args); | ||
} | ||
on(eventName, eventHandler) { | ||
return super.on(eventName, eventHandler); | ||
} | ||
} | ||
exports.MessageLogger = MessageLogger; | ||
exports.messageLogger = new MessageLogger(); | ||
function make_errorLog(context) { | ||
function errorLogFunc(...args) { | ||
const output = dump("E", args); | ||
exports.messageLogger.emit("errorMessage", output); | ||
} | ||
return errorLogFunc; | ||
} | ||
exports.make_errorLog = make_errorLog; | ||
function make_warningLog(context) { | ||
function errorLogFunc(...args) { | ||
const output = dump("W", args); | ||
exports.messageLogger.emit("warningMessage", output); | ||
function warningLogFunc(...args) { | ||
if (g_logLevel >= LogLevel.Warning) { | ||
const ctxt = getCallerContext(3); | ||
loggers.warningLogger(ctxt, ...args); | ||
} | ||
return errorLogFunc; | ||
} | ||
function make_warningLog(context) { | ||
return warningLogFunc; | ||
} | ||
exports.make_warningLog = make_warningLog; | ||
//# sourceMappingURL=make_loggers.js.map |
{ | ||
"name": "node-opcua-debug", | ||
"version": "2.110.0", | ||
"version": "2.113.0", | ||
"description": "pure nodejs OPCUA SDK - module debug", | ||
@@ -36,3 +36,3 @@ "scripts": { | ||
"homepage": "http://node-opcua.github.io/", | ||
"gitHead": "f419b91486436bfd804d5d66d2b77db834a4deaf", | ||
"gitHead": "36db335391fedd39726990a1b37f7768da16466a", | ||
"files": [ | ||
@@ -39,0 +39,0 @@ "dist", |
@@ -10,1 +10,2 @@ /** | ||
export { dump, dumpIf } from "./dump_if"; | ||
export { LogLevel, setLogLevel, setDebugLogger, setWarningLogger, setErrorLogger } from "./make_loggers"; |
@@ -14,2 +14,13 @@ /** | ||
export enum LogLevel { | ||
Emergency = 0, | ||
Alert = 1, | ||
Critic = 2, | ||
Error = 3, | ||
Warning = 4, | ||
Notice = 5, | ||
Info = 6, | ||
Debug = 7 | ||
} | ||
// istanbul ignore next | ||
@@ -37,3 +48,10 @@ if (_process.env && false) { | ||
: 25; | ||
let g_logLevel: LogLevel = process.env.NODEOPCUA_LOG_LEVEL | ||
? (parseInt(process.env.NODEOPCUA_LOG_LEVEL) as LogLevel) | ||
: LogLevel.Warning; | ||
export function setLogLevel(level: LogLevel): void { | ||
g_logLevel = level; | ||
} | ||
function extractBasename(name: string): string { | ||
@@ -51,2 +69,66 @@ if (!name) { | ||
interface Context { | ||
filename: string; | ||
callerline: number; | ||
} | ||
const contextCounter: Record<string,number> = { | ||
} | ||
const increaseCounter = (ctxt: Context) => { | ||
const { filename, callerline } = ctxt; | ||
const key = `${filename}:${callerline}};` | ||
const bucket = contextCounter[key]; | ||
if (!bucket) { | ||
contextCounter[key] = 1; | ||
return 1; | ||
} | ||
contextCounter[key] = contextCounter[key] +1; | ||
return contextCounter[key]; | ||
} | ||
const threshold = 100; | ||
type PrintFunc = (data?: any, ...argN: any[]) => void; | ||
const loggers = { | ||
errorLogger: (ctxt: Context, ...args: [any, ...any[]]) => { | ||
const occurenceCount = increaseCounter(ctxt); | ||
if (occurenceCount > threshold) { | ||
return; | ||
} | ||
const output = dump(ctxt, "E", args); | ||
messageLogger.emit("errorMessage", output); | ||
if (occurenceCount === threshold) { | ||
dump(ctxt, "E", [`This error occured more than ${threshold} times, no more error will be logged for this context`]); | ||
return; | ||
} | ||
}, | ||
warningLogger: (ctxt: Context, ...args: [any, ...any[]]) => { | ||
const occurenceCount = increaseCounter(ctxt); | ||
if (occurenceCount > threshold) { | ||
return; | ||
} | ||
const output = dump(ctxt, "W", args); | ||
messageLogger.emit("warningMessage", output); | ||
if (occurenceCount === threshold) { | ||
dump(ctxt, "W", [`This warning occured more than ${threshold} times, no more warning will be logged for this context`]); | ||
return; | ||
} | ||
}, | ||
debugLogger: (ctxt: Context, ...args: [any, ...any[]]) => { | ||
const output = dump(ctxt, "D", args); | ||
} | ||
}; | ||
export function setDebugLogger(log: PrintFunc): void { | ||
loggers.debugLogger = log; | ||
} | ||
export function setWarningLogger(log: PrintFunc): void { | ||
loggers.warningLogger = log; | ||
} | ||
export function setErrorLogger(log: PrintFunc): void { | ||
loggers.errorLogger = log; | ||
} | ||
export function setDebugFlag(scriptFullPath: string, flag: boolean): void { | ||
@@ -56,3 +138,13 @@ const filename = extractBasename(scriptFullPath); | ||
const decoratedFilename = chalk.yellow(w(filename, 60)); | ||
console.log(" Setting debug for ", decoratedFilename, " to ", (flag ? chalk.cyan : chalk.red)(flag.toString(), sTraceFlag)); | ||
loggers.debugLogger( | ||
{ | ||
filename: __filename, | ||
callerline: -1 | ||
}, | ||
" Setting debug for ", | ||
decoratedFilename, | ||
" to ", | ||
(flag ? chalk.cyan : chalk.red)(flag.toString(), sTraceFlag) | ||
); | ||
g_logLevel = LogLevel.Debug; | ||
} | ||
@@ -90,19 +182,20 @@ debugFlags[filename] = flag; | ||
function buildPrefix(mode: "E" | "D" | "W"): string { | ||
function getCallerContext(level: number) { | ||
const stack: string = new Error("").stack || ""; | ||
// caller line number | ||
const l: string[] = stack.split("\n")[4].split(":"); | ||
const callerLine: number = parseInt(l[l.length - 2], 10); | ||
const l: string[] = stack.split("\n")[level].split(":"); | ||
const callerline: number = parseInt(l[l.length - 2], 10); | ||
const filename: string = extractBasename(l[l.length - 3]); | ||
return file_line(mode, filename, callerLine); | ||
return { filename, callerline }; | ||
} | ||
function dump(mode: "E" | "D" | "W", args1: [any?, ...any[]]) { | ||
function dump(ctx: Context, mode: "E" | "D" | "W", args1: [any?, ...any[]]) { | ||
const a2 = Object.values(args1) as [string, ...string[]]; | ||
const output = format(...a2); | ||
let a1 = [buildPrefix(mode)]; | ||
const { filename, callerline } = ctx; | ||
let a1 = [file_line(mode, filename, callerline)]; | ||
let i = 0; | ||
for (const line of output.split("\n")) { | ||
const lineArguments = ([] as string[]).concat(a1, [line]) as [string, ...string[]]; | ||
const lineArguments = ([] as string[]).concat(a1, [line]); | ||
// eslint-disable-next-line prefer-spread | ||
console.log(...lineArguments); | ||
@@ -113,3 +206,4 @@ a1 = [continuation]; | ||
const a3 = a1.concat([` .... TRUNCATED ..... (NODEOPCUA_DEBUG_MAXLINE_PER_MESSAGE=${maxLines}`]); | ||
console.log(...(a3 as [string, ...string[]])); | ||
// eslint-disable-next-line prefer-spread | ||
console.log(...a3); | ||
break; | ||
@@ -121,2 +215,12 @@ } | ||
export class MessageLogger extends EventEmitter { | ||
constructor() { | ||
super(); | ||
} | ||
public on(eventName: "warningMessage" | "errorMessage", eventHandler: () => void): this { | ||
return super.on(eventName, eventHandler); | ||
} | ||
} | ||
export const messageLogger = new MessageLogger(); | ||
/** | ||
@@ -131,37 +235,30 @@ * @method make_debugLog | ||
const filename = extractBasename(scriptFullPath); | ||
function debugLogFunc(...args: [any?, ...any[]]) { | ||
if (debugFlags[filename]) { | ||
dump("D", args); | ||
if (debugFlags[filename] && g_logLevel >= LogLevel.Debug) { | ||
const ctxt = getCallerContext(3); | ||
loggers.debugLogger(ctxt, ...args); | ||
} | ||
} | ||
return debugLogFunc; | ||
} | ||
export class MessageLogger extends EventEmitter { | ||
constructor() { | ||
super(); | ||
function errorLogFunc(...args: [any?, ...any[]]) { | ||
if (g_logLevel >= LogLevel.Error) { | ||
const ctxt = getCallerContext(3); | ||
loggers.errorLogger(ctxt, ...args); | ||
} | ||
public on(eventName: "warningMessage" | "errorMessage", eventHandler: () => void): this { | ||
return super.on(eventName, eventHandler); | ||
} | ||
} | ||
export const messageLogger = new MessageLogger(); | ||
export function make_errorLog(context: string): (...arg: any[]) => void { | ||
function errorLogFunc(...args: [any?, ...any[]]) { | ||
const output = dump("E", args); | ||
messageLogger.emit("errorMessage", output); | ||
} | ||
export function make_errorLog(context: string): PrintFunc { | ||
return errorLogFunc; | ||
} | ||
export function make_warningLog(context: string): (...arg: any[]) => void { | ||
function errorLogFunc(...args: [any?, ...any[]]) { | ||
const output = dump("W", args); | ||
messageLogger.emit("warningMessage", output); | ||
function warningLogFunc(...args: [any?, ...any[]]) { | ||
if (g_logLevel >= LogLevel.Warning) { | ||
const ctxt = getCallerContext(3); | ||
loggers.warningLogger(ctxt, ...args); | ||
} | ||
return errorLogFunc; | ||
} | ||
export function make_warningLog(context: string): PrintFunc { | ||
return warningLogFunc; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62110
45
1089
5