Created
May 31, 2019 06:19
-
-
Save Mic92/db496441cde56eb3b5f2b478213ca486 to your computer and use it in GitHub Desktop.
printf aquivalent for the zig programming language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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