Skip to content

Instantly share code, notes, and snippets.

@Mic92
Created May 31, 2019 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mic92/db496441cde56eb3b5f2b478213ca486 to your computer and use it in GitHub Desktop.
Save Mic92/db496441cde56eb3b5f2b478213ca486 to your computer and use it in GitHub Desktop.
printf aquivalent for the zig programming language
const std = @import("std");
const io = std.io;
const File = std.fs.File;
var stdout_file: File = undefined;
var stdout_file_out_stream: File.OutStream = undefined;
var stdout_stream: ?*io.OutStream(File.WriteError) = null;
var stdout_mutex = std.Mutex.init();
// Write to stdout ignoring errors
// Similar to std.debug.warn
pub fn printf(comptime fmt: []const u8, args: ...) void {
const held = stdout_mutex.acquire();
defer held.release();
const stdout = getStdoutStream() catch return;
stdout.print(fmt, args) catch return;
}
pub fn getStdoutStream() !*io.OutStream(File.WriteError) {
if (stdout_stream) |st| {
return st;
} else {
stdout_file = try io.getStdOut();
stdout_file_out_stream = stdout_file.outStream();
const st = &stdout_file_out_stream.stream;
stdout_stream = st;
return st;
}
}
pub fn main() anyerror!void {
const world = "world";
printf("hello {}\n", world);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment