From 1fe39007c580e4f29bbb50696f27a92caaf3329d Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Tue, 27 Aug 2024 11:30:23 -0700 Subject: [PATCH] add logging control useful for build scripts --- src/aws.zig | 64 ++++++++++++++++++++++++++++++++++++++++++- src/aws_http.zig | 52 ++++++++++++++++++++++++++++++++++- src/aws_http_base.zig | 2 +- src/aws_signing.zig | 51 +++++++++++++++++++++++++++++++++- 4 files changed, 165 insertions(+), 4 deletions(-) diff --git a/src/aws.zig b/src/aws.zig index f126e62..6b1b556 100644 --- a/src/aws.zig +++ b/src/aws.zig @@ -9,7 +9,69 @@ const date = @import("date.zig"); const servicemodel = @import("servicemodel.zig"); const xml_shaper = @import("xml_shaper.zig"); -const log = std.log.scoped(.aws); +const scoped_log = std.log.scoped(.aws); + +/// control all logs directly/indirectly used by aws sdk. Not recommended for +/// use under normal circumstances, but helpful for times when the zig logging +/// controls are insufficient (e.g. use in build script) +pub fn globalLogControl(aws_level: std.log.Level, http_level: std.log.Level, signing_level: std.log.Level, off: bool) void { + const signing = @import("aws_signing.zig"); + logs_off = off; + signing.logs_off = off; + awshttp.logs_off = off; + log_level = aws_level; + awshttp.log_level = http_level; + signing.log_level = signing_level; +} +/// Specifies logging level. This should not be touched unless the normal +/// zig logging capabilities are inaccessible (e.g. during a build) +pub var log_level: std.log.Level = .debug; + +/// Turn off logging completely +pub var logs_off: bool = false; +const log = struct { + /// Log an error message. This log level is intended to be used + /// when something has gone wrong. This might be recoverable or might + /// be followed by the program exiting. + pub fn err( + comptime format: []const u8, + args: anytype, + ) void { + if (!logs_off and @intFromEnum(std.log.Level.err) <= @intFromEnum(log_level)) + scoped_log.err(format, args); + } + + /// Log a warning message. This log level is intended to be used if + /// it is uncertain whether something has gone wrong or not, but the + /// circumstances would be worth investigating. + pub fn warn( + comptime format: []const u8, + args: anytype, + ) void { + if (!logs_off and @intFromEnum(std.log.Level.warn) <= @intFromEnum(log_level)) + scoped_log.warn(format, args); + } + + /// Log an info message. This log level is intended to be used for + /// general messages about the state of the program. + pub fn info( + comptime format: []const u8, + args: anytype, + ) void { + if (!logs_off and @intFromEnum(std.log.Level.info) <= @intFromEnum(log_level)) + scoped_log.info(format, args); + } + + /// Log a debug message. This log level is intended to be used for + /// messages which are only useful for debugging. + pub fn debug( + comptime format: []const u8, + args: anytype, + ) void { + if (!logs_off and @intFromEnum(std.log.Level.debug) <= @intFromEnum(log_level)) + scoped_log.debug(format, args); + } +}; pub const Options = struct { region: []const u8 = "aws-global", diff --git a/src/aws_http.zig b/src/aws_http.zig index e682926..aae7bcb 100644 --- a/src/aws_http.zig +++ b/src/aws_http.zig @@ -17,7 +17,57 @@ const CN_NORTHWEST_1_HASH = std.hash_map.hashString("cn-northwest-1"); const US_ISO_EAST_1_HASH = std.hash_map.hashString("us-iso-east-1"); const US_ISOB_EAST_1_HASH = std.hash_map.hashString("us-isob-east-1"); -const log = std.log.scoped(.awshttp); +const scoped_log = std.log.scoped(.awshttp); + +/// Specifies logging level. This should not be touched unless the normal +/// zig logging capabilities are inaccessible (e.g. during a build) +pub var log_level: std.log.Level = .debug; + +/// Turn off logging completely +pub var logs_off: bool = false; +const log = struct { + /// Log an error message. This log level is intended to be used + /// when something has gone wrong. This might be recoverable or might + /// be followed by the program exiting. + pub fn err( + comptime format: []const u8, + args: anytype, + ) void { + if (!logs_off and @intFromEnum(std.log.Level.err) <= @intFromEnum(log_level)) + scoped_log.err(format, args); + } + + /// Log a warning message. This log level is intended to be used if + /// it is uncertain whether something has gone wrong or not, but the + /// circumstances would be worth investigating. + pub fn warn( + comptime format: []const u8, + args: anytype, + ) void { + if (!logs_off and @intFromEnum(std.log.Level.warn) <= @intFromEnum(log_level)) + scoped_log.warn(format, args); + } + + /// Log an info message. This log level is intended to be used for + /// general messages about the state of the program. + pub fn info( + comptime format: []const u8, + args: anytype, + ) void { + if (!logs_off and @intFromEnum(std.log.Level.info) <= @intFromEnum(log_level)) + scoped_log.info(format, args); + } + + /// Log a debug message. This log level is intended to be used for + /// messages which are only useful for debugging. + pub fn debug( + comptime format: []const u8, + args: anytype, + ) void { + if (!logs_off and @intFromEnum(std.log.Level.debug) <= @intFromEnum(log_level)) + scoped_log.debug(format, args); + } +}; pub const AwsError = error{ AddHeaderError, diff --git a/src/aws_http_base.zig b/src/aws_http_base.zig index eb05a59..f624c3a 100644 --- a/src/aws_http_base.zig +++ b/src/aws_http_base.zig @@ -22,7 +22,7 @@ pub const Result = struct { self.allocator.free(h.value); } self.allocator.free(self.headers); - log.debug("http result deinit complete", .{}); + //log.debug("http result deinit complete", .{}); return; } }; diff --git a/src/aws_signing.zig b/src/aws_signing.zig index 925f1b6..4d3ffa7 100644 --- a/src/aws_signing.zig +++ b/src/aws_signing.zig @@ -3,8 +3,57 @@ const base = @import("aws_http_base.zig"); const auth = @import("aws_authentication.zig"); const date = @import("date.zig"); -const log = std.log.scoped(.aws_signing); +const scoped_log = std.log.scoped(.aws_signing); +/// Specifies logging level. This should not be touched unless the normal +/// zig logging capabilities are inaccessible (e.g. during a build) +pub var log_level: std.log.Level = .debug; + +/// Turn off logging completely +pub var logs_off: bool = false; +const log = struct { + /// Log an error message. This log level is intended to be used + /// when something has gone wrong. This might be recoverable or might + /// be followed by the program exiting. + pub fn err( + comptime format: []const u8, + args: anytype, + ) void { + if (!logs_off and @intFromEnum(std.log.Level.err) <= @intFromEnum(log_level)) + scoped_log.err(format, args); + } + + /// Log a warning message. This log level is intended to be used if + /// it is uncertain whether something has gone wrong or not, but the + /// circumstances would be worth investigating. + pub fn warn( + comptime format: []const u8, + args: anytype, + ) void { + if (!logs_off and @intFromEnum(std.log.Level.warn) <= @intFromEnum(log_level)) + scoped_log.warn(format, args); + } + + /// Log an info message. This log level is intended to be used for + /// general messages about the state of the program. + pub fn info( + comptime format: []const u8, + args: anytype, + ) void { + if (!logs_off and @intFromEnum(std.log.Level.info) <= @intFromEnum(log_level)) + scoped_log.info(format, args); + } + + /// Log a debug message. This log level is intended to be used for + /// messages which are only useful for debugging. + pub fn debug( + comptime format: []const u8, + args: anytype, + ) void { + if (!logs_off and @intFromEnum(std.log.Level.debug) <= @intFromEnum(log_level)) + scoped_log.debug(format, args); + } +}; // TODO: Remove this?! This is an aws_signing, so we should know a thing // or two about aws. So perhaps the right level of abstraction here // is to have our service signing idiosyncracies dealt with in this