From 58a3ab1a650f6452e0db9e04b4a2cb64a108d992 Mon Sep 17 00:00:00 2001 From: Andrea Bocci Date: Fri, 10 Jul 2020 09:10:46 +0200 Subject: [PATCH] Add support of printf() to MessageLogger --- FWCore/MessageLogger/interface/ErrorObj.h | 2 + FWCore/MessageLogger/interface/ErrorObj.icc | 9 ++ .../MessageLogger/interface/MessageLogger.h | 89 +++++++++++++++++++ .../MessageLogger/interface/MessageSender.h | 7 ++ .../MessageService/test/UnitTestClient_C.cc | 8 ++ .../test/unit_test_outputs/u10_warnings.log | 12 +++ .../test/unit_test_outputs/u6_warnings.log | 12 +++ 7 files changed, 139 insertions(+) diff --git a/FWCore/MessageLogger/interface/ErrorObj.h b/FWCore/MessageLogger/interface/ErrorObj.h index 4a785b7c44810..50845dcd87d35 100644 --- a/FWCore/MessageLogger/interface/ErrorObj.h +++ b/FWCore/MessageLogger/interface/ErrorObj.h @@ -80,6 +80,8 @@ namespace edm { inline ErrorObj& operator<<(std::ios_base& (*f)(std::ios_base&)); template inline ErrorObj& format(std::string_view fmt, Args const&... args); + template + inline ErrorObj& printf(std::string_view fmt, Args const&... args); virtual ErrorObj& emitToken(const ELstring& txt); diff --git a/FWCore/MessageLogger/interface/ErrorObj.icc b/FWCore/MessageLogger/interface/ErrorObj.icc index 9a4bc683f734a..1e207ceaf6205 100644 --- a/FWCore/MessageLogger/interface/ErrorObj.icc +++ b/FWCore/MessageLogger/interface/ErrorObj.icc @@ -15,6 +15,7 @@ #include #include "fmt/ostream.h" +#include "fmt/printf.h" namespace edm { @@ -64,6 +65,14 @@ namespace edm { return *this; } + template + inline ErrorObj& ErrorObj::printf(std::string_view fmt, Args const&... args) { + auto str = fmt::sprintf(fmt, args...); + if (!str.empty()) + emitToken(str); + return *this; + } + // ---------------------------------------------------------------------- } // end of namespace edm diff --git a/FWCore/MessageLogger/interface/MessageLogger.h b/FWCore/MessageLogger/interface/MessageLogger.h index aa70963bb2675..a2f03f4ac89fa 100644 --- a/FWCore/MessageLogger/interface/MessageLogger.h +++ b/FWCore/MessageLogger/interface/MessageLogger.h @@ -172,6 +172,13 @@ namespace edm { return *this; } + template + LogWarning& printf(std::string_view fmt, Args const&... args) { + if (ap.valid()) + ap.printf(fmt, args...); + return *this; + } + template LogWarning& log(F&& iF) { if (ap.valid()) { @@ -218,6 +225,13 @@ namespace edm { return *this; } + template + LogError& printf(std::string_view fmt, Args const&... args) { + if (ap.valid()) + ap.printf(fmt, args...); + return *this; + } + template LogError& log(F&& iF) { if (ap.valid()) { @@ -259,6 +273,13 @@ namespace edm { return *this; } + template + LogSystem& printf(std::string_view fmt, Args const&... args) { + if (ap.valid()) + ap.printf(fmt, args...); + return *this; + } + template LogSystem& log(F&& iF) { iF(ap); @@ -306,6 +327,13 @@ namespace edm { return *this; } + template + LogInfo& printf(std::string_view fmt, Args const&... args) { + if (ap.valid()) + ap.printf(fmt, args...); + return *this; + } + template LogInfo& log(F&& iF) { if (ap.valid()) { @@ -358,6 +386,13 @@ namespace edm { return *this; } + template + LogVerbatim& printf(std::string_view fmt, Args&... args) { + if (ap.valid()) + ap.printf(fmt, args...); + return *this; + } + template LogVerbatim& log(F&& iF) { if (ap.valid()) { @@ -410,6 +445,13 @@ namespace edm { return *this; } + template + LogPrint& printf(std::string_view fmt, Args const&... args) { + if (ap.valid()) + ap.printf(fmt, args...); + return *this; + } + template LogPrint& log(F&& iF) { if (ap.valid()) { @@ -458,6 +500,13 @@ namespace edm { return *this; } + template + LogProblem& printf(std::string_view fmt, Args const&... args) { + if (ap.valid()) + ap.printf(fmt, args...); + return *this; + } + template LogProblem& log(F&& iF) { if (ap.valid()) { @@ -506,6 +555,13 @@ namespace edm { return *this; } + template + LogImportant& printf(std::string_view fmt, Args const&... args) { + if (ap.valid()) + ap.printf(fmt, args...); + return *this; + } + template LogImportant& log(F&& iF) { if (ap.valid()) { @@ -551,6 +607,13 @@ namespace edm { return *this; } + template + LogAbsolute& printf(std::string_view fmt, Args const&... args) { + if (ap.valid()) + ap.printf(fmt, args...); + return *this; + } + template LogAbsolute& log(F&& iF) { iF(ap); @@ -601,6 +664,13 @@ namespace edm { return *this; } + template + LogDebug_& printf(std::string_view fmt, Args const&... args) { + if (ap.valid()) + ap.printf(fmt, args...); + return *this; + } + template LogDebug_& log(F&& iF) { if (ap.valid()) { @@ -646,6 +716,13 @@ namespace edm { return *this; } + template + LogTrace_& printf(std::string_view fmt, Args const&... args) { + if (ap.valid()) + ap.printf(fmt, args...); + return *this; + } + template LogTrace_& log(F&& iF) { if (ap.valid()) { @@ -694,6 +771,13 @@ namespace edm { return *this; } + template + LogWarningThatSuppressesLikeLogInfo& printf(std::string_view fmt, Args const&... args) { + if (ap.valid()) + ap.printf(fmt, args...); + return *this; + } + template LogWarningThatSuppressesLikeLogInfo& log(F&& iF) { if (ap.valid()) { @@ -727,6 +811,11 @@ namespace edm { return *this; } + template + Suppress_LogDebug_& printf(std::string_view fmt, Args const&... args) { + return *this; + } + template Suppress_LogDebug_& log(F&& iF) { return *this; diff --git a/FWCore/MessageLogger/interface/MessageSender.h b/FWCore/MessageLogger/interface/MessageSender.h index d7e4b8195baed..477b631a4bdf4 100644 --- a/FWCore/MessageLogger/interface/MessageSender.h +++ b/FWCore/MessageLogger/interface/MessageSender.h @@ -49,6 +49,13 @@ namespace edm { return *this; } + template + MessageSender& printf(std::string_view fmt, Args const&... args) { + if (valid()) + errorobj_p->printf(fmt, args...); + return *this; + } + bool valid() { return errorobj_p != nullptr; } private: diff --git a/FWCore/MessageService/test/UnitTestClient_C.cc b/FWCore/MessageService/test/UnitTestClient_C.cc index d0d7c18566715..3a96aab55924f 100644 --- a/FWCore/MessageService/test/UnitTestClient_C.cc +++ b/FWCore/MessageService/test/UnitTestClient_C.cc @@ -31,6 +31,14 @@ namespace edmtest { edm::LogWarning("cat_A").format("Test of format precision: Pi with precision 12 is {:.12g}", d); edm::LogWarning("cat_A").format( "Test of format spacing: The following should read a b cc: {} {:+>} {:>2}", "a", "b", "cc"); + + edm::LogWarning("cat_A").printf("Test of printf hex: %d in hex is %x", i, i); + edm::LogWarning("cat_A") + .printf("Test of printf fill and width: ") + .printf("The following should read abcdefg 00012: %9s %05d", "abcdefg", 12); + edm::LogWarning("cat_A").printf("Test of printf precision: Pi with precision 12 is %.12g", d); + edm::LogWarning("cat_A").printf( + "Test of printf spacing: The following should read a b cc: %-2s%s%3s", "a", "b", "cc"); } // MessageLoggerClient::analyze() } // namespace edmtest diff --git a/FWCore/MessageService/test/unit_test_outputs/u10_warnings.log b/FWCore/MessageService/test/unit_test_outputs/u10_warnings.log index 2a78eb3743e28..b446f718bcc7c 100644 --- a/FWCore/MessageService/test/unit_test_outputs/u10_warnings.log +++ b/FWCore/MessageService/test/unit_test_outputs/u10_warnings.log @@ -22,3 +22,15 @@ Test of format precision: Pi with precision 12 is 3.14159265359 %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 Test of format spacing: The following should read a b cc: a b cc %MSG +%MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 +Test of printf hex: 145 in hex is 91 +%MSG +%MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 +Test of printf fill and width: The following should read abcdefg 00012: abcdefg 00012 +%MSG +%MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 +Test of printf precision: Pi with precision 12 is 3.14159265359 +%MSG +%MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 +Test of printf spacing: The following should read a b cc: a b cc +%MSG diff --git a/FWCore/MessageService/test/unit_test_outputs/u6_warnings.log b/FWCore/MessageService/test/unit_test_outputs/u6_warnings.log index 2a78eb3743e28..b446f718bcc7c 100644 --- a/FWCore/MessageService/test/unit_test_outputs/u6_warnings.log +++ b/FWCore/MessageService/test/unit_test_outputs/u6_warnings.log @@ -22,3 +22,15 @@ Test of format precision: Pi with precision 12 is 3.14159265359 %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 Test of format spacing: The following should read a b cc: a b cc %MSG +%MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 +Test of printf hex: 145 in hex is 91 +%MSG +%MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 +Test of printf fill and width: The following should read abcdefg 00012: abcdefg 00012 +%MSG +%MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 +Test of printf precision: Pi with precision 12 is 3.14159265359 +%MSG +%MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 +Test of printf spacing: The following should read a b cc: a b cc +%MSG