aboutsummaryrefslogtreecommitdiff
path: root/lib/std/log.zig
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2023-01-03 19:37:11 +0200
committerAndrew Kelley <andrew@ziglang.org>2023-01-05 02:31:29 -0700
commitf83834993e2628e347da71a11ffb07c804fc46c5 (patch)
tree21fd10b3ba19c4236326549872a202a4ffe29b3f /lib/std/log.zig
parentfe2bd9dda8467b775da4fe3bd535aece9e07ee1b (diff)
downloadzig-f83834993e2628e347da71a11ffb07c804fc46c5.tar.gz
zig-f83834993e2628e347da71a11ffb07c804fc46c5.zip
std: collect all options under one namespace
Diffstat (limited to 'lib/std/log.zig')
-rw-r--r--lib/std/log.zig41
1 files changed, 15 insertions, 26 deletions
diff --git a/lib/std/log.zig b/lib/std/log.zig
index 1d1b514c03..48a287a4ee 100644
--- a/lib/std/log.zig
+++ b/lib/std/log.zig
@@ -1,6 +1,6 @@
//! std.log is a standardized interface for logging which allows for the logging
//! of programs and libraries using this interface to be formatted and filtered
-//! by the implementer of the root.log function.
+//! by the implementer of the `std.options.logFn` function.
//!
//! Each log message has an associated scope enum, which can be used to give
//! context to the logging. The logging functions in std.log implicitly use a
@@ -13,16 +13,20 @@
//! `const log = std.log.scoped(.libfoo);` to use .libfoo as the scope of its
//! log messages.
//!
-//! An example root.log might look something like this:
+//! An example `logFn` might look something like this:
//!
//! ```
//! const std = @import("std");
//!
-//! // Set the log level to info
-//! pub const log_level: std.log.Level = .info;
+//! pub const std_options = struct {
+//! // Set the log level to info
+//! pub const log_level = .info;
//!
-//! // Define root.log to override the std implementation
-//! pub fn log(
+//! // Define logFn to override the std implementation
+//! pub const logFn = myLogFn;
+//! };
+//!
+//! pub fn myLogFn(
//! comptime level: std.log.Level,
//! comptime scope: @TypeOf(.EnumLiteral),
//! comptime format: []const u8,
@@ -70,7 +74,6 @@
const std = @import("std.zig");
const builtin = @import("builtin");
-const root = @import("root");
pub const Level = enum {
/// Error: something has gone wrong. This might be recoverable or might
@@ -102,22 +105,14 @@ pub const default_level: Level = switch (builtin.mode) {
.ReleaseFast, .ReleaseSmall => .err,
};
-/// The current log level. This is set to root.log_level if present, otherwise
-/// log.default_level.
-pub const level: Level = if (@hasDecl(root, "log_level"))
- root.log_level
-else
- default_level;
+const level = std.options.log_level;
pub const ScopeLevel = struct {
scope: @Type(.EnumLiteral),
level: Level,
};
-const scope_levels = if (@hasDecl(root, "scope_levels"))
- root.scope_levels
-else
- [0]ScopeLevel{};
+const scope_levels = std.options.log_scope_levels;
fn log(
comptime message_level: Level,
@@ -127,13 +122,7 @@ fn log(
) void {
if (comptime !logEnabled(message_level, scope)) return;
- if (@hasDecl(root, "log")) {
- if (@typeInfo(@TypeOf(root.log)) != .Fn)
- @compileError("Expected root.log to be a function");
- root.log(message_level, scope, format, args);
- } else {
- defaultLog(message_level, scope, format, args);
- }
+ std.options.logFn(message_level, scope, format, args);
}
/// Determine if a specific log message level and scope combination are enabled for logging.
@@ -149,8 +138,8 @@ pub fn defaultLogEnabled(comptime message_level: Level) bool {
return comptime logEnabled(message_level, default_log_scope);
}
-/// The default implementation for root.log. root.log may forward log messages
-/// to this function.
+/// The default implementation for the log function, custom log functions may
+/// forward log messages to this function.
pub fn defaultLog(
comptime message_level: Level,
comptime scope: @Type(.EnumLiteral),