Open demizer opened 3 years ago
I might be missing something, is this what you are looking for?;
const std = @import("std");
pub fn main() void {
std.log.info("{}-{}: Hello world", .{ @src().fn_name, @src().line });
}
I think the idea here is to be able to implement it in the log function only, without the caller having to provide the @src()
argument.
I have another janky implementation here that doesn't require an allocator. This allows one to set the logging format when testing. The test runner doesn't expose the root source file so it's not currently possible to setup a logging scope.
I'd like to point out that this would also be very helpful if it was implemented by default for std.debug.print
. The documentation says explicitly that it is meant to be used for printf debugging. I think that having something like the filename and line number printed by default next to print statement would be very useful in this scenario.
Is there any update here? Actually this is essential when debugging with stdout ...
When running code in a live environment, it is incredibly handy to have the line number and filename where the log statement was made to quickly jump to that portion of the code.
The following is the best I could come up with (with help from IRC) in a timely manner and limited experience (DON'T COPY THIS).
std.debug.StackIterator
is used to get the address of the function where the logging call is made and printed to a buffer. Then i get the end of the line where ": " (note the space) is and used that to slice the buffer. Very janky:With output that looks like:
There are other ways this could possibly be done with
@src()
but that would require it being used everywhere a log statement is made. Also having to create a buffer for every log statement is probably not good.There should be a better way to do this, for example, Go has it built into the logging library: https://golang.org/pkg/log/#pkg-constants. Same goes for Python.