You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature Request: downcasting Log, but for real #666
Seems #399 was already closed (4.5 years ago, for bad usecase & maybe backwards compatibility reasons?), but I have an actual use!
My logger writes a message on panic, but does so with a custom format different to anything you can write with the Log trait. So I need to be able to downcast the &dyn Log into my own logger from the panic handler. I could probably use a OnceLock to store if my logger's been initialized but log already does that with the (set_)logger methods, so it feels redundant.
For now my current way of doing this is this very cursed set of functions:
fnas_dyn_ref(logger:*constLogger) -> *constdynLog{// split into one function to always attach the same metadata
logger as*constdynLog}fnupcast_log(logger:&'staticLogger) -> &'staticdynLog{// SAFETY: as_dyn_ref returns a reference to the same object as passed inunsafe{&*as_dyn_ref(logger)}}fndowncast_log(log:&'staticdynLog) -> Option<&'staticLogger>{// horribly cursed implementation to fetch a reference to the installed loggerlet(logger_ptr, logger_meta) = (&rawconst*log).to_raw_parts();let(_, fake_logger_meta) = as_dyn_ref(ptr::null::<Logger>()).to_raw_parts();(logger_meta == fake_logger_meta).then(|| {// SAFETY: v-tables match so it's probably ours!unsafe{&*logger_ptr.cast::<Logger>()}})}
but that's a lot of unsafe, and nightly-only.
To prevent needing to add a + 'static bound on Log (which probably breaks compatibility somehow), yandros suggested (in rplcs #dark-arts) adding something like:
This is something I'd be open to exploring a bit more. We'd need to be careful adding a type_id method to the Log trait, because it would be unsound to override. The Error trait for instance defines its squirrel-type-id method like this to try get around that
Ah yeah that makes sense, in which case I'm not quite sure how one would implement that without some sort of + 'static or + Any bound on the Log trait itself (although I'm curious on how much real code that would actually break, given that the library only ever asks for &'static dyn Logs).
I am the author of indicatif-log-bridge and i would like the same feature, downcasting the &'static dyn Log to LogWrapper (the specific logger) to access the interal MultiProgress.
i might be misinformed but i think this would be as simple as adding a simple function with a default implementation to Log
Activity
KodrAus commentedon May 14, 2025
Hi @1e1001 👋
This is something I'd be open to exploring a bit more. We'd need to be careful adding a
type_idmethod to theLogtrait, because it would be unsound to override. TheErrortrait for instance defines its squirrel-type-id method like this to try get around that1e1001 commentedon May 21, 2025
Ah yeah that makes sense, in which case I'm not quite sure how one would implement that without some sort of
+ 'staticor+ Anybound on theLogtrait itself (although I'm curious on how much real code that would actually break, given that the library only ever asks for&'static dyn Logs).djugei commentedon Sep 19, 2026
I am the author of indicatif-log-bridge and i would like the same feature, downcasting the
&'static dyn Logto LogWrapper (the specific logger) to access the interalMultiProgress.i might be misinformed but i think this would be as simple as adding a simple function with a default implementation to Log
due to the default implementation it would be backwards compatible.