How to print "standard" libc error messages?

In C I usually do:

        r = read(fd, rxb->pos, ba);
        if (-1 == r) {
                log_sys_msg("%s/%s() - read(%d): %s\n", me->name, __func__, fd, strerror(errno));
                goto __failure;
        }

I want to get same error string returned by strerror() in Zig, when using os.read()

_ =  os.read(fd, buf[0..]) catch |err| {
    // how to get libc error string for the err?...
}

Zig’s os.read() is a layer of abstraction over the underlying os.system.read(). If you use the underlying method then you can use os.errno() and if you link to libc then use c.strerror() just the same. But it’s really just a libc concept, so there’s no direct equivalent that I know of.

Ok. Actually I only wanted a “user friendly” error message (in terminal window), so just printing an error is ok.

const std = @import("std");

pub fn main() void {
    _ = std.os.open("no-such-file.txt", std.os.O.RDONLY, 0) catch |e| {
        std.debug.print("{}\n", .{e});
    };
}
$ ./e
error.FileNotFound

Glad you got it working as you wanted. You could check out the std.log module for “proper” logging, as std.debug.print is meant mostly for “print debugging”. Also @src() could give you the equivalent of c’s __func__ macro (and a few more), if you wanted some more similarity to the original message format.

Yes, I know about log namespace, thanx anyway. The point is that learning standard (or any other) library is separate part and as a rule this part is much bigger than learning a language itself (syntax, concepts…). For the moment std.debug.print is more than enough for me. :slight_smile:

No, I just wanted some equivalent for “no such file or directory”, “connection refused” and so on… FileNotFound, ConnectionRefused (and so on) are quite well suited for that.