Make try_log public so that custom loggers can use it#104
Open
Boscop wants to merge 2 commits intoDrakulix:masterfrom
Open
Make try_log public so that custom loggers can use it#104Boscop wants to merge 2 commits intoDrakulix:masterfrom
Boscop wants to merge 2 commits intoDrakulix:masterfrom
Conversation
Drakulix
requested changes
Jun 5, 2022
Owner
Drakulix
left a comment
There was a problem hiding this comment.
In principle I don't mind to do this, but right now try_log has no documentation.
If you could try to add those (and make sure to explain potential use-cases and reasons this function is public) I would gladly merge this!
Author
|
I'm not sure what kind of doc to add. Like this: pub struct HostLogger {
level: LevelFilter,
config: Config,
}
impl HostLogger {
pub fn new(log_level: LevelFilter, config: Config) -> Box<HostLogger> {
Box::new(HostLogger { level: log_level, config })
}
}
impl Log for HostLogger {
fn enabled(&self, metadata: &Metadata<'_>) -> bool {
metadata.level() <= self.level
}
fn log(&self, record: &Record<'_>) {
if self.enabled(record.metadata()) {
let mut s = Vec::new();
let _ = try_log(&self.config, record, &mut Cursor::new(&mut s));
let s = std::str::from_utf8(&s).expect("utf8").trim();
host_log(s);
}
}
fn flush(&self) {}
}
impl SharedLogger for HostLogger {
fn level(&self) -> LevelFilter {
self.level
}
fn config(&self) -> Option<&Config> {
Some(&self.config)
}
fn as_log(self: Box<Self>) -> Box<dyn Log> {
Box::new(*self)
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
My use case that requires this change is:
From my DLL plugin I want to log to a file but also to the host, by sending it strings (one per log record).
I need to log each line separately to the host, but
Writedoesn't let me know when each record starts/ends.So I need to log to a
String(actuallyCursor<Vec<u8>>, thenstr::from_utf8, then send theStringto the host) and I need to be able to calltry_logfor that.