Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EspLogger is not extensible #476

Closed
gvaradarajan opened this issue Aug 28, 2024 · 7 comments
Closed

EspLogger is not extensible #476

gvaradarajan opened this issue Aug 28, 2024 · 7 comments

Comments

@gvaradarajan
Copy link

I would like to be able to extend the functionality of EspLogger for my own custom purposes like so

struct MyLogger<L>(L);

impl<L> ::log::Log for MyLogger<L>
where
    L: ::log::Log,
{
    fn enabled(&self, metadata: &log::Metadata) -> bool {
        self.0.enabled(metadata)
    }

    fn flush(&self) {
        self.0.flush()
    }

    fn log(&self, record: &log::Record) {
        if self.enabled(record.metadata()) {
            self.0.log(record);
            <do other things with the record...>
        }
    }
}

However as of this commit, I'm no longer able to do so because the public struct now has a private constructor. Could we maybe throw a cheeky "pub" in front of that const fn new line?

@ivmarkov
Copy link
Collaborator

ivmarkov commented Aug 28, 2024

Help me understand how a pub EspLogger::new would help. It is not like Rust is an OOP language, where you can "inherit" from EspLogger, so not sure how that would be useful?

(And btw the new is private because the logger is supposed to be a singleton.)

@gvaradarajan
Copy link
Author

Help me understand how a pub EspLogger::new would help. It is not like Rust is an OOP language, where you can "inherit" from EspLogger, so not sure how that would be useful?

(And btw the new is private because the logger is supposed to be a singleton.)

Rust is not OOP, but composition is still possible. In the example I put above, let's suppose that the <do other things with the record> involves uploading the log to some cloud storage. I could call log::set_boxed_logger(MyLogger(EspLogger::new())) at the start of my code and then all Rust logs would be efficiently redirected to UART as EspLogger is implemented to do and then MyLogger could redirect that log over the internet with its own logic.

I understand the pattern of desiring a singleton, but enforcing that pattern is already handled by the log crate.

@ivmarkov
Copy link
Collaborator

ivmarkov commented Aug 28, 2024

Help me understand how a pub EspLogger::new would help. It is not like Rust is an OOP language, where you can "inherit" from EspLogger, so not sure how that would be useful?
(And btw the new is private because the logger is supposed to be a singleton.)

Rust is not OOP, but composition is still possible. In the example I put above, let's suppose that the <do other things with the record> involves uploading the log to some cloud storage. I could call log::set_boxed_logger(MyLogger(EspLogger::new())) at the start of my code and then all Rust logs would be efficiently redirected to UART as EspLogger is implemented to do and then MyLogger could redirect that log over the internet with its own logic.

I understand the pattern of desiring a singleton, but enforcing that pattern is already handled by the log crate.

OK, so your idea would be:

  • Wrap the esp logger in your own stuff
  • Do NOT call EspLogger::initialize_default, and instead do your own ::log::set_logger(...) dance with the log crate

BTW: You do realize this way you can only grab the logs originating from rust code. Logs originating from the C code you need to grab with a different facility, which is much more hairy (vsprintf and stuff like that).

@gvaradarajan
Copy link
Author

gvaradarajan commented Aug 28, 2024

Yes that's exactly what my plan is. I am aware about those other logs, I have a separate additional use of esp_log_set_vprintf that handles those statements

ivmarkov added a commit that referenced this issue Aug 28, 2024
ivmarkov added a commit that referenced this issue Aug 28, 2024
@ivmarkov
Copy link
Collaborator

Yes that's exactly what my plan is. I am aware about those other logs, I have a separate additional use of the esp_log_set_vprintf that handles those statements

Would be great if you could share your code that deals with the vprintf blackmagic from Rust (if you do it from Rust and with pure rust (i.e. no C stubs and such)). I remember a user was fighting this for a long time without declaring success. We might include such an utility in esp-idf-svc::log as that might be useful to many folks.

@ivmarkov
Copy link
Collaborator

Closing as fixed by 11d9d0a

But if you could really share the vsprintf stuff, that would be helpful.

@github-project-automation github-project-automation bot moved this from Todo to Done in esp-rs Aug 28, 2024
@gvaradarajan
Copy link
Author

gvaradarajan commented Aug 28, 2024

Thank you for hearing me out @ivmarkov! I have an example for using the vprintf setter that I wrote for my work here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants