Syndica/sig

Logger: Scoped logging

0xNineteen opened this issue · 5 comments

to improve logging readability it would be nice to have scope titles -- something like:

scoped_logger = logger.scope('gossip-process-messages')
gossip_service.processMessages(scoped_logger, ...) 

// then log messages would prefix with [{scope}]
// eg, [gossip-process-messages] {normal logs}
dnut commented

Most logging libraries have something like this. This would make it a lot easier to make sense of the logs, and it will have increasing importance as we build out more and more components. I've put some component names manually into individual log messages, but it would be better to manage it more centrally.

I think the ideal would be to automatically derive it from the call stack. I already looked into and I think I have an approach that would work. I just didn't decide on a format or try to integrate it with the logging library yet.

const std = @import("std");

pub fn main() void {
    const info = callingContext() catch unreachable;
    std.debug.print("file name: {s}\nname: {s}\n unit: {s}\n", .{
        info.line_info.?.file_name,
        info.symbol_name,
        info.compile_unit_name,
    });
}

pub fn callingContext() !std.debug.SymbolInfo {
    const address = @returnAddress();
    const debug_info = try std.debug.getSelfDebugInfo();
    const module = try debug_info.getModuleForAddress(address);
    return try module.getSymbolAtAddress(debug_info.allocator, address);
}

@dnut Looking at the callingContext() I was wondering if it would work when compiled in non debug mode, ie ReleaseSafe, ReleaseSmall, ReleaseFast. Quickly tried it and in these mode, it prints out gibberish.

Got ideas on how to handle this?

dnut commented

@dadepo For me it works exactly the same regardless of the optimize mode. What target are you building for?

... What target are you building for?

@dnut target is arm64... I am on Apple M1

 ➜ zig build-exe src/main.zig -O ReleaseSmall
 ➜ ./main                                    
unit: �o���g��_��W��O��{��@�������Ixh�����i�)��
|@�!)�"!�a�!����R��l�Y
...
...
dnut commented

completed in #277