qol fix error handling in deinit

This commit is contained in:
2026-08-08 00:08:32 +02:00
parent f1b07147b1
commit 0d3e97423a

View File

@@ -12,7 +12,7 @@ pub const Level = enum {
fatal, fatal,
/// Returns the fixed-width, uppercase label used as the log-line /// Returns the fixed-width, uppercase label used as the log-line
/// prefix for `level` (e.g. `"INFO "` for `.info`), derived from /// prefix for `level` (e.g. `"[INFO] "` for `.info`), derived from
/// the enum's own field name via `@tagName`. /// the enum's own field name via `@tagName`.
/// All labels are padded to the width of the longest level name, /// All labels are padded to the width of the longest level name,
/// so prefixes line up in a fixed-width terminal/file. /// so prefixes line up in a fixed-width terminal/file.
@@ -45,14 +45,10 @@ pub const Level = enum {
/// `level` sets the minimum severity that will be written; messages /// `level` sets the minimum severity that will be written; messages
/// below this level are discarded. /// below this level are discarded.
/// ///
/// `buffer` is the size, in bytes, of the internal write buffer /// `buffer` is the size, in bytes, allocated for the internal write
/// allocated for `writer`. Larger buffers reduce the number of /// buffer. Larger buffers reduce the number of underlying
/// underlying writes at the cost of more memory and higher latency /// writes at the cost of more memory and higher latency
/// before data is flushed. /// before data is flushed.
///
/// `writer` is the underlying file the logger writes to (e.g. stderr
/// or a log file). The logger takes no ownership of it beyond the
/// lifetime of the wrapping `std.Io.File.Writer`.
pub const InitOptions = struct { pub const InitOptions = struct {
pub const ErrorCallback = Logger.ErrorCallback; pub const ErrorCallback = Logger.ErrorCallback;
@@ -139,8 +135,9 @@ pub const Logger = struct {
/// and `WriterState`. /// and `WriterState`.
/// ///
/// Safe to call once initialization via `init` has succeeded. /// Safe to call once initialization via `init` has succeeded.
/// Flush errors are silently ignored, since there is no caller /// Flush and lock errors are reported via `on_error` if set,
/// left to meaningfully report them to at teardown time. /// but otherwise ignored, since there is no caller left to
/// meaningfully return them to at teardown time.
/// ///
/// Must not be called more than once, as the underlying /// Must not be called more than once, as the underlying
/// allocations are freed unconditionally. /// allocations are freed unconditionally.
@@ -148,10 +145,10 @@ pub const Logger = struct {
{ {
if (logger.state.mutex.lock(logger.io)) |_| if (logger.state.mutex.lock(logger.io)) |_|
{ {
logger.state.writer.interface.flush() catch {}; logger.state.writer.interface.flush() catch |e| if (logger.on_error) |h| h(e);
logger.state.mutex.unlock(logger.io); logger.state.mutex.unlock(logger.io);
} }
else |_| {} else |e| { if (logger.on_error) |h| h(e); }
logger.allocator.free(logger.state.buffer); logger.allocator.free(logger.state.buffer);
logger.allocator.destroy(logger.state); logger.allocator.destroy(logger.state);