11//! std.log is a standardized interface for logging which allows for the logging
22//! of programs and libraries using this interface to be formatted and filtered
3- //! by the implementer of the root.log function.
3+ //! by the implementer of the `std.options.logFn` function.
44//!
55//! Each log message has an associated scope enum, which can be used to give
66//! context to the logging. The logging functions in std.log implicitly use a
1313//! `const log = std.log.scoped(.libfoo);` to use .libfoo as the scope of its
1414//! log messages.
1515//!
16- //! An example root.log might look something like this:
16+ //! An example `logFn` might look something like this:
1717//!
1818//! ```
1919//! const std = @import("std");
2020//!
21- //! // Set the log level to info
22- //! pub const log_level: std.log.Level = .info;
21+ //! pub const std_options = struct {
22+ //! // Set the log level to info
23+ //! pub const log_level = .info;
2324//!
24- //! // Define root.log to override the std implementation
25- //! pub fn log(
25+ //! // Define logFn to override the std implementation
26+ //! pub const logFn = myLogFn;
27+ //! };
28+ //!
29+ //! pub fn myLogFn(
2630//! comptime level: std.log.Level,
2731//! comptime scope: @TypeOf(.EnumLiteral),
2832//! comptime format: []const u8,
7074
7175const std = @import ("std.zig" );
7276const builtin = @import ("builtin" );
73- const root = @import ("root" );
7477
7578pub const Level = enum {
7679 /// Error: something has gone wrong. This might be recoverable or might
@@ -102,22 +105,14 @@ pub const default_level: Level = switch (builtin.mode) {
102105 .ReleaseFast , .ReleaseSmall = > .err ,
103106};
104107
105- /// The current log level. This is set to root.log_level if present, otherwise
106- /// log.default_level.
107- pub const level : Level = if (@hasDecl (root , "log_level" ))
108- root .log_level
109- else
110- default_level ;
108+ const level = std .options .log_level ;
111109
112110pub const ScopeLevel = struct {
113111 scope : @Type (.EnumLiteral ),
114112 level : Level ,
115113};
116114
117- const scope_levels = if (@hasDecl (root , "scope_levels" ))
118- root .scope_levels
119- else
120- [0 ]ScopeLevel {};
115+ const scope_levels = std .options .log_scope_levels ;
121116
122117fn log (
123118 comptime message_level : Level ,
@@ -127,13 +122,7 @@ fn log(
127122) void {
128123 if (comptime ! logEnabled (message_level , scope )) return ;
129124
130- if (@hasDecl (root , "log" )) {
131- if (@typeInfo (@TypeOf (root .log )) != .Fn )
132- @compileError ("Expected root.log to be a function" );
133- root .log (message_level , scope , format , args );
134- } else {
135- defaultLog (message_level , scope , format , args );
136- }
125+ std .options .logFn (message_level , scope , format , args );
137126}
138127
139128/// 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 {
149138 return comptime logEnabled (message_level , default_log_scope );
150139}
151140
152- /// The default implementation for root.log. root. log may forward log messages
153- /// to this function.
141+ /// The default implementation for the log function, custom log functions may
142+ /// forward log messages to this function.
154143pub fn defaultLog (
155144 comptime message_level : Level ,
156145 comptime scope : @Type (.EnumLiteral ),
0 commit comments