diff --git a/readme.md b/readme.md index 84a1507a9..24c433dae 100644 --- a/readme.md +++ b/readme.md @@ -253,7 +253,7 @@ pub fn main() !void { return; } - std.log.debug("Account info: {any}", .{resp.result().value.data}); + std.log.debugf("Account info: {any}", .{resp.result().value.data}); } ``` @@ -290,7 +290,7 @@ pub fn main() !void { return; } - std.log.debug("balance info: {any}", .{resp.result().value}); + std.log.debugf("balance info: {any}", .{resp.result().value}); } ``` @@ -327,7 +327,7 @@ pub fn main() !void { return; } - std.log.debug("block height: {any}", .{resp.result()}); + std.log.debugf("block height: {any}", .{resp.result()}); } ``` @@ -378,7 +378,7 @@ pub fn main() !void { return; } - std.log.debug("block info: {any}", .{resp.result()}); + std.log.debugf("block info: {any}", .{resp.result()}); } ``` @@ -429,7 +429,7 @@ pub fn main() !void { return; } - std.log.debug("block production info: {any}", .{resp.result()}); + std.log.debugf("block production info: {any}", .{resp.result()}); } ``` @@ -466,7 +466,7 @@ pub fn main() !void { return; } - std.log.debug("block commitment info: {any}", .{resp.result()}); + std.log.debugf("block commitment info: {any}", .{resp.result()}); } ``` @@ -513,7 +513,7 @@ pub fn main() !void { return; } - std.log.debug("blocks: {any}", .{resp.result()}); + std.log.debugf("blocks: {any}", .{resp.result()}); } ``` @@ -559,7 +559,7 @@ pub fn main() !void { return; } - std.log.debug("blocks: {any}", .{resp.result()}); + std.log.debugf("blocks: {any}", .{resp.result()}); } ``` @@ -596,7 +596,7 @@ pub fn main() !void { return; } - std.log.debug("block time: {any}", .{resp.result()}); + std.log.debugf("block time: {any}", .{resp.result()}); } ``` @@ -633,7 +633,7 @@ pub fn main() !void { return; } - std.log.debug("cluster nodes: {any}", .{resp.result()}); + std.log.debugf("cluster nodes: {any}", .{resp.result()}); } ``` @@ -679,7 +679,7 @@ pub fn main() !void { return; } - std.log.debug("epoch info: {any}", .{resp.result()}); + std.log.debugf("epoch info: {any}", .{resp.result()}); } ``` @@ -716,7 +716,7 @@ pub fn main() !void { return; } - std.log.debug("epoch schedule: {any}", .{resp.result()}); + std.log.debugf("epoch schedule: {any}", .{resp.result()}); } ``` @@ -762,7 +762,7 @@ pub fn main() !void { return; } - std.log.debug("message fee info: {any}", .{resp.result()}); + std.log.debugf("message fee info: {any}", .{resp.result()}); } ``` @@ -799,7 +799,7 @@ pub fn main() !void { return; } - std.log.debug("first available block: {any}", .{resp.result()}); + std.log.debugf("first available block: {any}", .{resp.result()}); } ``` @@ -833,7 +833,7 @@ pub fn main() !void { return; } - std.log.debug("genesis hash: {any}", .{resp.result()}); + std.log.debugf("genesis hash: {any}", .{resp.result()}); } ``` @@ -872,7 +872,7 @@ pub fn main() !void { return; } - std.log.debug("health: {any}", .{resp.result()}); + std.log.debugf("health: {any}", .{resp.result()}); } ``` @@ -911,7 +911,7 @@ pub fn main() !void { return; } - std.log.debug("snapshot info: {any}", .{resp.result()}); + std.log.debugf("snapshot info: {any}", .{resp.result()}); } ``` @@ -948,7 +948,7 @@ pub fn main() !void { return; } - std.log.debug("indentity info: {any}", .{resp.result()}); + std.log.debugf("indentity info: {any}", .{resp.result()}); } ``` @@ -994,7 +994,7 @@ pub fn main() !void { return; } - std.log.debug("inflation info: {any}", .{resp.result()}); + std.log.debugf("inflation info: {any}", .{resp.result()}); } ``` @@ -1031,7 +1031,7 @@ pub fn main() !void { return; } - std.log.debug("inflation rate: {any}", .{resp.result()}); + std.log.debugf("inflation rate: {any}", .{resp.result()}); } ``` @@ -1087,7 +1087,7 @@ pub fn main() !void { return; } - std.log.debug("inflation reward info: {any}", .{resp.result()}); + std.log.debugf("inflation reward info: {any}", .{resp.result()}); } ``` @@ -1134,7 +1134,7 @@ pub fn main() !void { return; } - std.log.debug("largest accounts: {any}", .{resp.result()}); + std.log.debugf("largest accounts: {any}", .{resp.result()}); } ``` @@ -1182,7 +1182,7 @@ pub fn main() !void { return; } - std.log.debug("latest blockhash: {any}", .{resp.result()}); + std.log.debugf("latest blockhash: {any}", .{resp.result()}); } ``` @@ -1229,7 +1229,7 @@ pub fn main() !void { return; } - std.log.debug("leader schedule: {any}", .{resp.result()}); + std.log.debugf("leader schedule: {any}", .{resp.result()}); } ``` @@ -1266,7 +1266,7 @@ pub fn main() !void { return; } - std.log.debug("max retransmit slot: {any}", .{resp.result()}); + std.log.debugf("max retransmit slot: {any}", .{resp.result()}); } ``` @@ -1303,7 +1303,7 @@ pub fn main() !void { return; } - std.log.debug("max shred insert slot: {any}", .{resp.result()}); + std.log.debugf("max shred insert slot: {any}", .{resp.result()}); } ``` @@ -1340,7 +1340,7 @@ pub fn main() !void { return; } - std.log.debug("minimum balance: {any}", .{resp.result()}); + std.log.debugf("minimum balance: {any}", .{resp.result()}); } ``` @@ -1395,7 +1395,7 @@ pub fn main() !void { return; } - std.log.debug("multiple accounts: {any}", .{resp.result()}); + std.log.debugf("multiple accounts: {any}", .{resp.result()}); } ``` @@ -1452,7 +1452,7 @@ pub fn main() !void { return; } - std.log.debug("program accounts: {any}", .{resp.result()}); + std.log.debugf("program accounts: {any}", .{resp.result()}); } ``` @@ -1491,7 +1491,7 @@ pub fn main() !void { return; } - std.log.debug("recent performance samples: {any}", .{resp.result()}); + std.log.debugf("recent performance samples: {any}", .{resp.result()}); } ``` @@ -1528,7 +1528,7 @@ pub fn main() !void { return; } - std.log.debug("recent prioritization fees: {any}", .{resp.result()}); + std.log.debugf("recent prioritization fees: {any}", .{resp.result()}); } ``` @@ -1580,7 +1580,7 @@ pub fn main() !void { return; } - std.log.debug("signatures: {any}", .{resp.result()}); + std.log.debugf("signatures: {any}", .{resp.result()}); } ```` @@ -1630,7 +1630,7 @@ pub fn main() !void { return; } - std.log.debug("signature statuses: {any}", .{resp.result()}); + std.log.debugf("signature statuses: {any}", .{resp.result()}); } ``` @@ -1677,7 +1677,7 @@ pub fn main() !void { return; } - std.log.debug("slot leader: {any}", .{resp.result()}); + std.log.debugf("slot leader: {any}", .{resp.result()}); } ``` @@ -1714,7 +1714,7 @@ pub fn main() !void { return; } - std.log.debug("slot leaders: {any}", .{resp.result()}); + std.log.debugf("slot leaders: {any}", .{resp.result()}); } ``` @@ -1764,7 +1764,7 @@ pub fn main() !void { return; } - std.log.debug("stake activation: {any}", .{resp.result()}); + std.log.debugf("stake activation: {any}", .{resp.result()}); } ``` @@ -1810,7 +1810,7 @@ pub fn main() !void { return; } - std.log.debug("min stake delegation: {any}", .{resp.result()}); + std.log.debugf("min stake delegation: {any}", .{resp.result()}); } ``` @@ -1857,7 +1857,7 @@ pub fn main() !void { return; } - std.log.debug("get supply: {any}", .{resp.result()}); + std.log.debugf("get supply: {any}", .{resp.result()}); } ``` @@ -1906,7 +1906,7 @@ pub fn main() !void { return; } - std.log.debug("token account balance: {any}", .{resp.result()}); + std.log.debugf("token account balance: {any}", .{resp.result()}); } ``` @@ -1966,7 +1966,7 @@ pub fn main() !void { return; } - std.log.debug("token accounts: {any}", .{resp.result()}); + std.log.debugf("token accounts: {any}", .{resp.result()}); } ``` @@ -2026,7 +2026,7 @@ pub fn main() !void { return; } - std.log.debug("token accounts: {any}", .{resp.result()}); + std.log.debugf("token accounts: {any}", .{resp.result()}); } ``` @@ -2075,7 +2075,7 @@ pub fn main() !void { return; } - std.log.debug("token largest accounts: {any}", .{resp.result()}); + std.log.debugf("token largest accounts: {any}", .{resp.result()}); } ``` @@ -2124,7 +2124,7 @@ pub fn main() !void { return; } - std.log.debug("token supply: {any}", .{resp.result()}); + std.log.debugf("token supply: {any}", .{resp.result()}); } ``` @@ -2174,7 +2174,7 @@ pub fn main() !void { return; } - std.log.debug("transaction: {any}", .{resp.result()}); + std.log.debugf("transaction: {any}", .{resp.result()}); } ``` @@ -2221,7 +2221,7 @@ pub fn main() !void { return; } - std.log.debug("transaction count: {any}", .{resp.result()}); + std.log.debugf("transaction count: {any}", .{resp.result()}); } ``` @@ -2258,7 +2258,7 @@ pub fn main() !void { return; } - std.log.debug("version: {any}", .{resp.result()}); + std.log.debugf("version: {any}", .{resp.result()}); } ``` @@ -2310,7 +2310,7 @@ pub fn main() !void { return; } - std.log.debug("vote accounts: {any}", .{resp.result()}); + std.log.debugf("vote accounts: {any}", .{resp.result()}); } ``` @@ -2357,7 +2357,7 @@ pub fn main() !void { return; } - std.log.debug("blockhash valid: {any}", .{resp.result()}); + std.log.debugf("blockhash valid: {any}", .{resp.result()}); } ``` @@ -2394,7 +2394,7 @@ pub fn main() !void { return; } - std.log.debug("minimum ledger slot: {any}", .{resp.result()}); + std.log.debugf("minimum ledger slot: {any}", .{resp.result()}); } ``` @@ -2443,7 +2443,7 @@ pub fn main() !void { return; } - std.log.debug("airdrop result: {any}", .{resp.result()}); + std.log.debugf("airdrop result: {any}", .{resp.result()}); } ``` @@ -2498,7 +2498,7 @@ pub fn main() !void { return; } - std.log.debug("tx signature: {any}", .{resp.result()}); + std.log.debugf("tx signature: {any}", .{resp.result()}); } ``` @@ -2557,7 +2557,7 @@ pub fn main() !void { return; } - std.log.debug("simulate tx info: {any}", .{resp.result()}); + std.log.debugf("simulate tx info: {any}", .{resp.result()}); } ``` diff --git a/src/cmd/cmd.zig b/src/cmd/cmd.zig index 8c35d0dae..980f776c2 100644 --- a/src/cmd/cmd.zig +++ b/src/cmd/cmd.zig @@ -3,6 +3,7 @@ const cli = @import("zig-cli"); const gossipCmd = @import("../gossip/cmd.zig"); const base58 = @import("base58-zig"); const LegacyContactInfo = @import("../gossip/crds.zig").LegacyContactInfo; +const Logger = @import("../trace/log.zig").Logger; const io = std.io; var allocator = std.heap.GeneralPurposeAllocator(.{}){}; @@ -44,7 +45,10 @@ var app = &cli.App{ // prints (and creates if DNE) pubkey in ~/.sig/identity.key fn identity(_: []const []const u8) !void { - const id = try gossipCmd.getOrInitIdentity(gpa); + var logger = Logger.init(gpa, .debug); + logger.spawn(); + + const id = try gossipCmd.getOrInitIdentity(gpa, logger); var pk: [50]u8 = undefined; var size = try base58Encoder.encode(&id.public_key.toBytes(), &pk); try std.io.getStdErr().writer().print("Identity: {s}\n", .{pk[0..size]}); @@ -52,9 +56,12 @@ fn identity(_: []const []const u8) !void { // gossip entrypoint fn gossip(_: []const []const u8) !void { + var logger = Logger.init(gpa, .debug); + logger.spawn(); + var gossip_port: u16 = @intCast(gossip_port_option.value.int.?); var entrypoints = std.ArrayList(LegacyContactInfo).init(gpa); - try gossipCmd.runGossipService(gossip_port, entrypoints); + try gossipCmd.runGossipService(gossip_port, entrypoints, logger); } pub fn run() !void { diff --git a/src/gossip/cluster_info.zig b/src/gossip/cluster_info.zig index 863a93e7b..ae951d193 100644 --- a/src/gossip/cluster_info.zig +++ b/src/gossip/cluster_info.zig @@ -15,14 +15,13 @@ const bincode = @import("bincode-zig"); const CrdsValue = @import("crds.zig").CrdsValue; const CrdsData = @import("crds.zig").CrdsData; const Version = @import("crds.zig").Version; +const Logger = @import("../trace/log.zig").Logger; const RwLock = std.Thread.RwLock; const GOSSIP_SLEEP_MILLIS: i64 = 100; const GossipController = struct {}; -const logger = std.log.scoped(.cluster_info); - pub const ClusterInfo = struct { gossip_controller: GossipController = .{}, our_keypair: KeyPair, @@ -58,13 +57,13 @@ pub const ClusterInfo = struct { keypair: KeyPair, }; - pub fn initSpy(allocator: std.mem.Allocator, gossip_socket_addr: SocketAddr, entrypoints: ArrayList(LegacyContactInfo)) !ClusterInfoPlus { + pub fn initSpy(allocator: std.mem.Allocator, gossip_socket_addr: SocketAddr, entrypoints: ArrayList(LegacyContactInfo), logger: *Logger) !ClusterInfoPlus { // bind to gosssip socket port var gossip_socket = try UdpSocket.create(.ipv4, .udp); try gossip_socket.bind(gossip_socket_addr.toEndpoint()); // get or init our keypair - var keypair = try cmd.getOrInitIdentity(allocator); + var keypair = try cmd.getOrInitIdentity(allocator, logger); // build our spy contact info var our_contact_info = try ContactInfo.initSpy( diff --git a/src/gossip/cmd.zig b/src/gossip/cmd.zig index 132a22b96..585aa8800 100644 --- a/src/gossip/cmd.zig +++ b/src/gossip/cmd.zig @@ -8,16 +8,15 @@ const AtomicBool = std.atomic.Atomic(bool); const SocketAddr = @import("net.zig").SocketAddr; const ArrayList = std.ArrayList; const LegacyContactInfo = @import("crds.zig").LegacyContactInfo; +const Logger = @import("../trace/log.zig").Logger; var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa_allocator = gpa.allocator(); -const logger = std.log.scoped(.cmd); - const IDENTITY_KEYPAIR_DIR = "/.sig"; const IDENTITY_KEYPAIR_PATH = "/identity.key"; -pub fn getOrInitIdentity(allocator: std.mem.Allocator) !Keypair { +pub fn getOrInitIdentity(allocator: std.mem.Allocator, logger: *Logger) !Keypair { const home_dir = std.os.getenv("HOME") orelse return error.UnableDetectHomeDir; var path = try std.mem.concat(allocator, u8, &[_][]const u8{ home_dir, IDENTITY_KEYPAIR_DIR, IDENTITY_KEYPAIR_PATH }); @@ -36,7 +35,7 @@ pub fn getOrInitIdentity(allocator: std.mem.Allocator) !Keypair { // create ~/.sig dir var dir = try std.mem.concat(allocator, u8, &[_][]const u8{ home_dir, IDENTITY_KEYPAIR_DIR }); std.fs.makeDirAbsolute(dir) catch { - logger.debug("sig directory already exists...", .{}); + logger.debugf("sig directory already exists...", .{}); }; // create new keypair @@ -55,15 +54,15 @@ pub fn getOrInitIdentity(allocator: std.mem.Allocator) !Keypair { } } -pub fn runGossipService(gossip_port: u16, entrypoints: ArrayList(LegacyContactInfo)) !void { +pub fn runGossipService(gossip_port: u16, entrypoints: ArrayList(LegacyContactInfo), logger: *Logger) !void { var exit = AtomicBool.init(false); var gossip_socket_addr = SocketAddr.init_ipv4(.{ 0, 0, 0, 0 }, gossip_port); - var spy = try ClusterInfo.initSpy(gpa_allocator, gossip_socket_addr, entrypoints); + var spy = try ClusterInfo.initSpy(gpa_allocator, gossip_socket_addr, entrypoints, logger); var gossip_service = GossipService.init(gpa_allocator, &spy.cluster_info, spy.gossip_socket, exit); - var handle = try std.Thread.spawn(.{}, GossipService.run, .{&gossip_service}); + var handle = try std.Thread.spawn(.{}, GossipService.run, .{ &gossip_service, logger }); handle.join(); } diff --git a/src/gossip/gossip_service.zig b/src/gossip/gossip_service.zig index d6b00aa73..b390c872c 100644 --- a/src/gossip/gossip_service.zig +++ b/src/gossip/gossip_service.zig @@ -14,6 +14,7 @@ const Protocol = @import("protocol.zig").Protocol; const Ping = @import("protocol.zig").Ping; const bincode = @import("bincode-zig"); const crds = @import("../gossip/crds.zig"); +const Logger = @import("../trace/log.zig").Logger; var gpa_allocator = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = gpa_allocator.allocator(); @@ -21,8 +22,6 @@ var gpa = gpa_allocator.allocator(); const PacketChannel = Channel(Packet); // const ProtocolChannel = Channel(Protocol); -const logger = std.log.scoped(.gossip_service); - pub const GossipService = struct { cluster_info: *ClusterInfo, gossip_socket: UdpSocket, @@ -55,17 +54,14 @@ pub const GossipService = struct { self.responder_channel.deinit(); } - pub fn run(self: *Self) !void { - logger.info("running gossip service at {any}", .{self.gossip_socket.getLocalEndPoint()}); + pub fn run(self: *Self, logger: *Logger) !void { + logger.infof("running gossip service at {any}", .{self.gossip_socket.getLocalEndPoint()}); defer self.deinit(); // spawn gossip udp receiver thread - var receiver_handle = try Thread.spawn(.{}, Self.read_gossip_socket, .{self}); - var packet_handle = try Thread.spawn(.{}, Self.process_packets, .{ - self, - gpa, - }); + var receiver_handle = try Thread.spawn(.{}, Self.read_gossip_socket, .{ self, logger }); + var packet_handle = try Thread.spawn(.{}, Self.process_packets, .{ self, gpa, logger }); var responder_handle = try Thread.spawn(.{}, Self.responder, .{self}); var gossip_loop_handle = try Thread.spawn(.{}, Self.gossip_loop, .{self}); @@ -140,7 +136,7 @@ pub const GossipService = struct { self.responder_channel.send(packet); } - fn read_gossip_socket(self: *Self) !void { + fn read_gossip_socket(self: *Self, logger: *Logger) !void { // we close the chan if no more packet's can ever be produced defer self.packet_channel.close(); @@ -160,10 +156,10 @@ pub const GossipService = struct { @memset(&read_buf, 0); } - logger.debug("reading gossip exiting...", .{}); + logger.debugf("reading gossip exiting...", .{}); } - pub fn process_packets(self: *Self, allocator: std.mem.Allocator) !void { + pub fn process_packets(self: *Self, allocator: std.mem.Allocator, logger: *Logger) !void { while (self.packet_channel.receive()) |p| { // note: to recieve PONG messages (from a local spy node) from a PING // you need to modify: streamer/src/socket.rs @@ -172,7 +168,7 @@ pub const GossipService = struct { // } var protocol_message = try bincode.readFromSlice(allocator, Protocol, p.data[0..p.size], bincode.Params.standard); - logger.debug("got a protocol message: {any}", .{protocol_message}); + logger.debugf("got a protocol message: {any}", .{protocol_message}); } } }; diff --git a/src/trace/entry.zig b/src/trace/entry.zig index 89a44e3bf..6bd10f789 100644 --- a/src/trace/entry.zig +++ b/src/trace/entry.zig @@ -44,7 +44,7 @@ pub const Entry = struct { return self; } - pub fn info(self: *Self, comptime fmt: []const u8, args: anytype) void { + pub fn infof(self: *Self, comptime fmt: []const u8, args: anytype) void { var message = std.ArrayList(u8).initCapacity(self.allocator, fmt.len * 2) catch @panic("could not initCapacity for message"); std.fmt.format(message.writer(), fmt, args) catch @panic("could not format"); self.message = message; @@ -53,7 +53,7 @@ pub const Entry = struct { self.logger.appendEntry(self); } - pub fn debug(self: *Self, comptime fmt: []const u8, args: anytype) void { + pub fn debugf(self: *Self, comptime fmt: []const u8, args: anytype) void { var message = std.ArrayList(u8).initCapacity(self.allocator, fmt.len * 2) catch @panic("could not initCapacity for message"); std.fmt.format(message.writer(), fmt, args) catch @panic("could not format"); self.message = message; @@ -62,7 +62,7 @@ pub const Entry = struct { self.logger.appendEntry(self); } - pub fn err(self: *Self, comptime fmt: []const u8, args: anytype) void { + pub fn errf(self: *Self, comptime fmt: []const u8, args: anytype) void { var message = std.ArrayList(u8).initCapacity(self.allocator, fmt.len * 2) catch @panic("could not initCapacity for message"); std.fmt.format(message.writer(), fmt, args) catch @panic("could not format"); self.message = message; @@ -71,7 +71,7 @@ pub const Entry = struct { self.logger.appendEntry(self); } - pub fn warn(self: *Self, comptime fmt: []const u8, args: anytype) void { + pub fn warnf(self: *Self, comptime fmt: []const u8, args: anytype) void { var message = std.ArrayList(u8).initCapacity(self.allocator, fmt.len * 2) catch @panic("could not initCapacity for message"); std.fmt.format(message.writer(), fmt, args) catch @panic("could not format"); self.message = message; @@ -80,6 +80,42 @@ pub const Entry = struct { self.logger.appendEntry(self); } + pub fn info(self: *Self, comptime msg: []const u8) void { + var message = std.ArrayList(u8).initCapacity(self.allocator, msg.len) catch @panic("could not initCapacity for message"); + message.appendSlice(msg[0..]) catch @panic("could not appendSlice for message"); + self.message = message; + self.time = time.DateTime.now(); + self.level = .info; + self.logger.appendEntry(self); + } + + pub fn debug(self: *Self, comptime msg: []const u8) void { + var message = std.ArrayList(u8).initCapacity(self.allocator, msg.len) catch @panic("could not initCapacity for message"); + message.appendSlice(msg[0..]) catch @panic("could not appendSlice for message"); + self.message = message; + self.time = time.DateTime.now(); + self.level = .debug; + self.logger.appendEntry(self); + } + + pub fn err(self: *Self, comptime msg: []const u8) void { + var message = std.ArrayList(u8).initCapacity(self.allocator, msg.len) catch @panic("could not initCapacity for message"); + message.appendSlice(msg[0..]) catch @panic("could not appendSlice for message"); + self.message = message; + self.time = time.DateTime.now(); + self.level = .err; + self.logger.appendEntry(self); + } + + pub fn warn(self: *Self, comptime msg: []const u8) void { + var message = std.ArrayList(u8).initCapacity(self.allocator, msg.len) catch @panic("could not initCapacity for message"); + message.appendSlice(msg[0..]) catch @panic("could not appendSlice for message"); + self.message = message; + self.time = time.DateTime.now(); + self.level = .warn; + self.logger.appendEntry(self); + } + pub fn format( self: *const Self, _: []const u8, @@ -112,7 +148,7 @@ test "trace.entry: should info log correctly" { .field("name", "a-mod") .field("elapsed", @as(i48, 135133340042)) .field("possible_value", anull) - .info("hello, {s}", .{"world!"}); + .infof("hello, {s}", .{"world!"}); std.debug.print("{any}\n\n", .{logger}); } diff --git a/src/trace/log.zig b/src/trace/log.zig index 8b8f21565..17c25cc35 100644 --- a/src/trace/log.zig +++ b/src/trace/log.zig @@ -39,7 +39,7 @@ pub const Logger = struct { } fn run(self: *Self) void { - var stdErrConsumer = BasicSink(std.io.getStdErr().writer()){}; + var stdErrConsumer = BasicStdErrSink{}; var runs: u8 = 0; while (runs < 5) : (runs += 1) { self.mux.lock(); @@ -61,19 +61,35 @@ pub const Logger = struct { return Entry.init(self.allocator, self).field(name, value); } - pub fn info(self: *Self, comptime fmt: []const u8, args: anytype) void { - Entry.init(self.allocator, self).info(fmt, args); + pub fn info(self: *Self, comptime msg: []const u8) void { + Entry.init(self.allocator, self).infof(msg, .{}); } - pub fn debug(self: *Self, comptime fmt: []const u8, args: anytype) void { - Entry.init(self.allocator, self).debug(fmt, args); + pub fn debug(self: *Self, comptime msg: []const u8) void { + Entry.init(self.allocator, self).debugf(msg, .{}); } - pub fn warn(self: *Self, comptime fmt: []const u8, args: anytype) void { + pub fn warn(self: *Self, comptime msg: []const u8) void { + Entry.init(self.allocator, self).warn(msg, .{}); + } + + pub fn err(self: *Self, comptime msg: []const u8) void { + Entry.init(self.allocator, self).err(msg, .{}); + } + + pub fn infof(self: *Self, comptime fmt: []const u8, args: anytype) void { + Entry.init(self.allocator, self).infof(fmt, args); + } + + pub fn debugf(self: *Self, comptime fmt: []const u8, args: anytype) void { + Entry.init(self.allocator, self).debugf(fmt, args); + } + + pub fn warnf(self: *Self, comptime fmt: []const u8, args: anytype) void { Entry.init(self.allocator, self).warn(fmt, args); } - pub fn err(self: *Self, comptime fmt: []const u8, args: anytype) void { + pub fn errf(self: *Self, comptime fmt: []const u8, args: anytype) void { Entry.init(self.allocator, self).err(fmt, args); } @@ -84,23 +100,29 @@ pub const Logger = struct { } }; -fn BasicSink(comptime writer: anytype) type { - return struct { - const Self = @This(); +const BasicStdErrSink = struct { + const Self = @This(); - pub fn consumeEntries(self: Self, entries: []*Entry) void { - _ = self; - for (entries) |e| { - logfmt.formatter(e, writer) catch unreachable; - } - } + pub fn consumeEntries(_: Self, entries: []*Entry) void { + var std_err_writer = std.io.getStdErr().writer(); + var std_err_mux = std.debug.getStderrMutex(); + std_err_mux.lock(); + defer std_err_mux.unlock(); - pub fn consumeEntry(self: Self, e: *Entry) void { - _ = self; - logfmt.formatter(e, writer) catch unreachable; + for (entries) |e| { + logfmt.formatter(e, std_err_writer) catch unreachable; } - }; -} + } + + pub fn consumeEntry(_: Self, e: *Entry) void { + var std_err_writer = std.io.getStdErr().writer(); + var std_err_mux = std.debug.getStderrMutex(); + std_err_mux.lock(); + defer std_err_mux.unlock(); + + logfmt.formatter(e, std_err_writer) catch unreachable; + } +}; test "trace.logger: works" { var logger = Logger.init(testing.allocator, .info); @@ -108,19 +130,19 @@ test "trace.logger: works" { logger.spawn(); - logger.field("elapsed", 4245).debug("request with id {s} succeeded", .{"abcd1234"}); - logger.field("kind", .some_enum_kind).info("operation was done", .{}); - logger.field("authorized", false).warn("api call received at {d} not authorized", .{10004}); - logger.field("error", "IOError").err("api call received at {d} broke the system!", .{10005}); + logger.field("elapsed", 4245).debugf("request with id {s} succeeded", .{"abcd1234"}); + logger.field("kind", .some_enum_kind).infof("operation was done", .{}); + logger.field("authorized", false).warnf("api call received at {d} not authorized", .{10004}); + logger.field("error", "IOError").errf("api call received at {d} broke the system!", .{10005}); std.time.sleep(std.time.ns_per_ms * 100); try testing.expect(logger.pending_entries.items.len == 0); - logger.field("elapsed", 4245).debug("request with id {s} succeeded", .{"abcd1234"}); - logger.field("kind", .some_enum_kind).info("operation was done", .{}); - logger.field("authorized", false).warn("api call received at {d} not authorized", .{10004}); - logger.field("error", "IOError").err("api call received at {d} broke the system!", .{10005}); + logger.field("elapsed", 4245).debug("request with id succeeded"); + logger.field("kind", .some_enum_kind).info("operation was done"); + logger.field("authorized", false).warn("api call received at not authorized"); + logger.field("error", "IOError").err("api call received broke the system!"); std.time.sleep(std.time.ns_per_ms * 100); }