2020-08-12 14:37:56 +01:00
|
|
|
//! std.log is a standardized interface for logging which allows for the logging
|
2020-05-15 16:10:56 +01:00
|
|
|
//! of programs and libraries using this interface to be formatted and filtered
|
2023-01-03 17:37:11 +00:00
|
|
|
//! by the implementer of the `std.options.logFn` function.
|
2020-05-15 16:10:56 +01:00
|
|
|
//!
|
2020-08-13 16:12:16 +01:00
|
|
|
//! 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
|
|
|
|
//! scope of .default.
|
|
|
|
//!
|
|
|
|
//! A logging namespace using a custom scope can be created using the
|
|
|
|
//! std.log.scoped function, passing the scope as an argument; the logging
|
|
|
|
//! functions in the resulting struct use the provided scope parameter.
|
|
|
|
//! For example, a library called 'libfoo' might use
|
|
|
|
//! `const log = std.log.scoped(.libfoo);` to use .libfoo as the scope of its
|
|
|
|
//! log messages.
|
2020-05-15 16:10:56 +01:00
|
|
|
//!
|
2023-01-03 17:37:11 +00:00
|
|
|
//! An example `logFn` might look something like this:
|
2020-05-15 16:10:56 +01:00
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! const std = @import("std");
|
|
|
|
//!
|
2023-01-03 17:37:11 +00:00
|
|
|
//! pub const std_options = struct {
|
|
|
|
//! // Set the log level to info
|
|
|
|
//! pub const log_level = .info;
|
2020-05-15 16:10:56 +01:00
|
|
|
//!
|
2023-01-03 17:37:11 +00:00
|
|
|
//! // Define logFn to override the std implementation
|
|
|
|
//! pub const logFn = myLogFn;
|
|
|
|
//! };
|
|
|
|
//!
|
|
|
|
//! pub fn myLogFn(
|
2020-05-15 16:10:56 +01:00
|
|
|
//! comptime level: std.log.Level,
|
|
|
|
//! comptime scope: @TypeOf(.EnumLiteral),
|
|
|
|
//! comptime format: []const u8,
|
2020-07-11 20:04:38 +01:00
|
|
|
//! args: anytype,
|
2020-05-15 16:10:56 +01:00
|
|
|
//! ) void {
|
2021-10-24 12:13:06 +01:00
|
|
|
//! // Ignore all non-error logging from sources other than
|
2022-10-26 21:03:02 +01:00
|
|
|
//! // .my_project, .nice_library and the default
|
2020-05-15 16:10:56 +01:00
|
|
|
//! const scope_prefix = "(" ++ switch (scope) {
|
2022-10-26 21:03:02 +01:00
|
|
|
//! .my_project, .nice_library, std.log.default_log_scope => @tagName(scope),
|
2023-06-15 08:14:16 +01:00
|
|
|
//! else => if (@intFromEnum(level) <= @intFromEnum(std.log.Level.err))
|
2020-05-15 16:10:56 +01:00
|
|
|
//! @tagName(scope)
|
|
|
|
//! else
|
|
|
|
//! return,
|
|
|
|
//! } ++ "): ";
|
|
|
|
//!
|
2022-08-30 22:07:23 +01:00
|
|
|
//! const prefix = "[" ++ comptime level.asText() ++ "] " ++ scope_prefix;
|
2020-05-15 16:10:56 +01:00
|
|
|
//!
|
|
|
|
//! // Print the message to stderr, silently ignoring any errors
|
2021-11-10 01:27:12 +00:00
|
|
|
//! std.debug.getStderrMutex().lock();
|
|
|
|
//! defer std.debug.getStderrMutex().unlock();
|
2020-07-21 09:47:30 +01:00
|
|
|
//! const stderr = std.io.getStdErr().writer();
|
2020-07-22 22:26:41 +01:00
|
|
|
//! nosuspend stderr.print(prefix ++ format ++ "\n", args) catch return;
|
2020-05-15 16:10:56 +01:00
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! pub fn main() void {
|
2020-08-13 16:12:16 +01:00
|
|
|
//! // Using the default scope:
|
2021-10-24 12:13:06 +01:00
|
|
|
//! std.log.debug("A borderline useless debug log message", .{}); // Won't be printed as log_level is .info
|
|
|
|
//! std.log.info("Flux capacitor is starting to overheat", .{});
|
2020-08-12 14:37:56 +01:00
|
|
|
//!
|
2020-08-13 16:12:16 +01:00
|
|
|
//! // Using scoped logging:
|
|
|
|
//! const my_project_log = std.log.scoped(.my_project);
|
|
|
|
//! const nice_library_log = std.log.scoped(.nice_library);
|
|
|
|
//! const verbose_lib_log = std.log.scoped(.verbose_lib);
|
2020-08-12 14:37:56 +01:00
|
|
|
//!
|
2021-10-24 12:13:06 +01:00
|
|
|
//! my_project_log.debug("Starting up", .{}); // Won't be printed as log_level is .info
|
|
|
|
//! nice_library_log.warn("Something went very wrong, sorry", .{});
|
|
|
|
//! verbose_lib_log.warn("Added 1 + 1: {}", .{1 + 1}); // Won't be printed as it gets filtered out by our log function
|
2020-05-15 16:10:56 +01:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//! Which produces the following output:
|
|
|
|
//! ```
|
2021-10-24 12:13:06 +01:00
|
|
|
//! [info] (default): Flux capacitor is starting to overheat
|
|
|
|
//! [warning] (nice_library): Something went very wrong, sorry
|
2020-05-15 16:10:56 +01:00
|
|
|
//! ```
|
|
|
|
|
2021-02-25 04:29:01 +00:00
|
|
|
const std = @import("std.zig");
|
2021-10-05 07:47:27 +01:00
|
|
|
const builtin = @import("builtin");
|
2021-02-25 04:29:01 +00:00
|
|
|
|
2020-05-15 16:10:56 +01:00
|
|
|
pub const Level = enum {
|
2021-10-24 12:13:06 +01:00
|
|
|
/// Error: something has gone wrong. This might be recoverable or might
|
|
|
|
/// be followed by the program exiting.
|
2020-05-15 16:10:56 +01:00
|
|
|
err,
|
|
|
|
/// Warning: it is uncertain if something has gone wrong or not, but the
|
|
|
|
/// circumstances would be worth investigating.
|
|
|
|
warn,
|
2021-10-24 12:13:06 +01:00
|
|
|
/// Info: general messages about the state of the program.
|
2020-05-15 16:10:56 +01:00
|
|
|
info,
|
|
|
|
/// Debug: messages only useful for debugging.
|
|
|
|
debug,
|
2021-06-24 11:30:05 +01:00
|
|
|
|
|
|
|
/// Returns a string literal of the given level in full text form.
|
2022-07-06 18:27:47 +01:00
|
|
|
pub fn asText(comptime self: Level) []const u8 {
|
2021-06-24 11:30:05 +01:00
|
|
|
return switch (self) {
|
|
|
|
.err => "error",
|
|
|
|
.warn => "warning",
|
2021-10-24 12:13:06 +01:00
|
|
|
.info => "info",
|
|
|
|
.debug => "debug",
|
2021-06-24 11:30:05 +01:00
|
|
|
};
|
|
|
|
}
|
2020-05-15 16:10:56 +01:00
|
|
|
};
|
|
|
|
|
2020-09-27 05:03:38 +01:00
|
|
|
/// The default log level is based on build mode.
|
2020-05-15 16:10:56 +01:00
|
|
|
pub const default_level: Level = switch (builtin.mode) {
|
|
|
|
.Debug => .debug,
|
2021-10-24 12:13:06 +01:00
|
|
|
.ReleaseSafe => .info,
|
|
|
|
.ReleaseFast, .ReleaseSmall => .err,
|
2020-05-15 16:10:56 +01:00
|
|
|
};
|
|
|
|
|
2023-01-03 17:37:11 +00:00
|
|
|
const level = std.options.log_level;
|
2020-05-15 16:10:56 +01:00
|
|
|
|
2021-06-09 10:23:45 +01:00
|
|
|
pub const ScopeLevel = struct {
|
|
|
|
scope: @Type(.EnumLiteral),
|
|
|
|
level: Level,
|
|
|
|
};
|
|
|
|
|
2023-01-03 17:37:11 +00:00
|
|
|
const scope_levels = std.options.log_scope_levels;
|
2021-06-09 10:23:45 +01:00
|
|
|
|
2020-05-15 16:10:56 +01:00
|
|
|
fn log(
|
|
|
|
comptime message_level: Level,
|
|
|
|
comptime scope: @Type(.EnumLiteral),
|
|
|
|
comptime format: []const u8,
|
2020-07-11 12:09:04 +01:00
|
|
|
args: anytype,
|
2020-05-15 16:10:56 +01:00
|
|
|
) void {
|
2022-10-26 21:03:02 +01:00
|
|
|
if (comptime !logEnabled(message_level, scope)) return;
|
|
|
|
|
2023-01-03 17:37:11 +00:00
|
|
|
std.options.logFn(message_level, scope, format, args);
|
2022-10-26 21:03:02 +01:00
|
|
|
}
|
2021-06-09 10:23:45 +01:00
|
|
|
|
2022-10-26 21:03:02 +01:00
|
|
|
/// Determine if a specific log message level and scope combination are enabled for logging.
|
|
|
|
pub fn logEnabled(comptime message_level: Level, comptime scope: @Type(.EnumLiteral)) bool {
|
|
|
|
inline for (scope_levels) |scope_level| {
|
2023-06-15 08:14:16 +01:00
|
|
|
if (scope_level.scope == scope) return @intFromEnum(message_level) <= @intFromEnum(scope_level.level);
|
2020-05-15 16:10:56 +01:00
|
|
|
}
|
2023-06-15 08:14:16 +01:00
|
|
|
return @intFromEnum(message_level) <= @intFromEnum(level);
|
2022-10-26 21:03:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Determine if a specific log message level using the default log scope is enabled for logging.
|
|
|
|
pub fn defaultLogEnabled(comptime message_level: Level) bool {
|
|
|
|
return comptime logEnabled(message_level, default_log_scope);
|
2020-05-15 16:10:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-03 17:37:11 +00:00
|
|
|
/// The default implementation for the log function, custom log functions may
|
|
|
|
/// forward log messages to this function.
|
2021-06-24 11:11:28 +01:00
|
|
|
pub fn defaultLog(
|
|
|
|
comptime message_level: Level,
|
|
|
|
comptime scope: @Type(.EnumLiteral),
|
|
|
|
comptime format: []const u8,
|
|
|
|
args: anytype,
|
|
|
|
) void {
|
2021-06-24 11:30:05 +01:00
|
|
|
const level_txt = comptime message_level.asText();
|
2021-06-24 11:11:28 +01:00
|
|
|
const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
|
|
|
|
const stderr = std.io.getStdErr().writer();
|
2021-11-10 01:27:12 +00:00
|
|
|
std.debug.getStderrMutex().lock();
|
|
|
|
defer std.debug.getStderrMutex().unlock();
|
2021-06-24 11:11:28 +01:00
|
|
|
nosuspend stderr.print(level_txt ++ prefix2 ++ format ++ "\n", args) catch return;
|
|
|
|
}
|
|
|
|
|
2020-08-12 14:37:56 +01:00
|
|
|
/// Returns a scoped logging namespace that logs all messages using the scope
|
|
|
|
/// provided here.
|
2020-08-12 13:03:02 +01:00
|
|
|
pub fn scoped(comptime scope: @Type(.EnumLiteral)) type {
|
|
|
|
return struct {
|
2021-10-24 12:13:06 +01:00
|
|
|
/// 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.
|
2020-08-12 13:03:02 +01:00
|
|
|
pub fn err(
|
|
|
|
comptime format: []const u8,
|
|
|
|
args: anytype,
|
|
|
|
) void {
|
|
|
|
@setCold(true);
|
|
|
|
log(.err, scope, format, args);
|
|
|
|
}
|
|
|
|
|
2020-08-12 14:54:21 +01:00
|
|
|
/// Log a warning message. This log level is intended to be used if
|
2020-08-12 13:03:02 +01:00
|
|
|
/// 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 {
|
|
|
|
log(.warn, scope, format, args);
|
|
|
|
}
|
|
|
|
|
2020-08-12 14:54:21 +01:00
|
|
|
/// Log an info message. This log level is intended to be used for
|
2020-08-12 13:03:02 +01:00
|
|
|
/// general messages about the state of the program.
|
|
|
|
pub fn info(
|
|
|
|
comptime format: []const u8,
|
|
|
|
args: anytype,
|
|
|
|
) void {
|
|
|
|
log(.info, scope, format, args);
|
|
|
|
}
|
|
|
|
|
2020-08-12 14:54:21 +01:00
|
|
|
/// Log a debug message. This log level is intended to be used for
|
2020-08-12 13:03:02 +01:00
|
|
|
/// messages which are only useful for debugging.
|
|
|
|
pub fn debug(
|
|
|
|
comptime format: []const u8,
|
|
|
|
args: anytype,
|
|
|
|
) void {
|
|
|
|
log(.debug, scope, format, args);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-26 21:03:02 +01:00
|
|
|
pub const default_log_scope = .default;
|
|
|
|
|
2020-08-12 14:37:56 +01:00
|
|
|
/// The default scoped logging namespace.
|
2022-10-26 21:03:02 +01:00
|
|
|
pub const default = scoped(default_log_scope);
|
2020-08-13 15:50:38 +01:00
|
|
|
|
|
|
|
/// Log an error message using the default scope. This log level is intended to
|
2021-10-24 12:13:06 +01:00
|
|
|
/// be used when something has gone wrong. This might be recoverable or might
|
|
|
|
/// be followed by the program exiting.
|
2020-08-13 15:50:38 +01:00
|
|
|
pub const err = default.err;
|
|
|
|
|
|
|
|
/// Log a warning message using the default scope. 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 const warn = default.warn;
|
|
|
|
|
|
|
|
/// Log an info message using the default scope. This log level is intended to
|
|
|
|
/// be used for general messages about the state of the program.
|
|
|
|
pub const info = default.info;
|
|
|
|
|
|
|
|
/// Log a debug message using the default scope. This log level is intended to
|
|
|
|
/// be used for messages which are only useful for debugging.
|
|
|
|
pub const debug = default.debug;
|