@plasosdk/plaso-electron-sdk 1.3.14 → 1.3.15

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.
package/scripts/logger.js CHANGED
@@ -1,138 +1,138 @@
1
- const { LEVEL } = require('../js/macro');
2
- const LEVEL_FORMAT_LENGTH = Math.max(...Object.keys(LEVEL).map((key) => key.length)) + 1;
3
-
4
- class LogFormatter {
5
- constructor(loggerPath) {
6
- this.loggerPath = loggerPath ? loggerPath.replace(/\\/g, '/') : '';
7
- this.fs;
8
- this.util;
9
- this.newLogTime;
10
- this.ws = null;
11
- }
12
-
13
- init() {
14
- try {
15
- this.fs = window.require('fs');
16
- this.util = window.require('util');
17
-
18
- const process = window.require('process');
19
- process.on('uncaughtException', (error) => this.write(LEVEL.error, error ? error.stack : {}));
20
- } catch (error) {
21
- console.error('log error', error);
22
- }
23
- }
24
-
25
- initLoggerFilePath(_loggerPath) {
26
- this.loggerPath = _loggerPath ? _loggerPath.replace(/\\/g, '/') : '';
27
- }
28
-
29
- createWS() {
30
- if (this.fs && this.loggerPath && this.newLogTime) {
31
- this.logFileName = 'plasoElectronSdk_';
32
- this.ws = this.fs.createWriteStream(`${this.loggerPath}/${this.logFileName}${this.newLogTime}.log`, { flags: 'a' });
33
- this.ws.write(`Start log: ${new Date()}\n\n`);
34
- }
35
- }
36
-
37
- _writeLog(prefix, logContent) {
38
- if (this.ws) {
39
- this.ws.write(prefix);
40
- this.ws.write(logContent);
41
- this.ws.write('\n');
42
- }
43
- }
44
-
45
- write(level, ...format) {
46
- try {
47
- if (!this.fs || !this.util) {
48
- return;
49
- }
50
- const today = new Date();
51
- const newLogTime = `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`;
52
- if (newLogTime !== this.newLogTime) {
53
- this.newLogTime = newLogTime;
54
- if (this.ws !== null) {
55
- this.ws.end();
56
- this.ws = null;
57
- }
58
- }
59
- if (this.ws == null) {
60
- this.createWS();
61
- }
62
- const maxContentSize = 10 * 1024;
63
- let logContent = this.util.format(...format);
64
- const logContentSize = logContent.length;
65
- // 单条日志长度超过10K则进行截取
66
- if (logContentSize > maxContentSize) {
67
- logContent =
68
- logContent.slice(0, maxContentSize) +
69
- ` <<< more (${((logContentSize - maxContentSize) / 1024).toFixed(1)}KB)`;
70
- }
71
- const logTime = `[${today.toLocaleTimeString('en-US', { hour12: false })}.${today
72
- .getMilliseconds()
73
- .toString()
74
- .padStart(3, '0')}]`;
75
- let logLevel = '';
76
- switch (level) {
77
- case LEVEL.ERROR:
78
- logLevel = 'ERROR';
79
- break;
80
- case LEVEL.WARN:
81
- logLevel = 'WARN';
82
- break;
83
- case LEVEL.INFO:
84
- logLevel = 'INFO';
85
- break;
86
- case LEVEL.DEBUG:
87
- logLevel = 'DEBUG';
88
- break;
89
- }
90
- const prefix = `${logLevel.padEnd(LEVEL_FORMAT_LENGTH, ' ')} ${logTime} `;
91
-
92
- this._writeLog(prefix, logContent);
93
- } catch (e) {
94
- console.error('log write error', e);
95
- }
96
- }
97
-
98
- /**
99
- * @param {any[]} content
100
- */
101
- info(...content) {
102
- this.log('info', ...content);
103
- }
104
-
105
- /**
106
- * @param {any[]} content
107
- */
108
- debug(...content) {
109
- this.log('debug', ...content);
110
- }
111
- /**
112
- * @param {any[]} content
113
- */
114
- warn(...content) {
115
- this.log('warn', ...content);
116
- }
117
- /**
118
- * @param {any[]} content
119
- */
120
- error(...content) {
121
- this.log('error', ...content);
122
- }
123
-
124
- /**
125
- * @private
126
- * @param {'info' | 'debug' | 'error' | 'warn'} level
127
- * @param {any[]} content
128
- */
129
- log(level, ...content) {
130
- if (this.loggerPath) {
131
- this.write(LEVEL[level], ...content);
132
- } else {
133
- console[level](...content);
134
- }
135
- }
136
- }
137
-
138
- module.exports = LogFormatter;
1
+ const { LEVEL } = require('../js/macro');
2
+ const LEVEL_FORMAT_LENGTH = Math.max(...Object.keys(LEVEL).map((key) => key.length)) + 1;
3
+
4
+ class LogFormatter {
5
+ constructor(loggerPath) {
6
+ this.loggerPath = loggerPath ? loggerPath.replace(/\\/g, '/') : '';
7
+ this.fs;
8
+ this.util;
9
+ this.newLogTime;
10
+ this.ws = null;
11
+ }
12
+
13
+ init() {
14
+ try {
15
+ this.fs = window.require('fs');
16
+ this.util = window.require('util');
17
+
18
+ const process = window.require('process');
19
+ process.on('uncaughtException', (error) => this.write(LEVEL.error, error ? error.stack : {}));
20
+ } catch (error) {
21
+ console.error('log error', error);
22
+ }
23
+ }
24
+
25
+ initLoggerFilePath(_loggerPath) {
26
+ this.loggerPath = _loggerPath ? _loggerPath.replace(/\\/g, '/') : '';
27
+ }
28
+
29
+ createWS() {
30
+ if (this.fs && this.loggerPath && this.newLogTime) {
31
+ this.logFileName = 'plasoElectronSdk_';
32
+ this.ws = this.fs.createWriteStream(`${this.loggerPath}/${this.logFileName}${this.newLogTime}.log`, { flags: 'a' });
33
+ this.ws.write(`Start log: ${new Date()}\n\n`);
34
+ }
35
+ }
36
+
37
+ _writeLog(prefix, logContent) {
38
+ if (this.ws) {
39
+ this.ws.write(prefix);
40
+ this.ws.write(logContent);
41
+ this.ws.write('\n');
42
+ }
43
+ }
44
+
45
+ write(level, ...format) {
46
+ try {
47
+ if (!this.fs || !this.util) {
48
+ return;
49
+ }
50
+ const today = new Date();
51
+ const newLogTime = `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`;
52
+ if (newLogTime !== this.newLogTime) {
53
+ this.newLogTime = newLogTime;
54
+ if (this.ws !== null) {
55
+ this.ws.end();
56
+ this.ws = null;
57
+ }
58
+ }
59
+ if (this.ws == null) {
60
+ this.createWS();
61
+ }
62
+ const maxContentSize = 10 * 1024;
63
+ let logContent = this.util.format(...format);
64
+ const logContentSize = logContent.length;
65
+ // 单条日志长度超过10K则进行截取
66
+ if (logContentSize > maxContentSize) {
67
+ logContent =
68
+ logContent.slice(0, maxContentSize) +
69
+ ` <<< more (${((logContentSize - maxContentSize) / 1024).toFixed(1)}KB)`;
70
+ }
71
+ const logTime = `[${today.toLocaleTimeString('en-US', { hour12: false })}.${today
72
+ .getMilliseconds()
73
+ .toString()
74
+ .padStart(3, '0')}]`;
75
+ let logLevel = '';
76
+ switch (level) {
77
+ case LEVEL.ERROR:
78
+ logLevel = 'ERROR';
79
+ break;
80
+ case LEVEL.WARN:
81
+ logLevel = 'WARN';
82
+ break;
83
+ case LEVEL.INFO:
84
+ logLevel = 'INFO';
85
+ break;
86
+ case LEVEL.DEBUG:
87
+ logLevel = 'DEBUG';
88
+ break;
89
+ }
90
+ const prefix = `${logLevel.padEnd(LEVEL_FORMAT_LENGTH, ' ')} ${logTime} `;
91
+
92
+ this._writeLog(prefix, logContent);
93
+ } catch (e) {
94
+ console.error('log write error', e);
95
+ }
96
+ }
97
+
98
+ /**
99
+ * @param {any[]} content
100
+ */
101
+ info(...content) {
102
+ this.log('info', ...content);
103
+ }
104
+
105
+ /**
106
+ * @param {any[]} content
107
+ */
108
+ debug(...content) {
109
+ this.log('debug', ...content);
110
+ }
111
+ /**
112
+ * @param {any[]} content
113
+ */
114
+ warn(...content) {
115
+ this.log('warn', ...content);
116
+ }
117
+ /**
118
+ * @param {any[]} content
119
+ */
120
+ error(...content) {
121
+ this.log('error', ...content);
122
+ }
123
+
124
+ /**
125
+ * @private
126
+ * @param {'info' | 'debug' | 'error' | 'warn'} level
127
+ * @param {any[]} content
128
+ */
129
+ log(level, ...content) {
130
+ if (this.loggerPath) {
131
+ this.write(LEVEL[level], ...content);
132
+ } else {
133
+ console[level](...content);
134
+ }
135
+ }
136
+ }
137
+
138
+ module.exports = LogFormatter;