# Locking && logging stdout

**URL:** <https://ziggit.dev/t/locking-logging-stdout/18628>\
**Category:** Help\
**Tags:** standard-library\
**Created:** [November 28, 2026, 11:11pm UTC](https://ziggit.dev/t/locking-logging-stdout/18628 "2026-11-28T23:11:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xolra0d](https://ziggit.dev/user_avatar/ziggit.dev/xolra0d/32/10695_2.png) [@xolra0d](https://ziggit.dev/u/xolra0d)\
**Post date:** [November 28, 2026, 11:11pm UTC](https://ziggit.dev/t/locking-logging-stdout/18628/1 "2026-11-28T23:11:10Z")

</div>

I want my program to log to stdout. However I cannot find a way to do it using `std.log` && global mutex, since `logFn` signature does not pass `std.Io` (for use in `std.Io.Mutex.lock` to lock stdout):

```zig
pub fn logFn(
    comptime message_level: std.log.Level,
    comptime scope: @EnumLiteral(),
    comptime format: []const u8,
    args: anytype,
) void;

```

I also don’t want to depend on libc for `std.c.pthread_mutex_t`. Is there any way to implement it without writing spinlock? Please, don’t mention `std.debug.lockStderr`, or that logging to stdout is a bad practice.

Thank you.

---

<div class="post-metadata">

**Author:** ![xolra0d](https://ziggit.dev/user_avatar/ziggit.dev/xolra0d/32/10695_2.png) [@xolra0d](https://ziggit.dev/u/xolra0d)\
**Post date:** [November 28, 2026, 11:16pm UTC](https://ziggit.dev/t/locking-logging-stdout/18628/2 "2026-11-28T23:16:25Z")

</div>

Maybe I can send `std.Io` as first arg in `args` var.

---

<div class="post-metadata">

**Author:** ![Cloudef](https://ziggit.dev/user_avatar/ziggit.dev/cloudef/32/1370_2.png) [@Cloudef](https://ziggit.dev/u/Cloudef)\
**Post date:** [November 28, 2026, 11:31pm UTC](https://ziggit.dev/t/locking-logging-stdout/18628/3 "2026-11-28T23:31:07Z")

</div>

You have to use global variable to pass something to `logFn`. The default log function just uses `std.debug.print` which uses global debug IO instance fyi, [https://codeberg.org/ziglang/zig/src/commit/eb23216dd90ef2e031498c65824858720f78082c/lib/std/log.zig#L96-L109](https://codeberg.org/ziglang/zig/src/commit/eb23216dd90ef2e031498c65824858720f78082c/lib/std/log.zig#L96-L109)

---

<div class="post-metadata">

**Author:** ![xolra0d](https://ziggit.dev/user_avatar/ziggit.dev/xolra0d/32/10695_2.png) [@xolra0d](https://ziggit.dev/u/xolra0d)\
**Post date:** [November 28, 2026, 11:38pm UTC](https://ziggit.dev/t/locking-logging-stdout/18628/4 "2026-11-28T23:38:06Z")

</div>

I came up with this solution:

```zig
const std = @import("std");

var stdoutMutex: std.Io.Mutex = .init;

// For locking `std.Io.Mutex`. Expected to be set first thing in
// main using init(io: *const std.Io)` fn before any call to `logFn`
// is received.
var logIo: *const std.Io = undefined;

/// Supply Io for multi-threaded logging.
pub fn init(io: *const std.Io) void {
    logIo = io;
}

pub fn logFn(
    comptime message_level: std.log.Level,
    comptime scope: @EnumLiteral(),
    comptime format: []const u8,
    args: anytype,
) void {
    stdoutMutex.lockUncancelable(logIo.*);
    defer stdoutMutex.unlock(logIo.*);
...

```

Not very strict, but should be ok.

---

<div class="post-metadata">

**Author:** ![Cloudef](https://ziggit.dev/user_avatar/ziggit.dev/cloudef/32/1370_2.png) [@Cloudef](https://ziggit.dev/u/Cloudef)\
**Post date:** [November 28, 2026, 11:48pm UTC](https://ziggit.dev/t/locking-logging-stdout/18628/5 "2026-11-28T23:48:52Z")

</div>

You might want to store and pass Io as a value, not pointer.
