mirror of
https://github.com/gabime/spdlog.git
synced 2025-01-12 08:50:26 +08:00
Added "clone()" support to loggers
This commit is contained in:
parent
1f8b2cbb8b
commit
91d8869f36
@ -18,6 +18,7 @@ void multi_sink_example();
|
|||||||
void user_defined_example();
|
void user_defined_example();
|
||||||
void err_handler_example();
|
void err_handler_example();
|
||||||
void syslog_example();
|
void syslog_example();
|
||||||
|
void clone_example();
|
||||||
|
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
@ -34,6 +35,8 @@ int main(int, char *[])
|
|||||||
rotating_example();
|
rotating_example();
|
||||||
daily_example();
|
daily_example();
|
||||||
|
|
||||||
|
clone_example();
|
||||||
|
|
||||||
// async logging using a backing thread pool
|
// async logging using a backing thread pool
|
||||||
async_example();
|
async_example();
|
||||||
|
|
||||||
@ -56,7 +59,7 @@ int main(int, char *[])
|
|||||||
// release any threads created by spdlog, and drop all loggers in the registry.
|
// release any threads created by spdlog, and drop all loggers in the registry.
|
||||||
spdlog::shutdown();
|
spdlog::shutdown();
|
||||||
}
|
}
|
||||||
// Exceptions will only be thrown upon failed logger or sink construction (not during logging)
|
// Exceptions will only be thrown upon failed logger or sink construction (not during logging)
|
||||||
catch (const spdlog::spdlog_ex &ex)
|
catch (const spdlog::spdlog_ex &ex)
|
||||||
{
|
{
|
||||||
std::cout << "Log init failed: " << ex.what() << std::endl;
|
std::cout << "Log init failed: " << ex.what() << std::endl;
|
||||||
@ -122,6 +125,14 @@ void daily_example()
|
|||||||
auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
|
auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clone a logger and give it new name.
|
||||||
|
// Useful for creating component/subsystem loggers from some "root" logger
|
||||||
|
void clone_example()
|
||||||
|
{
|
||||||
|
auto network_logger = spdlog::get("console")->clone("network");
|
||||||
|
network_logger->info("Logging network stuff..");
|
||||||
|
}
|
||||||
|
|
||||||
#include "spdlog/async.h"
|
#include "spdlog/async.h"
|
||||||
void async_example()
|
void async_example()
|
||||||
{
|
{
|
||||||
|
@ -55,6 +55,8 @@ public:
|
|||||||
async_logger(std::string logger_name, sink_ptr single_sink, std::weak_ptr<details::thread_pool> tp,
|
async_logger(std::string logger_name, sink_ptr single_sink, std::weak_ptr<details::thread_pool> tp,
|
||||||
async_overflow_policy overflow_policy = async_overflow_policy::block);
|
async_overflow_policy overflow_policy = async_overflow_policy::block);
|
||||||
|
|
||||||
|
std::shared_ptr<logger> clone(std::string new_name) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void sink_it_(details::log_msg &msg) override;
|
void sink_it_(details::log_msg &msg) override;
|
||||||
void flush_() override;
|
void flush_() override;
|
||||||
|
@ -98,3 +98,17 @@ inline void spdlog::async_logger::backend_flush_()
|
|||||||
}
|
}
|
||||||
SPDLOG_CATCH_AND_HANDLE
|
SPDLOG_CATCH_AND_HANDLE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline std::shared_ptr<spdlog::logger> spdlog::async_logger::clone(std::string new_name)
|
||||||
|
{
|
||||||
|
auto cloned = std::make_shared<spdlog::async_logger>(std::move(new_name),
|
||||||
|
sinks_.begin(), sinks_.end(),
|
||||||
|
thread_pool_,
|
||||||
|
overflow_policy_);
|
||||||
|
|
||||||
|
cloned->set_level(this->level());
|
||||||
|
cloned->flush_on(this->flush_level());
|
||||||
|
cloned->set_error_handler(this->error_handler());
|
||||||
|
return std::move(cloned);
|
||||||
|
}
|
@ -271,6 +271,12 @@ inline void spdlog::logger::flush_on(level::level_enum log_level)
|
|||||||
flush_level_.store(log_level);
|
flush_level_.store(log_level);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline spdlog::level::level_enum spdlog::logger::flush_level() const
|
||||||
|
{
|
||||||
|
return static_cast<spdlog::level::level_enum>(flush_level_.load(std::memory_order_relaxed));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
inline bool spdlog::logger::should_flush_(const details::log_msg &msg)
|
inline bool spdlog::logger::should_flush_(const details::log_msg &msg)
|
||||||
{
|
{
|
||||||
auto flush_level = flush_level_.load(std::memory_order_relaxed);
|
auto flush_level = flush_level_.load(std::memory_order_relaxed);
|
||||||
@ -346,3 +352,12 @@ inline std::vector<spdlog::sink_ptr> &spdlog::logger::sinks()
|
|||||||
{
|
{
|
||||||
return sinks_;
|
return sinks_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline std::shared_ptr<spdlog::logger> spdlog::logger::clone(std::string logger_name)
|
||||||
|
{
|
||||||
|
auto cloned = std::make_shared<spdlog::logger>(std::move(logger_name), sinks_.begin(), sinks_.end());
|
||||||
|
cloned->set_level(this->level());
|
||||||
|
cloned->flush_on(this->flush_level());
|
||||||
|
cloned->set_error_handler(this->error_handler());
|
||||||
|
return std::move(cloned);
|
||||||
|
}
|
@ -115,21 +115,28 @@ public:
|
|||||||
level::level_enum level() const;
|
level::level_enum level() const;
|
||||||
const std::string &name() const;
|
const std::string &name() const;
|
||||||
|
|
||||||
|
|
||||||
// set formatting for the sinks in this logger.
|
// set formatting for the sinks in this logger.
|
||||||
// each sink will get a seperate instance of the formatter object.
|
// each sink will get a seperate instance of the formatter object.
|
||||||
void set_formatter(std::unique_ptr<formatter> formatter);
|
void set_formatter(std::unique_ptr<formatter> formatter);
|
||||||
void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
|
void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
|
||||||
|
|
||||||
|
// flush functions
|
||||||
void flush();
|
void flush();
|
||||||
void flush_on(level::level_enum log_level);
|
void flush_on(level::level_enum log_level);
|
||||||
|
level::level_enum flush_level() const;
|
||||||
|
|
||||||
|
// sinks
|
||||||
const std::vector<sink_ptr> &sinks() const;
|
const std::vector<sink_ptr> &sinks() const;
|
||||||
|
|
||||||
std::vector<sink_ptr> &sinks();
|
std::vector<sink_ptr> &sinks();
|
||||||
|
|
||||||
|
// error handler
|
||||||
void set_error_handler(log_err_handler err_handler);
|
void set_error_handler(log_err_handler err_handler);
|
||||||
log_err_handler error_handler();
|
log_err_handler error_handler();
|
||||||
|
|
||||||
|
// create new logger with same sinks and configuration.
|
||||||
|
virtual std::shared_ptr<logger> clone(std::string logger_name);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void sink_it_(details::log_msg &msg);
|
virtual void sink_it_(details::log_msg &msg);
|
||||||
virtual void flush_();
|
virtual void flush_();
|
||||||
|
Loading…
Reference in New Issue
Block a user