replace debug() with our custom logger

mostly we want trace() and log(). trace() can be enabled whenever
we want by flipping a flag and restarting box
This commit is contained in:
Girish Ramakrishnan
2026-03-12 22:55:28 +05:30
parent d57554a48c
commit 01d0c738bc
104 changed files with 1187 additions and 1174 deletions
+10 -10
View File
@@ -1,12 +1,12 @@
import assert from 'node:assert';
import BoxError from './boxerror.js';
import child_process from 'node:child_process';
import debugModule from 'debug';
import logger from './logger.js';
import path from 'node:path';
import safe from 'safetydance';
import _ from './underscore.js';
const debug = debugModule('box:shell');
const { log, trace } = logger('shell');
function lineCount(buffer) {
assert(Buffer.isBuffer(buffer));
@@ -30,7 +30,7 @@ function spawn(tag, file, args, options) {
assert(Array.isArray(args));
assert.strictEqual(typeof options, 'object'); // note: spawn() has no encoding option of it's own
debug(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')}`);
log(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')}`);
const maxLines = options.maxLines || Number.MAX_SAFE_INTEGER;
const logger = options.logger || null;
@@ -79,26 +79,26 @@ function spawn(tag, file, args, options) {
e.timedOut = timedOut;
e.terminated = terminated;
debug(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')} errored`, e);
log(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')} errored`, e);
reject(e);
});
cp.on('error', function (error) { // when the command itself could not be started
debug(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')} errored`, error);
log(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')} errored`, error);
});
cp.terminate = function () {
terminated = true;
// many approaches to kill sudo launched process failed. we now have a sudo wrapper to kill the full tree
child_process.execFile('/usr/bin/sudo', [ KILL_CHILD_CMD, cp.pid, process.pid ], { encoding: 'utf8' }, (error, stdout, stderr) => {
if (error) debug(`${tag}: failed to kill children`, stdout, stderr);
else debug(`${tag}: terminated ${cp.pid}`, stdout, stderr);
if (error) log(`${tag}: failed to kill children`, stdout, stderr);
else log(`${tag}: terminated ${cp.pid}`, stdout, stderr);
});
};
abortSignal?.addEventListener('abort', () => {
debug(`${tag}: aborting ${cp.pid}`);
log(`${tag}: aborting ${cp.pid}`);
cp.terminate();
}, { once: true });
@@ -106,10 +106,10 @@ function spawn(tag, file, args, options) {
if (options.timeout) {
killTimerId = setTimeout(async () => {
debug(`${tag}: timedout`);
log(`${tag}: timedout`);
timedOut = true;
if (typeof options.onTimeout !== 'function') return cp.terminate();
await safe(options.onTimeout(), { debug });
await safe(options.onTimeout(), { debug: log });
}, options.timeout);
}