sivel/zprint
Thread-safe print utilities for Zig CLI applications with formatted printing to stdout and stderr
Thread-safe print utilities for Zig CLI applications with formatted printing to stdout and stderr.
WriterConfig
allows custom file handles with their own mutexesAdd zprint to your project using zig fetch
:
zig fetch --save https://github.com/sivel/zprint/archive/v0.1.0.tar.gz
Then in your build.zig
, add the dependency:
const zprint = b.dependency("zprint", .{});
exe.root_module.addImport("zprint", zprint.module("zprint"));
const zprint = @import("zprint");
// Print to stdout with error handling
try zprint.stdout("Hello {s}! Number: {d}\n", .{ "world", 42 });
// Print to stderr with error handling
try zprint.stderr("Error: {s}\n", .{"something went wrong"});
// Debug versions that silently ignore errors
zprint.debug.stdout("Debug message to stdout\n", .{});
zprint.debug.stderr("Debug message to stderr\n", .{});
// Create your own writer config
var custom_mutex = std.Thread.Mutex.Recursive.init;
var custom_file_writer: std.fs.File.Writer = .{
.interface = std.fs.File.Writer.initInterface(&.{}),
.file = .stdout(),
.mode = .streaming,
};
const custom_config = zprint.WriterConfig{
.mutex = &custom_mutex,
.file_writer = &custom_file_writer,
.file = .stdout(),
};
try zprint.printConfig(custom_config, "Custom config: {s}\n", .{"works!"});
// Use with any writer
const writer = lockWriterConfig(custom_config, &buffer);
defer unlockWriterConfig(custom_config);
try zprint.print(writer, "Generic printing: {}\n", .{value});
stdout(fmt, args) !void
- Print to stdout with error handlingstderr(fmt, args) !void
- Print to stderr with error handlingprintConfig(config, fmt, args) !void
- Print using custom writer configprint(writer, fmt, args) !void
- Core function for any writerdebug.stdout(fmt, args) void
- Print to stdout, ignore errorsdebug.stderr(fmt, args) void
- Print to stderr, ignore errorsWriterConfig
- Configuration struct for custom writerslockWriterConfig(config, buffer) *Writer
- Lock a writer for useunlockWriterConfig(config) void
- Unlock a writerZig's standard library provides std.debug.print
for debugging (stderr only) but lacks a higher level stdout printing solution for CLI applications. zprint fills this gap by providing:
std.debug.print
which only goes to stderrMIT License - see LICENSE file for details.