ag-common 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,8 @@
1
+ export declare type TLogType = 'TRACE' | 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'FATAL';
2
+ export declare const GetLogLevel: (l: TLogType) => number;
3
+ export declare const trace: (...args: any[]) => void;
4
+ export declare const debug: (...args: any[]) => void;
1
5
  export declare const info: (...args: any[]) => void;
2
6
  export declare const warn: (...args: any[]) => void;
3
7
  export declare const error: (...args: any[]) => void;
4
- export declare const debug: (...args: any[]) => void;
5
- export declare const trace: (...args: any[]) => void;
8
+ export declare const fatal: (...args: any[]) => void;
@@ -1,25 +1,153 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.trace = exports.debug = exports.error = exports.warn = exports.info = void 0;
3
+ exports.fatal = exports.error = exports.warn = exports.info = exports.debug = exports.trace = exports.GetLogLevel = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-explicit-any */
5
+ const _1 = require(".");
6
+ const GetLogLevel = (l) => ['TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'].findIndex((s) => s === l);
7
+ exports.GetLogLevel = GetLogLevel;
5
8
  /* eslint-disable no-console */
6
- const info = (...args) => {
7
- console.log(...args);
9
+ function dateF() {
10
+ const d = new Date();
11
+ const str = `${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}`;
12
+ return str;
13
+ }
14
+ function nicify(...args) {
15
+ const ret = [];
16
+ args.forEach((a) => {
17
+ a.forEach((v) => {
18
+ if (v !== null &&
19
+ typeof v !== 'undefined' &&
20
+ v.toString().indexOf('Error:') !== -1 &&
21
+ v.stack) {
22
+ ret.push(`${v.stack}`);
23
+ }
24
+ else if (typeof v === 'string') {
25
+ if (v.trim() !== 'undefined') {
26
+ ret.push(`${v.trim()}`);
27
+ }
28
+ }
29
+ else if (typeof v === 'object') {
30
+ ret.push(JSON.parse(JSON.stringify(v)));
31
+ }
32
+ else {
33
+ ret.push(v);
34
+ }
35
+ });
36
+ });
37
+ return ret;
38
+ }
39
+ function logprocess(type, date, args) {
40
+ var _a;
41
+ const ds = date ? `[${date}]` : '';
42
+ const retm = [ds, type, ...args].filter(_1.notEmpty).join('\t');
43
+ const min = (0, exports.GetLogLevel)((_a = process.env.LOG_LEVEL) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'WARN';
44
+ const typesLogLevel = (0, exports.GetLogLevel)(type);
45
+ // env ignores it
46
+ if (typesLogLevel < min) {
47
+ return;
48
+ }
49
+ switch (type) {
50
+ case 'TRACE': {
51
+ console.trace(retm);
52
+ break;
53
+ }
54
+ case 'DEBUG': {
55
+ console.debug(retm);
56
+ break;
57
+ }
58
+ case 'INFO': {
59
+ console.log(retm);
60
+ break;
61
+ }
62
+ case 'WARN': {
63
+ console.warn(retm);
64
+ break;
65
+ }
66
+ case 'ERROR': {
67
+ console.error(retm);
68
+ break;
69
+ }
70
+ case 'FATAL': {
71
+ console.error(retm);
72
+ break;
73
+ }
74
+ default: {
75
+ console.log(retm);
76
+ break;
77
+ }
78
+ }
79
+ }
80
+ function printStackTrace(...args) {
81
+ const callstack = [];
82
+ let isCallstackPopulated = false;
83
+ try {
84
+ throw new Error('Test');
85
+ }
86
+ catch (e) {
87
+ const er = e;
88
+ if (er.stack) {
89
+ // Firefox / chrome
90
+ const lines = er.stack.split('\n');
91
+ for (let i = 0, len = lines.length; i < len; i += 1) {
92
+ callstack.push(` ${lines[i]} `);
93
+ }
94
+ // Remove call to logStackTrace()
95
+ callstack.shift();
96
+ isCallstackPopulated = true;
97
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
98
+ // @ts-ignore
99
+ }
100
+ else if (window.opera && er.message) {
101
+ // Opera
102
+ const lines = er.message.split('\n');
103
+ for (let i = 0, len = lines.length; i < len; i += 1) {
104
+ if (lines[i].match(/^\s*[A-Za-z0-9\-_$]+\(/)) {
105
+ let entry = lines[i];
106
+ // Append next line also since it has the file info
107
+ if (lines[i + 1]) {
108
+ entry += ` at ${lines[i + 1]}`;
109
+ i += 1;
110
+ }
111
+ callstack.push(entry);
112
+ }
113
+ }
114
+ // Remove call to logStackTrace()
115
+ callstack.shift();
116
+ isCallstackPopulated = true;
117
+ }
118
+ }
119
+ if (!isCallstackPopulated) {
120
+ // IE and Safari
121
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
122
+ // @ts-ignore
123
+ let currentFunction = args.callee.caller;
124
+ while (currentFunction) {
125
+ const fn = currentFunction.toString();
126
+ const fname = fn.substring(fn.indexOf('function') + 8, fn.indexOf('(')) ||
127
+ 'anonymous';
128
+ callstack.push(fname);
129
+ currentFunction = currentFunction.caller;
130
+ }
131
+ }
132
+ return callstack.join('\n');
133
+ }
134
+ const trace = (...args) => {
135
+ const argsNice = nicify(args);
136
+ args.push(printStackTrace());
137
+ logprocess('TRACE', dateF(), argsNice);
8
138
  };
139
+ exports.trace = trace;
140
+ const debug = (...args) => logprocess('DEBUG', dateF(), nicify(args));
141
+ exports.debug = debug;
142
+ const info = (...args) => logprocess('INFO', dateF(), nicify(args));
9
143
  exports.info = info;
10
- const warn = (...args) => {
11
- console.warn(...args);
12
- };
144
+ const warn = (...args) => logprocess('WARN', dateF(), nicify(args));
13
145
  exports.warn = warn;
14
- const error = (...args) => {
15
- console.error(...args);
16
- };
146
+ const error = (...args) => logprocess('ERROR', dateF(), nicify(args));
17
147
  exports.error = error;
18
- const debug = (...args) => {
19
- console.debug(...args);
148
+ const fatal = (...args) => {
149
+ const argsNice = nicify(args);
150
+ args.push(printStackTrace());
151
+ logprocess('FATAL', dateF(), argsNice);
20
152
  };
21
- exports.debug = debug;
22
- const trace = (...args) => {
23
- console.trace(...args);
24
- };
25
- exports.trace = trace;
153
+ exports.fatal = fatal;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",