Get caller's std.builtin.SourceLocation?

I think you want this builtin? @src:

const std = @import("std");

fn exampleFunction() !void {
    const s = @src();
    std.debug.print("file: {s}\n", .{s.file});
    std.debug.print("fn_name: {s}\n", .{s.fn_name});
    std.debug.print("line: {d}\n", .{s.line});
    std.debug.print("column: {d}\n", .{s.column});
}

pub fn main() !void {
    try exampleFunction();
}
file: sourcelocation.zig
fn_name: exampleFunction
line: 4
column: 15

If you want to create some kind of utility printf debugging thing you need to pass @src() to the function as a parameter with type std.builtin.SourceLocation:

fn print(s: std.builtin.SourceLocation, comptime fmt: []const u8, args: anytype) void {
    std.debug.print(fmt, args);
    std.debug.print("file: {s}\n", .{s.file});
    std.debug.print("fn_name: {s}\n", .{s.fn_name});
    std.debug.print("line: {d}\n", .{s.line});
    std.debug.print("column: {d}\n", .{s.column});
}

pub fn main() !void {
    const foo = 12;
    print(@src(), "foo is: {d}\n", .{foo});
}

Another way would be to use:

3 Likes