Hi! In my C and Python code, sometimes I want to better track what data goes where when I don’t have a debugger attached. Take the following examples:
import inspect
def trace(fn):
def wrapper(*args, **kwargs):
_trace(fn)
fn(*args, **kwargs)
return wrapper
def _trace(fn):
sig = inspect.signature(fn)
params = []
for name, param in sig.parameters.items():
p = f"{name}"
if param.annotation != inspect.Parameter.empty:
p += f": {param.annotation.__name__}"
if param.default != inspect.Parameter.empty:
p += f" = {param.default}"
params.append(p)
print(f"{fn.__name__}({', '.join(params)}) -> {sig.return_annotation.__name__}")
@trace
def example(a: int, b, c: str = "hello") -> bool:
return True
example(1, 2)
Output: example(a: int, b, c: str = hello) -> bool
#include <stdio.h>
#define LOG_TRACE(fmt, ...) printf(fmt "\n", __VA_ARGS__);
int someFunc(void *foo_ptr, void *bar_ptr)
{
LOG_TRACE("%s(%p, %p)", __FUNCTION__, foo_ptr, bar_ptr);
// ...
return 0;
}
int main(void)
{
someFunc((void *)0xdeadbeef, (void *)0xcafebabe);
return 0;
}
Output: someFunc(0xdeadbeef, 0xcafebabe)
(I realize my quick Python example doesn’t actually print out the values I passed in [which is the goal here], but you get the gist of what I’m looking for).
I’m not sure if this is even possible in Zig… I tried looking through the source code for similar code, and using @src(), but nothing seems close to the examples above.
Does anyone have a function or clue that would point me in the right direction for this? Ultimately, I’m looking for something along these lines:
inline fn trace() void {
// Some magic here...
}
const Point = struct {
x: u32,
y: u32,
fn add(self: *@This(), scalar: u32) void {
self.*.x += scalar;
self.*.y += scalar;
}
};
fn foo(a: u64, b: Point) !u32 {
trace();
// ...
}
pub fn main() !void {
try foo(4, .{ .x = 4, .y = 9});
}
Output: foo(a: u64 = 4, b: Point = .{ .x = 4, .y = 9, fn add(...)})