Commit 5d7845c1 authored by gabime's avatar gabime

Added "clone()" support to loggers

parent 91d8869f
...@@ -161,6 +161,18 @@ void daily_example() ...@@ -161,6 +161,18 @@ void daily_example()
--- ---
#### Periodic flush #### Periodic flush
```c++ ```c++
// clone a logger and give it new name.
// Useful for creating subsystem loggers from some "root" logger
void clone_example()
{
auto network_logger = spdlog::get("console")->clone("network");
network_logger->info("Logging network stuff..");
}
```
---
#### Cloning loggers for
```c++
// periodically flush all *registered* loggers every 3 seconds: // periodically flush all *registered* loggers every 3 seconds:
// warning: only use if all your loggers are thread safe! // warning: only use if all your loggers are thread safe!
spdlog::flush_every(std::chrono::seconds(3)); spdlog::flush_every(std::chrono::seconds(3));
......
...@@ -99,13 +99,9 @@ inline void spdlog::async_logger::backend_flush_() ...@@ -99,13 +99,9 @@ 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) 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), auto cloned = std::make_shared<spdlog::async_logger>(std::move(new_name), sinks_.begin(), sinks_.end(), thread_pool_, overflow_policy_);
sinks_.begin(), sinks_.end(),
thread_pool_,
overflow_policy_);
cloned->set_level(this->level()); cloned->set_level(this->level());
cloned->flush_on(this->flush_level()); cloned->flush_on(this->flush_level());
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include <memory> #include <memory>
#include <string> #include <string>
// create logger with given name, sinks and the default pattern formatter // create logger with given name, sinks and the default pattern formatter
// all other ctors will call this one // all other ctors will call this one
template<typename It> template<typename It>
...@@ -276,7 +275,6 @@ inline spdlog::level::level_enum spdlog::logger::flush_level() const ...@@ -276,7 +275,6 @@ inline spdlog::level::level_enum spdlog::logger::flush_level() const
return static_cast<spdlog::level::level_enum>(flush_level_.load(std::memory_order_relaxed)); 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);
......
...@@ -115,7 +115,6 @@ public: ...@@ -115,7 +115,6 @@ 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);
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
#include <cstdio> #include <cstdio>
#include <exception> #include <exception>
#include <fstream> #include <fstream>
#include <ostream>
#include <iostream> #include <iostream>
#include <ostream>
#include <string> #include <string>
#define SPDLOG_TRACE_ON #define SPDLOG_TRACE_ON
......
...@@ -2,12 +2,11 @@ ...@@ -2,12 +2,11 @@
using namespace std::chrono; using namespace std::chrono;
using std::chrono::milliseconds; using std::chrono::milliseconds;
using test_clock = std::chrono::high_resolution_clock ; using test_clock = std::chrono::high_resolution_clock;
static milliseconds millis_from(const test_clock::time_point &tp0) static milliseconds millis_from(const test_clock::time_point &tp0)
{ {
return std::chrono::duration_cast<milliseconds>(test_clock::now()-tp0); return std::chrono::duration_cast<milliseconds>(test_clock::now() - tp0);
} }
TEST_CASE("dequeue-empty-nowait", "[mpmc_blocking_q]") TEST_CASE("dequeue-empty-nowait", "[mpmc_blocking_q]")
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment