refactor tail invokation into logtail.sh

This commit is contained in:
Girish Ramakrishnan
2023-03-27 10:38:09 +02:00
parent 456da972e9
commit 603f92251e
7 changed files with 80 additions and 85 deletions

View File

@@ -41,7 +41,7 @@ const apps = require('./apps.js'),
eventlog = require('./eventlog.js'),
execSync = require('child_process').execSync,
fs = require('fs'),
LogStream = require('./log-stream.js'),
logs = require('./logs.js'),
mail = require('./mail.js'),
notifications = require('./notifications.js'),
oidc = require('./oidc.js'),
@@ -53,7 +53,6 @@ const apps = require('./apps.js'),
services = require('./services.js'),
settings = require('./settings.js'),
shell = require('./shell.js'),
spawn = require('child_process').spawn,
sysinfo = require('./sysinfo.js'),
tasks = require('./tasks.js'),
users = require('./users.js');
@@ -217,27 +216,16 @@ async function getLogs(unit, options) {
assert.strictEqual(typeof unit, 'string');
assert(options && typeof options === 'object');
assert.strictEqual(typeof options.lines, 'number');
assert.strictEqual(typeof options.format, 'string');
assert.strictEqual(typeof options.follow, 'boolean');
debug(`Getting logs for ${unit}`);
const lines = options.lines === -1 ? '+1' : options.lines,
format = options.format || 'json',
follow = options.follow;
debug('Getting logs for %s as %s', unit, format);
let args = [ '--lines=' + lines ];
if (follow) args.push('--follow');
// need to handle box.log without subdir
if (unit === 'box') args.push(path.join(paths.LOG_DIR, 'box.log'));
else if (unit.startsWith('crash-')) args.push(path.join(paths.CRASH_LOG_DIR, unit.slice(6) + '.log'));
let logFile = '';
if (unit === 'box') logFile = path.join(paths.LOG_DIR, 'box.log'); // box.log is at the top
else if (unit.startsWith('crash-')) logFile = path.join(paths.CRASH_LOG_DIR, unit.slice(6) + '.log');
else throw new BoxError(BoxError.BAD_FIELD, `No such unit '${unit}'`);
const cp = spawn('/usr/bin/tail', args);
const cp = logs.tail([logFile], { lines: options.lines, follow: options.follow });
const logStream = new LogStream({ format, source: unit });
const logStream = new logs.LogStream({ format: options.format || 'json', source: unit });
logStream.close = cp.kill.bind(cp, 'SIGKILL'); // hook for caller. closing stream kills the child process
cp.stdout.pipe(logStream);