diff --git a/dune/xt/common/timedlogging.cc b/dune/xt/common/timedlogging.cc index e1245eb3daba394bff8db9ef0d34dc63c4afe65b..e97e11d1d965f9b841b3f1a07de09c4734196980 100644 --- a/dune/xt/common/timedlogging.cc +++ b/dune/xt/common/timedlogging.cc @@ -27,22 +27,23 @@ namespace XT { namespace Common { -DefaultLogger::DefaultLogger(const std::string& prefix, +DefaultLogger::DefaultLogger(const std::string& prfx, bool start_disabled, const std::array<std::string, 3>& colors, bool global_timer) - : timer_() - , prefix_(prefix) + : prefix(prfx) + , copy_count(0) + , timer_() , colors_(colors) , global_timer_(global_timer) , info_(std::make_shared<TimedPrefixedLogStream>(global_timer_ ? SecondsSinceStartup() : timer_, - build_prefix(prefix_.empty() ? "info" : prefix_, colors_[0]), + build_prefix(prfx.empty() ? "info" : prfx, copy_count, colors_[0]), std::cout)) , debug_(std::make_shared<TimedPrefixedLogStream>(global_timer_ ? SecondsSinceStartup() : timer_, - build_prefix(prefix_.empty() ? "debug" : prefix_, colors_[1]), + build_prefix(prfx.empty() ? "debug" : prfx, copy_count, colors_[1]), std::cout)) , warn_(std::make_shared<TimedPrefixedLogStream>(global_timer_ ? SecondsSinceStartup() : timer_, - build_prefix(prefix_.empty() ? "warn" : prefix_, colors_[2]), + build_prefix(prfx.empty() ? "warn" : prfx, copy_count, colors_[2]), std::cerr)) , info_enabled(!start_disabled) , debug_enabled(!start_disabled) @@ -50,19 +51,23 @@ DefaultLogger::DefaultLogger(const std::string& prefix, {} DefaultLogger::DefaultLogger(const DefaultLogger& other) - : timer_() - , prefix_(other.prefix_) + : prefix(other.prefix) + , copy_count(other.copy_count + 1) + , timer_() , colors_(other.colors_) , global_timer_(other.global_timer_) - , info_(std::make_shared<TimedPrefixedLogStream>(global_timer_ ? SecondsSinceStartup() : timer_, - build_prefix(prefix_.empty() ? "info" : prefix_, colors_[0]), - std::cout)) - , debug_(std::make_shared<TimedPrefixedLogStream>(global_timer_ ? SecondsSinceStartup() : timer_, - build_prefix(prefix_.empty() ? "debug" : prefix_, colors_[1]), - std::cout)) - , warn_(std::make_shared<TimedPrefixedLogStream>(global_timer_ ? SecondsSinceStartup() : timer_, - build_prefix(prefix_.empty() ? "warn" : prefix_, colors_[2]), - std::cerr)) + , info_( + std::make_shared<TimedPrefixedLogStream>(global_timer_ ? SecondsSinceStartup() : timer_, + build_prefix(prefix.empty() ? "info" : prefix, copy_count, colors_[0]), + std::cout)) + , debug_(std::make_shared<TimedPrefixedLogStream>( + global_timer_ ? SecondsSinceStartup() : timer_, + build_prefix(prefix.empty() ? "debug" : prefix, copy_count, colors_[1]), + std::cout)) + , warn_( + std::make_shared<TimedPrefixedLogStream>(global_timer_ ? SecondsSinceStartup() : timer_, + build_prefix(prefix.empty() ? "warn" : prefix, copy_count, colors_[2]), + std::cerr)) , info_enabled(other.info_enabled) , debug_enabled(other.debug_enabled) , warn_enabled(other.warn_enabled) @@ -71,19 +76,23 @@ DefaultLogger::DefaultLogger(const DefaultLogger& other) DefaultLogger& DefaultLogger::operator=(const DefaultLogger& other) { if (&other != this) { + prefix = other.prefix; + copy_count = other.copy_count; timer_ = other.timer_; - prefix_ = other.prefix_; colors_ = other.colors_; global_timer_ = other.global_timer_; - info_ = std::make_shared<TimedPrefixedLogStream>(global_timer_ ? SecondsSinceStartup() : timer_, - build_prefix(prefix_.empty() ? "info" : prefix_, colors_[0]), - std::cout); - debug_ = std::make_shared<TimedPrefixedLogStream>(global_timer_ ? SecondsSinceStartup() : timer_, - build_prefix(prefix_.empty() ? "debug" : prefix_, colors_[1]), - std::cout); - warn_ = std::make_shared<TimedPrefixedLogStream>(global_timer_ ? SecondsSinceStartup() : timer_, - build_prefix(prefix_.empty() ? "warn" : prefix_, colors_[2]), - std::cerr); + info_ = + std::make_shared<TimedPrefixedLogStream>(global_timer_ ? SecondsSinceStartup() : timer_, + build_prefix(prefix.empty() ? "info" : prefix, copy_count, colors_[0]), + std::cout); + debug_ = std::make_shared<TimedPrefixedLogStream>( + global_timer_ ? SecondsSinceStartup() : timer_, + build_prefix(prefix.empty() ? "debug" : prefix, copy_count, colors_[1]), + std::cout); + warn_ = + std::make_shared<TimedPrefixedLogStream>(global_timer_ ? SecondsSinceStartup() : timer_, + build_prefix(prefix.empty() ? "warn" : prefix, copy_count, colors_[2]), + std::cerr); info_enabled = other.info_enabled; debug_enabled = other.debug_enabled; warn_enabled = other.warn_enabled; @@ -91,19 +100,20 @@ DefaultLogger& DefaultLogger::operator=(const DefaultLogger& other) return *this; } // ... operator=(...) -void DefaultLogger::enable(const std::string& prefix) +void DefaultLogger::enable(const std::string& prfx) { info_enabled = true; debug_enabled = true; warn_enabled = true; - if (!prefix.empty()) { - prefix_ = prefix; + if (!prfx.empty()) { + prefix = prfx; + copy_count = 0; info_ = std::make_shared<TimedPrefixedLogStream>( - global_timer_ ? SecondsSinceStartup() : timer_, build_prefix(prefix_, colors_[0]), std::cout); + global_timer_ ? SecondsSinceStartup() : timer_, build_prefix(prfx, copy_count, colors_[0]), std::cout); debug_ = std::make_shared<TimedPrefixedLogStream>( - global_timer_ ? SecondsSinceStartup() : timer_, build_prefix(prefix_, colors_[1]), std::cout); + global_timer_ ? SecondsSinceStartup() : timer_, build_prefix(prfx, copy_count, colors_[1]), std::cout); warn_ = std::make_shared<TimedPrefixedLogStream>( - global_timer_ ? SecondsSinceStartup() : timer_, build_prefix(prefix_, colors_[2]), std::cerr); + global_timer_ ? SecondsSinceStartup() : timer_, build_prefix(prfx, copy_count, colors_[2]), std::cerr); } } // ... enable(...) diff --git a/dune/xt/common/timedlogging.hh b/dune/xt/common/timedlogging.hh index f4f0cf5d8bd4dc7dd033c7c665cb82773da66ea7..c9222033b6b516255f8184425ca8e281365be832 100644 --- a/dune/xt/common/timedlogging.hh +++ b/dune/xt/common/timedlogging.hh @@ -56,17 +56,26 @@ DUNE_EXPORT inline const Timer& SecondsSinceStartup() */ class DefaultLogger { - static std::string build_prefix(const std::string& prefix, const std::string& clr) + static std::string build_prefix(const std::string& prfx, const size_t cnt, const std::string& clr) { const std::string actual_color = terminal_supports_color() ? color(clr) : ""; + std::string copy_count_str = ""; + if (cnt > 0) + copy_count_str += "[" + to_string(cnt) + "]"; + std::string ret; if (actual_color.empty()) - return prefix + ": "; + ret = prfx + copy_count_str + ": "; else - return actual_color + StreamModifiers::bold + prefix + ": " + StreamModifiers::normal; - } + ret = actual_color + StreamModifiers::bold + prfx + StreamModifiers::normal + copy_count_str + + StreamModifiers::bold + ": " + StreamModifiers::normal; + return ret; + } // ... build_prefix(...) public: - DefaultLogger(const std::string& prefix = "", + std::string prefix; + size_t copy_count; + + DefaultLogger(const std::string& prfx = "", bool start_disabled = false, const std::array<std::string, 3>& colors = {"blue", "darkgray", "red"}, bool global_timer = true); @@ -77,7 +86,6 @@ public: private: Timer timer_; - std::string prefix_; std::array<std::string, 3> colors_; bool global_timer_; std::shared_ptr<std::ostream> info_; @@ -89,7 +97,14 @@ public: bool debug_enabled; bool warn_enabled; - void enable(const std::string& prefix = ""); + void enable(const std::string& prfx = ""); + + void enable_like(const DefaultLogger& other) + { + this->info_enabled = this->info_enabled || other.info_enabled; + this->debug_enabled = this->debug_enabled || other.debug_enabled; + this->warn_enabled = this->warn_enabled || other.warn_enabled; + } void disable(); @@ -126,51 +141,56 @@ class WithLogger public: mutable DefaultLogger logger; -protected: - const std::string logging_id; + DXT_DEPRECATED_MSG("Use this.logger.prefix instead (12.08.2020)!") const std::string logging_id; -public: - WithLogger(const std::string& prefix, const std::string& id, const bool start_enabled = false) - : logger(prefix, start_enabled) + DXT_DEPRECATED_MSG("Use WithLogger(id, start_enabled) instead (12.08.2020)!") + WithLogger(const std::string& /*prefix*/, const std::string& id, const bool start_enabled = false) + : logger(id, start_enabled) + , logging_id(id) + { + LOG_(debug) << "WithLogger(this=" << this << ")" << std::endl; + } + + WithLogger(const std::string& id, const bool start_enabled = false) + : logger(id, start_enabled) , logging_id(id) { - LOG_(debug) << logging_id << "(this=" << this << ")" << std::endl; + LOG_(debug) << "WithLogger(this=" << this << ")" << std::endl; } WithLogger(const ThisType& other) : logger(other.logger) - , logging_id(other.logging_id) + , logging_id(other.logger.prefix) { - LOG_(debug) << logging_id << "(this=" << this << ", other=" << &other << ")" << std::endl; + LOG_(debug) << "WithLogger(this=" << this << ", other=" << &other << ")" << std::endl; } WithLogger(ThisType&& source) : logger(std::move(source.logger)) , logging_id(std::move(source.logging_id)) { - LOG_(debug) << logging_id << "(this=" << this << ", source=" << &source << ")" << std::endl; + LOG_(debug) << "WithLogger(this=" << this << ", source=" << &source << ")" << std::endl; } ~WithLogger() { - LOG_(debug) << "~" << logging_id << "(this=" << this << ")" << std::endl; + LOG_(debug) << "~WithLogger(this=" << this << ")" << std::endl; } ThisType& operator=(const ThisType& other) { - LOG_(debug) << logging_id << "operator=(this=" << this << ", other=" << &other << ")" << std::endl; + LOG_(debug) << "WithLogger.operator=(this=" << this << ", other=" << &other << ")" << std::endl; } ThisType& operator=(ThisType&& source) { - LOG_(debug) << logging_id << "operator=(this=" << this << ", source=" << &source << ")" << std::endl; + LOG_(debug) << "WithLogger.operator=(this=" << this << ", source=" << &source << ")" << std::endl; } + DXT_DEPRECATED_MSG("Use this.logger.enable_like(other.logger) instead (12.08.2020)!") void enable_logging_like(const ThisType& other) { - this->logger.info_enabled = this->logger.info_enabled || other.logger.info_enabled; - this->logger.debug_enabled = this->logger.debug_enabled || other.logger.debug_enabled; - this->logger.warn_enabled = this->logger.warn_enabled || other.logger.warn_enabled; + this->logger.enable_like(other.logger); } }; // class WithLogger