Skip to content

Commit

Permalink
Improve log legibility (#470)
Browse files Browse the repository at this point in the history
* Improve log legibility

Strips all escape codes from terminal output logs

* [Refactor] Simplify code, use log transforms

* [Refactor] Use manual log transforms

Framework is not appropriate for these two call sites - log is shared with entire app, has no filtering available, and requires type kludges.
  • Loading branch information
webfiltered authored Dec 12, 2024
1 parent 9aa4b21 commit 15560fb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/main-process/comfyDesktopApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ComfyServerConfig } from '../config/comfyServerConfig';
import fs from 'fs';
import { InstallOptions, type ElectronContextMenuOptions } from '../preload';
import path from 'path';
import { getModelsDirectory, validateHardware } from '../utils';
import { ansiCodes, getModelsDirectory, validateHardware } from '../utils';
import { DownloadManager } from '../models/DownloadManager';
import { VirtualEnvironment } from '../virtualEnvironment';
import { InstallWizard } from '../install/installWizard';
Expand Down Expand Up @@ -185,11 +185,11 @@ export class ComfyDesktopApp {
const virtualEnvironment = new VirtualEnvironment(this.basePath);
await virtualEnvironment.create({
onStdout: (data) => {
log.info(data);
log.info(data.replaceAll(ansiCodes, ''));
this.appWindow.send(IPC_CHANNELS.LOG_MESSAGE, data);
},
onStderr: (data) => {
log.error(data);
log.error(data.replaceAll(ansiCodes, ''));
this.appWindow.send(IPC_CHANNELS.LOG_MESSAGE, data);
},
});
Expand Down
8 changes: 7 additions & 1 deletion src/main-process/comfyServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { app } from 'electron';
import { IPC_CHANNELS, ServerArgs } from '../constants';
import { VirtualEnvironment } from '../virtualEnvironment';
import { rotateLogFiles } from '../utils';
import { ansiCodes, rotateLogFiles } from '../utils';
import { getAppResourcesPath } from '../install/resourcePaths';
import log from 'electron-log/main';
import path from 'path';
Expand Down Expand Up @@ -102,6 +102,12 @@ export class ComfyServer {
const comfyUILog = log.create({ logId: 'comfyui' });
comfyUILog.transports.file.fileName = 'comfyui.log';

// TODO: Check if electron-log has updated types
comfyUILog.transports.file.transforms.push(({ data }) => {
// @ts-expect-error electron-log types are broken. data and return type are `string`.
return typeof data === 'string' ? data.replaceAll(ansiCodes, '') : data;
});

const comfyServerProcess = this.virtualEnvironment.runPythonCommand(this.launchArgs, {
onStdout: (data) => {
comfyUILog.info(data);
Expand Down
9 changes: 3 additions & 6 deletions src/services/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getAppResourcesPath } from '../install/resourcePaths';
import log from 'electron-log/main';
import { AppWindow } from '../main-process/appWindow';
import { IPC_CHANNELS } from '../constants';
import { ansiCodes } from '../utils';

function parseLogFile(logPath: string): Set<string> {
console.log('Parsing log file:', logPath);
Expand Down Expand Up @@ -72,12 +73,8 @@ async function installCustomNodes(
'--no-deps',
];
const { exitCode } = await virtualEnvironment.runPythonCommandAsync(cmd, {
onStdout: (data) => {
log.info(data.toString());
},
onStderr: (data) => {
log.error(data.toString());
},
onStdout: (data) => log.info(data.toString().replaceAll(ansiCodes, '')),
onStderr: (data) => log.error(data.toString().replaceAll(ansiCodes, '')),
});
if (exitCode !== 0) {
log.error(`Failed to install custom nodes: ${exitCode}`);
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { exec } from 'child_process';
import { promisify } from 'util';
import log from 'electron-log/main';

export const ansiCodes = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;

export async function pathAccessible(path: string): Promise<boolean> {
try {
await fsPromises.access(path);
Expand Down

0 comments on commit 15560fb

Please sign in to comment.