Code Monkey home page Code Monkey logo

Comments (6)

Lancern avatar Lancern commented on June 12, 2024

At the very least we should simplify the procedure to create new loggers based on the default logger providing a different name. It's a very common practice for library authors.

from spdlog-rs.

SpriteOvO avatar SpriteOvO commented on June 12, 2024

Interesting suggestion, which involves some details to consider.

First, there is actually a simpler way to do this than your first example.

let mut new_logger = spdlog::default_logger().deref().clone();
new_logger.set_name(Some("new name"));

(Logger::set_name here does not currently exist, I just forgot it XD, and implement it is easy)

Based on this, we need to reconsider the need for a shorthand method. I don't completely reject your proposal, but we need to figure out the following questions:

  • In the current implementation, in order to implement periodic flushing, the receiver of set_flush_period is &Arc<Self> (code).

    LoggerBuilder returns Logger instead of Arc<Logger>, so it's not possible for Logger::new_from_logger to exactly replicate all the arguments of a logger (periodic flushing will not work on the new logger, if the old logger set it).

    Neither can my example above, which would cause a panic at runtime if a flush period is set (code). So along with set_name, a try_clone may also need to be implemented.

  • So it should be possible to implement a

    Logger::clone_with(self: &Arc<Self>, modifier: impl FnOnce(&mut Logger)) -> Arc<Logger>

    , and it will be used in this way:

    let new_logger = spdlog::default_logger().clone_with(|new| new.set_name("new logger"));

    Compared to the above code, it is just one less line. However, since it serves only Arc<Logger>, it should be able to replicate all arguments (although users cannot modify the flush period in the closure).

  • Further, I think that if we can find a way to implement the receiver of set_flush_period as &Logger instead of the current &Arc<Logger>, and it doesn't cost much (e.g. performance, interoperability, etc.), then my first example is short enough and should be able to replicate all the arguments, so we don't need a clone_with shorthand, which is probably the best.

from spdlog-rs.

Lancern avatar Lancern commented on June 12, 2024

from spdlog-rs.

SpriteOvO avatar SpriteOvO commented on June 12, 2024

clone_with_name should not be too different from clone_with, its function signature also requires the same receiver &Arc<Logger> and the same return type Arc<Logger>. I prefer clone_with because it provides more interoperability, so that in addition to downstream library code, downstream binary code will also benefit, as downstream binary code is particularly interested in properties such as sinks.

from spdlog-rs.

Lancern avatar Lancern commented on June 12, 2024

I suggest we add both of clone_with and clone_with_name, and make clone_with_name a convenient wrapper around clone_with.

from spdlog-rs.

SpriteOvO avatar SpriteOvO commented on June 12, 2024

I suggest we add both of clone_with and clone_with_name, and make clone_with_name a convenient wrapper around clone_with.

Agree with that, I will open a PR later :)

from spdlog-rs.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.