diff --git a/package.json b/package.json index 789db6f3..3f64559f 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "js-yaml": "^3.13.1", "limiter": "^1.1.5", "pem": "^1.14.2", + "rotating-file-stream": "^2.1.1", "safe-compare": "^1.1.4", "semver": "^7.1.3", "tar": "^6.0.1", diff --git a/src/node/wrapper.ts b/src/node/wrapper.ts index cb0aefab..6e2e7e74 100644 --- a/src/node/wrapper.ts +++ b/src/node/wrapper.ts @@ -1,6 +1,9 @@ -import { logger, field } from "@coder/logger" +import { field, logger } from "@coder/logger" import * as cp from "child_process" +import * as path from "path" +import * as rfs from "rotating-file-stream" import { Emitter } from "../common/emitter" +import { paths } from "./util" interface HandshakeMessage { type: "handshake" @@ -140,8 +143,17 @@ export interface WrapperOptions { export class WrapperProcess { private process?: cp.ChildProcess private started?: Promise + private readonly logStdoutStream: rfs.RotatingFileStream + private readonly logStderrStream: rfs.RotatingFileStream public constructor(private currentVersion: string, private readonly options?: WrapperOptions) { + const opts = { + size: "10M", + maxFiles: 10, + } + this.logStdoutStream = rfs.createStream(path.join(paths.data, "coder-logs", "code-server-stdout.log"), opts) + this.logStderrStream = rfs.createStream(path.join(paths.data, "coder-logs", "code-server-stderr.log"), opts) + ipcMain().onDispose(() => { if (this.process) { this.process.removeAllListeners() @@ -176,6 +188,15 @@ export class WrapperProcess { public start(): Promise { if (!this.started) { this.started = this.spawn().then((child) => { + // Log both to stdout and to the log directory. + if (child.stdout) { + child.stdout.pipe(this.logStdoutStream) + child.stdout.pipe(process.stdout) + } + if (child.stderr) { + child.stderr.pipe(this.logStderrStream) + child.stderr.pipe(process.stderr) + } logger.debug(`spawned inner process ${child.pid}`) ipcMain() .handshake(child) @@ -205,7 +226,7 @@ export class WrapperProcess { CODE_SERVER_PARENT_PID: process.pid.toString(), NODE_OPTIONS: nodeOptions, }, - stdio: ["inherit", "inherit", "inherit", "ipc"], + stdio: ["ipc"], }) } } diff --git a/yarn.lock b/yarn.lock index cf61ced9..85ec456d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6144,6 +6144,11 @@ ripemd160@^2.0.0, ripemd160@^2.0.1: hash-base "^3.0.0" inherits "^2.0.1" +rotating-file-stream@^2.1.1: + version "2.1.3" + resolved "https://registry.yarnpkg.com/rotating-file-stream/-/rotating-file-stream-2.1.3.tgz#4b3cc8f56ae70b3e30ccdb4ee6b14d95e66b02bb" + integrity sha512-zZ4Tkngxispo7DgiTqX0s4ChLtM3qET6iYsDA9tmgDEqJ3BFgRq/ZotsKEDAYQt9pAn9JwwqT27CSwQt3CTxNg== + run-async@^2.4.0: version "2.4.1" resolved "https://registry.yarnpkg.com/run-async/-/run-async-2.4.1.tgz#8440eccf99ea3e70bd409d49aab88e10c189a455"