Skip to content

Commit

Permalink
Merge branch 'fix-console-output-error-dialog'
Browse files Browse the repository at this point in the history
  • Loading branch information
raksooo committed Oct 20, 2023
2 parents 4319388 + 7d2b512 commit 73f7bc6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Line wrap the file at 100 chars. Th
### Fixed
- Show correct endpoint in CLI for custom relays.
- Lower risk of being rate limited.
- Fix error dialog when failing to write to console by handling the thrown error.

#### Windows
- Correctly detect whether OS is Windows Server (primarily for logging in daemon.log).
Expand Down
66 changes: 39 additions & 27 deletions gui/src/shared/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,55 @@ export class Logger {
this.outputs
.filter((output) => level <= output.level)
.forEach(async (output) => {
const maybePromise = output.write(level, message);
if (maybePromise instanceof Promise) {
try {
await maybePromise;
} catch (e) {
const error = e as Error;
console.error(
`${output.constructor.name}.write: ${error.message}. Original message: ${message}`,
);
}
try {
await output.write(level, message);
} catch (e) {
const error = e as Error;
console.error(
`${output.constructor.name}.write: ${error.message}. Original message: ${message}`,
);
}
});
}
}

export class ConsoleOutput implements ILogOutput {
private disabled = false;

constructor(public level: LogLevel) {}

public write(level: LogLevel, message: string) {
switch (level) {
case LogLevel.error:
console.error(message);
break;
case LogLevel.warning:
console.warn(message);
break;
case LogLevel.info:
console.info(message);
break;
case LogLevel.verbose:
console.log(message);
break;
case LogLevel.debug:
console.log(message);
break;
if (this.disabled) {
return;
}

try {
switch (level) {
case LogLevel.error:
console.error(message);
break;
case LogLevel.warning:
console.warn(message);
break;
case LogLevel.info:
console.info(message);
throw new Error('Log failed');
break;
case LogLevel.verbose:
console.log(message);
break;
case LogLevel.debug:
console.log(message);
break;
}
} catch (error) {
this.disabled = true;

const message = error instanceof Object && 'message' in error ? error.message : '';
logger.error('Disabling console output due to:', message, error);
}
}
}

export default new Logger();
const logger = new Logger();
export default logger;

0 comments on commit 73f7bc6

Please sign in to comment.