From e52d411d60119c8d8cdb2ec2c79b0fb0ef9a2a4f Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Wed, 3 Apr 2024 16:17:20 -0400 Subject: [PATCH] Synchronized DefaultLogHandler writes. --- changelogs/3.0.1.md | 7 +++++++ source/slf4d/default_provider/handler.d | 14 ++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 changelogs/3.0.1.md diff --git a/changelogs/3.0.1.md b/changelogs/3.0.1.md new file mode 100644 index 0000000..aa1d93e --- /dev/null +++ b/changelogs/3.0.1.md @@ -0,0 +1,7 @@ +# Version 3.0.1 + +- Added synchronization to the DefaultLogHandler to avoid interleaving messages + between the stdout and stderr streams. Each is individually synchronized by + the library/system, but both are not synchronized with respect to each other, + so errors and info messages may overwrite each other when printed into a + single source. diff --git a/source/slf4d/default_provider/handler.d b/source/slf4d/default_provider/handler.d index cb99ee3..69ea138 100644 --- a/source/slf4d/default_provider/handler.d +++ b/source/slf4d/default_provider/handler.d @@ -35,12 +35,14 @@ class DefaultLogHandler : LogHandler { public void handle(immutable LogMessage msg) { import std.stdio; string logStr = formatLogMessage(msg, this.colored); - if (msg.level.value >= Levels.ERROR.value) { - stderr.writeln(logStr); - stderr.flush(); - } else { - stdout.writeln(logStr); - stdout.flush(); + synchronized(this) { + if (msg.level.value >= Levels.ERROR.value) { + stderr.writeln(logStr); + stderr.flush(); + } else { + stdout.writeln(logStr); + stdout.flush(); + } } } }