mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-20 06:07:55 +00:00
push, logs: Fix parsing of local mode device logs (NDJSON stream)
Resolves: #2185 Change-type: patch
This commit is contained in:
parent
0b0fb94834
commit
d50d18d492
@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* @license
|
* @license
|
||||||
* Copyright 2018-2020 Balena Ltd.
|
* Copyright 2018-2021 Balena Ltd.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -63,6 +63,7 @@ async function displayDeviceLogs(
|
|||||||
filterServices?: string[],
|
filterServices?: string[],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { addSIGINTHandler } = await import('../helpers');
|
const { addSIGINTHandler } = await import('../helpers');
|
||||||
|
const { parse: ndjsonParse } = await import('ndjson');
|
||||||
let gotSignal = false;
|
let gotSignal = false;
|
||||||
const handleSignal = () => {
|
const handleSignal = () => {
|
||||||
gotSignal = true;
|
gotSignal = true;
|
||||||
@ -72,8 +73,12 @@ async function displayDeviceLogs(
|
|||||||
process.once('SIGTERM', handleSignal);
|
process.once('SIGTERM', handleSignal);
|
||||||
try {
|
try {
|
||||||
await new Promise((_resolve, reject) => {
|
await new Promise((_resolve, reject) => {
|
||||||
logs.on('data', (log) => {
|
const jsonStream = ndjsonParse();
|
||||||
displayLogLine(log, logger, system, filterServices);
|
jsonStream.on('data', (log) => {
|
||||||
|
displayLogObject(log, logger, system, filterServices);
|
||||||
|
});
|
||||||
|
jsonStream.on('error', (e) => {
|
||||||
|
logger.logWarn(`Error parsing NDJSON log chunk: ${e}`);
|
||||||
});
|
});
|
||||||
logs.once('error', reject);
|
logs.once('error', reject);
|
||||||
logs.once('end', () => {
|
logs.once('end', () => {
|
||||||
@ -84,6 +89,7 @@ async function displayDeviceLogs(
|
|||||||
reject(new DeviceConnectionLostError());
|
reject(new DeviceConnectionLostError());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
logs.pipe(jsonStream);
|
||||||
});
|
});
|
||||||
} finally {
|
} finally {
|
||||||
process.removeListener('SIGINT', handleSignal);
|
process.removeListener('SIGINT', handleSignal);
|
||||||
@ -140,21 +146,6 @@ export function displayBuildLog(log: BuildLog, logger: Logger): void {
|
|||||||
logger.logBuild(toPrint);
|
logger.logBuild(toPrint);
|
||||||
}
|
}
|
||||||
|
|
||||||
// mutates serviceColours
|
|
||||||
function displayLogLine(
|
|
||||||
log: string | Buffer,
|
|
||||||
logger: Logger,
|
|
||||||
system: boolean,
|
|
||||||
filterServices?: string[],
|
|
||||||
): void {
|
|
||||||
try {
|
|
||||||
const obj: Log = JSON.parse(log.toString());
|
|
||||||
displayLogObject(obj, logger, system, filterServices);
|
|
||||||
} catch (e) {
|
|
||||||
logger.logDebug(`Dropping device log due to failed parsing: ${e}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function displayLogObject<T extends Log>(
|
export function displayLogObject<T extends Log>(
|
||||||
obj: T,
|
obj: T,
|
||||||
logger: Logger,
|
logger: Logger,
|
||||||
|
10
npm-shrinkwrap.json
generated
10
npm-shrinkwrap.json
generated
@ -2428,6 +2428,16 @@
|
|||||||
"@types/node": "*"
|
"@types/node": "*"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"@types/ndjson": {
|
||||||
|
"version": "2.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/ndjson/-/ndjson-2.0.0.tgz",
|
||||||
|
"integrity": "sha512-z1inV91BPfnnUwX0Q6TiIspIrhDsE7XJRGLutLGSRc++rQEqVzGxkG2xEKFgYjPVqaef4q3S4fXxcggJvfI70A==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"@types/node": "*",
|
||||||
|
"@types/through": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
"@types/net-keepalive": {
|
"@types/net-keepalive": {
|
||||||
"version": "0.4.1",
|
"version": "0.4.1",
|
||||||
"resolved": "https://registry.npmjs.org/@types/net-keepalive/-/net-keepalive-0.4.1.tgz",
|
"resolved": "https://registry.npmjs.org/@types/net-keepalive/-/net-keepalive-0.4.1.tgz",
|
||||||
|
@ -139,6 +139,7 @@
|
|||||||
"@types/mocha": "^8.0.4",
|
"@types/mocha": "^8.0.4",
|
||||||
"@types/mock-require": "^2.0.0",
|
"@types/mock-require": "^2.0.0",
|
||||||
"@types/moment-duration-format": "^2.2.2",
|
"@types/moment-duration-format": "^2.2.2",
|
||||||
|
"@types/ndjson": "^2.0.0",
|
||||||
"@types/net-keepalive": "^0.4.1",
|
"@types/net-keepalive": "^0.4.1",
|
||||||
"@types/nock": "^11.0.7",
|
"@types/nock": "^11.0.7",
|
||||||
"@types/node": "^10.17.28",
|
"@types/node": "^10.17.28",
|
||||||
@ -245,6 +246,7 @@
|
|||||||
"mixpanel": "^0.10.3",
|
"mixpanel": "^0.10.3",
|
||||||
"moment": "^2.27.0",
|
"moment": "^2.27.0",
|
||||||
"moment-duration-format": "^2.3.2",
|
"moment-duration-format": "^2.3.2",
|
||||||
|
"ndjson": "^2.0.0",
|
||||||
"node-cleanup": "^2.1.2",
|
"node-cleanup": "^2.1.2",
|
||||||
"node-unzip-2": "^0.2.8",
|
"node-unzip-2": "^0.2.8",
|
||||||
"oclif": "^1.16.1",
|
"oclif": "^1.16.1",
|
||||||
|
Loading…
Reference in New Issue
Block a user