From 5bba92f76c8b563c54281d8950ab21a209bb325e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 5 Aug 2019 19:47:00 +0100 Subject: Move width limited domain writer to a general location --- icetray/icetray/logWriterConsole.cpp | 55 +----------------------------------- icetray/icetray/logger.cpp | 44 +++++++++++++++++++++++++++++ icetray/icetray/logger.h | 13 +++++++++ 3 files changed, 58 insertions(+), 54 deletions(-) diff --git a/icetray/icetray/logWriterConsole.cpp b/icetray/icetray/logWriterConsole.cpp index 11d9a16..5894e47 100644 --- a/icetray/icetray/logWriterConsole.cpp +++ b/icetray/icetray/logWriterConsole.cpp @@ -1,64 +1,11 @@ #include #include "logWriterConsole.h" -#include #include -namespace AdHoc { - AdHocFormatter(DomainFmt, ".%?"); - StreamWriterT('d') { - template - static void write(stream & s, ssize_t width, const IceTray::Logging::Domain & domain, const Pn & ... pn) - { - auto di = domain.begin(); - if (di == domain.end()) { - return StreamWriter::next(s, pn...); - } - - if (width == -1) { - s << *di++; - while (di != domain.end()) { - DomainFmt::write(s, *di++); - } - } - else { - auto target = width; - while (di != domain.end()) { - auto total = di == domain.begin() ? -1 : 0; - for (auto dic = di; dic != domain.end(); dic++) { - total += 1 + dic->length(); - } - if (di == domain.begin()) { - if (total > target) { - s << di->front(); - target -= 1; - } - else { - s << *di; - target -= di->length(); - } - } - else { - if (total > target) { - DomainFmt::write(s, di->front()); - target -= 2; - } - else { - DomainFmt::write(s, *di); - target -= 1 + di->length(); - } - } - di++; - } - } - StreamWriter::next(s, pn...); - } - }; -} - // NOLINTNEXTLINE(modernize-concat-nested-namespaces) namespace IceTray { namespace Logging { - AdHocFormatter(LogMsg, "%?: %d: %?\n"); + AdHocFormatter(LogMsg, "%?: %D: %?\n"); ConsoleLogWriter::ConsoleLogWriter(const Ice::PropertiesPtr & p) : AbstractLogWriter("logging.console", p), width(p ? p->getPropertyAsIntWithDefault("logging.console.width", -1) : -1) diff --git a/icetray/icetray/logger.cpp b/icetray/icetray/logger.cpp index 6ceff96..0944947 100644 --- a/icetray/icetray/logger.cpp +++ b/icetray/icetray/logger.cpp @@ -228,6 +228,50 @@ namespace IceTray { boost::algorithm::split(domainTokens, domain, boost::algorithm::is_any_of(".:"), boost::algorithm::token_compress_on); return domainTokens; } + + AdHocFormatter(DomainFmt, ".%?"); + void + AbstractLogWriter::writeDomain(std::ostream & s, ssize_t width, const IceTray::Logging::Domain & domain) + { + if (auto di = domain.begin(); di != domain.end()) { + if (width == -1) { + s << *di++; + while (di != domain.end()) { + DomainFmt::write(s, *di++); + } + } + else { + auto target = width; + while (di != domain.end()) { + auto total = di == domain.begin() ? -1 : 0; + for (auto dic = di; dic != domain.end(); dic++) { + total += 1 + dic->length(); + } + if (di == domain.begin()) { + if (total > target) { + s << di->front(); + target -= 1; + } + else { + s << *di; + target -= di->length(); + } + } + else { + if (total > target) { + DomainFmt::write(s, di->front()); + target -= 2; + } + else { + DomainFmt::write(s, *di); + target -= 1 + di->length(); + } + } + di++; + } + } + } + } } } diff --git a/icetray/icetray/logger.h b/icetray/icetray/logger.h index 5e7ef1f..2cdfcb1 100644 --- a/icetray/icetray/logger.h +++ b/icetray/icetray/logger.h @@ -10,6 +10,7 @@ #include #include #include +#include namespace IceTray { namespace Logging { @@ -106,6 +107,7 @@ namespace IceTray { public: static Domain splitDomain(const std::string &); + static void writeDomain(std::ostream &, ssize_t, const Domain &); }; typedef AdHoc::Factory LogWriterFactory; @@ -116,5 +118,16 @@ namespace LOG = ::IceTray::Logging; #define LOGMANAGER() (::IceTray::Logging::LogManager::getDefault()) +namespace AdHoc { + StreamWriterT('D') { + template + static void write(stream & s, ssize_t width, const IceTray::Logging::Domain & domain, const Pn & ... pn) + { + IceTray::Logging::AbstractLogWriter::writeDomain(s, width, domain); + StreamWriter::next(s, pn...); + } + }; +} + #endif -- cgit v1.2.3