commit 72058e38cf07d3ecc9f70d2e7b5588a15676c8b0
parent e8d28ba3da6df2fecd13869045678b4e4a0cc3ff
Author: Jose Lombera <jose@lombera.dev>
Date: Wed, 7 Sep 2022 01:08:26 -0500
log: add default logger
Add reference to default logger that doesn't change after updating
global logger with log::setlogger(). This allows user to restore global
logger to default value.
Before:
log::setlogger(log::silent);
log::print("NOT LOGGED");
// At this point log::global == log::silent,
// thus next line has no effect.
log::setlogger(log::global);
log::print("NOT LOGGED EITHER");
After:
log::setlogger(log::silent);
log::print("NOT LOGGED");
log:setlogger(log::default);
log::print("LOGGED");
Also, make pointer to global logger private, effectively making it an
implementation detail.
Signed-off-by: Jose Lombera <jose@lombera.dev>
Diffstat:
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/log/global.ha b/log/global.ha
@@ -1,10 +1,13 @@
use io;
use os;
-// The default global logger instance.
-export let global: *logger = &_global;
+// The global logger instance.
+let global: *logger = &_default;
-let _global: stdlogger = stdlogger {
+// Default logger that writes to [[os::stderr]].
+export let default: *logger = &_default;
+
+let _default: stdlogger = stdlogger {
println = &log_println,
printfln = &log_printfln,
sink = -1: io::file,
@@ -12,7 +15,7 @@ let _global: stdlogger = stdlogger {
@init fn init() void = {
// XXX: Would be nice not to have to do this
- _global.sink = os::stderr;
+ _default.sink = os::stderr;
};
// Sets the global logger instance to the provided logger.