Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #62 from AtkinsSJ/log-to-file
Browse files Browse the repository at this point in the history
On Node, send console.foo() logging to a file, or discard it
  • Loading branch information
KernelDeimos authored Mar 14, 2024
2 parents 56be4f1 + 856e94a commit 258226a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
50 changes: 49 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"dependencies": {
"@pkgjs/parseargs": "^0.11.0",
"capture-console": "^1.0.2",
"cli-columns": "^4.0.0",
"columnify": "^1.6.0",
"fs-mode-to-string": "^0.0.2",
Expand Down
35 changes: 35 additions & 0 deletions src/main_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,41 @@ import { launchPuterShell } from './puter-shell/main.js';
import { NodeStdioPTT } from './pty/NodeStdioPTT.js';
import { CreateFilesystemProvider } from './platform/node/filesystem.js';
import { CreateEnvProvider } from './platform/node/env.js';
import { parseArgs } from '@pkgjs/parseargs';
import capcon from 'capture-console';
import fs from 'fs';

const { values } = parseArgs({
options: {
'log': {
type: 'string',
}
},
args: process.argv.slice(2),
});
const logFile = await (async () => {
if (!values.log)
return;
return await fs.promises.open(values.log, 'w');
})();


// Capture console.foo() output and either send it to the log file, or to nowhere.
for (const [name, oldMethod] of Object.entries(console)) {
console[name] = async (...args) => {
let result;
const stdio = capcon.interceptStdio(() => {
result = oldMethod(...args);
});

if (logFile) {
await logFile.write(stdio.stdout);
await logFile.write(stdio.stderr);
}

return result;
};
}

const ctx = new Context({
ptt: new NodeStdioPTT(),
Expand Down

0 comments on commit 258226a

Please sign in to comment.