@fordi-org/sdn 0.0.2 → 0.0.4
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/lib/shellQuote.js +1 -1
- package/package.json +1 -1
- package/service/logs.js +29 -6
package/lib/shellQuote.js
CHANGED
package/package.json
CHANGED
package/service/logs.js
CHANGED
|
@@ -19,11 +19,8 @@ const formatDateTime = (date) => {
|
|
|
19
19
|
return `${d} ${t}`;
|
|
20
20
|
}
|
|
21
21
|
const decoder = new TextDecoder();
|
|
22
|
+
const ansiEscRx = /\x1b\[([\d;]+)m/g;
|
|
22
23
|
export const formatJournal = (line) => {
|
|
23
|
-
const time = parseInt(line.__REALTIME_TIMESTAMP.slice(0, -3));
|
|
24
|
-
const sym = line._TRANSPORT === 'stdout' ? ' > ' : '‼> ';
|
|
25
|
-
const message = Array.isArray(line.MESSAGE) ? decoder.decode(new Uint8Array(line.MESSAGE)) : line.MESSAGE;
|
|
26
|
-
return `${formatDateTime(new Date(time))}${sym}${message}`;
|
|
27
24
|
}
|
|
28
25
|
|
|
29
26
|
export function journalctl() {
|
|
@@ -31,9 +28,35 @@ export function journalctl() {
|
|
|
31
28
|
console.info(`> ${shellQuote(argv)}`);
|
|
32
29
|
return jsonCmd(argv[0], argv.slice(1));
|
|
33
30
|
}
|
|
34
|
-
|
|
31
|
+
|
|
32
|
+
const useColor = process.argv.slice(2).includes('--force-color') || process.stdout.isTTY;
|
|
33
|
+
|
|
34
|
+
// Only generate colors if on a terminal
|
|
35
|
+
const SET = (...states) => useColor ? `\x1b[${states.join(';')}m` : '';
|
|
36
|
+
const RESET = SET(0);
|
|
35
37
|
if (fileURLToPath(import.meta.url) === resolve(process.argv[1])) {
|
|
38
|
+
let state = '0';
|
|
36
39
|
for await (const line of journalctl(process.argv.slice(2))) {
|
|
37
|
-
|
|
40
|
+
const time = parseInt(line.__REALTIME_TIMESTAMP.slice(0, -3));
|
|
41
|
+
const sym = line._TRANSPORT === 'stdout' ? ' > ' : '‼> ';
|
|
42
|
+
// Unwrap the line if it's an array of numbers
|
|
43
|
+
let message = Array.isArray(line.MESSAGE) && (typeof line.MESSAGE[0] === 'number')
|
|
44
|
+
? decoder.decode(new Uint8Array(line.MESSAGE))
|
|
45
|
+
: line.MESSAGE;
|
|
46
|
+
|
|
47
|
+
if (!useColor) {
|
|
48
|
+
// Strip colors if not on a terminal
|
|
49
|
+
message = message.replace(ansiEscRx, '');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Reset and restore the current ANSI color state
|
|
53
|
+
const output = `${RESET}${formatDateTime(new Date(time))}${sym}${SET(state)}${message}${RESET}`;
|
|
54
|
+
|
|
55
|
+
// Capture the last esc[*m instance in the message, and store it to be restored before the next message.
|
|
56
|
+
// Unnessesary if not on a terminal.
|
|
57
|
+
if (useColor) {
|
|
58
|
+
state = [...(message.matchAll(ansiEscRx) ?? [])].at(-1)?.[1] ?? '0';
|
|
59
|
+
}
|
|
60
|
+
console.log(output);
|
|
38
61
|
}
|
|
39
62
|
}
|