diff --git a/FWCore/MessageLogger/interface/ErrorObj.h b/FWCore/MessageLogger/interface/ErrorObj.h index 7f05699e2894a..d48e762871fbe 100644 --- a/FWCore/MessageLogger/interface/ErrorObj.h +++ b/FWCore/MessageLogger/interface/ErrorObj.h @@ -79,6 +79,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(std::string_view 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 a61d10e414d23..f0a2000ee4528 100644 --- a/FWCore/MessageLogger/interface/MessageLogger.h +++ b/FWCore/MessageLogger/interface/MessageLogger.h @@ -101,6 +101,13 @@ namespace edm { return *this; } + template + ThisLog& printf(std::string_view fmt, Args const&... args) { + if (ap.valid()) + ap.printf(fmt, args...); + return *this; + } + template ThisLog& log(F&& iF) { if (ap.valid()) { @@ -202,6 +209,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 64b103b8e758a..18e6f712d7ad3 100644 --- a/FWCore/MessageLogger/interface/MessageSender.h +++ b/FWCore/MessageLogger/interface/MessageSender.h @@ -44,6 +44,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() const noexcept { return errorobj_p != nullptr; } private: diff --git a/FWCore/MessageService/bin/Standalone.cpp b/FWCore/MessageService/bin/Standalone.cpp index 098f795cf2803..cff5c1d538299 100644 --- a/FWCore/MessageService/bin/Standalone.cpp +++ b/FWCore/MessageService/bin/Standalone.cpp @@ -9,6 +9,7 @@ ----------------------------------------------------------------------*/ +#include #include #include #include @@ -41,7 +42,7 @@ void DoMyStuff() { // be substantially more complex. This example is about as simple // as can be. - double d = 3.14159265357989; + double d = M_PI; edm::LogWarning("cat_A") << "Test of std::setprecision(p):" << " Pi with precision 12 is " << std::setprecision(12) << d; diff --git a/FWCore/MessageService/test/UnitTestClient_C.cc b/FWCore/MessageService/test/UnitTestClient_C.cc index d188740577211..d3475dfeb4931 100644 --- a/FWCore/MessageService/test/UnitTestClient_C.cc +++ b/FWCore/MessageService/test/UnitTestClient_C.cc @@ -1,11 +1,12 @@ +#include +#include + #include "FWCore/Framework/interface/Frameworkfwd.h" -#include "FWCore/Framework/interface/global/EDAnalyzer.h" #include "FWCore/Framework/interface/MakerMacros.h" +#include "FWCore/Framework/interface/global/EDAnalyzer.h" #include "FWCore/MessageLogger/interface/MessageLogger.h" #include "FWCore/Utilities/interface/StreamID.h" -#include - namespace edmtest { class UnitTestClient_C : public edm::global::EDAnalyzer<> { @@ -17,24 +18,32 @@ namespace edmtest { void UnitTestClient_C::analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const { int i = 145; - edm::LogWarning("cat_A") << "Test of std::hex:" << i << std::hex << "in hex is" << i; - edm::LogWarning("cat_A") << "Test of std::setw(n) and std::setfill('c'):" - << "The following should read ++abcdefg $$$12:" << std::setfill('+') << std::setw(9) + edm::LogWarning("cat_A") << "Test of std::hex: " << i << std::hex << " in hex is " << i; + edm::LogWarning("cat_A") << "Test of std::setw(n) and std::setfill('c'): " + << "The following should read ++abcdefg $$$12: " << std::setfill('+') << std::setw(9) << "abcdefg" << std::setw(5) << std::setfill('$') << 12; - double d = 3.14159265357989; - edm::LogWarning("cat_A") << "Test of std::setprecision(p):" - << "Pi with precision 12 is" << std::setprecision(12) << d; - edm::LogWarning("cat_A") << "Test of spacing:" - << "The following should read a b c dd:" + double d = M_PI; + edm::LogWarning("cat_A") << "Test of std::setprecision(p): " + << "Pi with precision 12 is " << std::setprecision(12) << d; + edm::LogWarning("cat_A") << "Test of spacing: " + << "The following should read a b c dd: " << "a" << std::setfill('+') << "b" << std::hex << "c" << std::setw(2) << "dd"; edm::LogWarning("cat_A").format("Test of format hex: {0} in hex is {0:x}", i); edm::LogWarning("cat_A") - .format("Test of format fill and width:") + .format("Test of format fill and width: ") .format("The following should read ++abcdefg $$$12: {:+>9} {:$>5}", "abcdefg", 12); - edm::LogWarning("cat_A").format("Test of format precision:Pi with precision 12 is {:.12g}", d); + 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"); } } // namespace edmtest diff --git a/FWCore/MessageService/test/UnitTestClient_G.cc b/FWCore/MessageService/test/UnitTestClient_G.cc index 0b54dbd5a7d71..c36d8c0e6419a 100644 --- a/FWCore/MessageService/test/UnitTestClient_G.cc +++ b/FWCore/MessageService/test/UnitTestClient_G.cc @@ -1,12 +1,13 @@ +#include +#include +#include + #include "FWCore/Framework/interface/Frameworkfwd.h" -#include "FWCore/Framework/interface/global/EDAnalyzer.h" #include "FWCore/Framework/interface/MakerMacros.h" +#include "FWCore/Framework/interface/global/EDAnalyzer.h" #include "FWCore/MessageLogger/interface/MessageLogger.h" #include "FWCore/Utilities/interface/StreamID.h" -#include -#include - namespace edmtest { class UnitTestClient_G : public edm::global::EDAnalyzer<> { @@ -21,7 +22,7 @@ namespace edmtest { std::cerr << "??? It appears that Message Processing is not Set Up???\n\n"; } - double d = 3.14159265357989; + double d = M_PI; edm::LogWarning("cat_A") << "Test of std::setprecision(p):" << " Pi with precision 12 is " << std::setprecision(12) << d; diff --git a/FWCore/MessageService/test/unit_test_outputs/infos.log b/FWCore/MessageService/test/unit_test_outputs/infos.log index ad901a6146258..3a90f780effd0 100644 --- a/FWCore/MessageService/test/unit_test_outputs/infos.log +++ b/FWCore/MessageService/test/unit_test_outputs/infos.log @@ -1,6 +1,6 @@ Begin processing the 1st record. Run 1, Event 1, LumiSection 1 on stream 0 at {Timestamp} %MSG-w cat_A: UnitTestClient_G:sendSomeMessages Run: 1 Event: 1 -Test of std::setprecision(p): Pi with precision 12 is 3.14159265358 +Test of std::setprecision(p): Pi with precision 12 is 3.14159265359 %MSG %MSG-i cat_B: UnitTestClient_G:sendSomeMessages Run: 1 Event: 1 Emit Info level message 1 diff --git a/FWCore/MessageService/test/unit_test_outputs/u10_warnings.log b/FWCore/MessageService/test/unit_test_outputs/u10_warnings.log index 5a1ffffbaa77a..b446f718bcc7c 100644 --- a/FWCore/MessageService/test/unit_test_outputs/u10_warnings.log +++ b/FWCore/MessageService/test/unit_test_outputs/u10_warnings.log @@ -1,24 +1,36 @@ %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 -Test of std::hex:145in hex is91 +Test of std::hex: 145 in hex is 91 %MSG %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 -Test of std::setw(n) and std::setfill('c'):The following should read ++abcdefg $$$12:++abcdefg$$$12 +Test of std::setw(n) and std::setfill('c'): The following should read ++abcdefg $$$12: ++abcdefg$$$12 %MSG %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 -Test of std::setprecision(p):Pi with precision 12 is3.14159265358 +Test of std::setprecision(p): Pi with precision 12 is 3.14159265359 %MSG %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 -Test of spacing:The following should read a b c dd:abcdd +Test of spacing: The following should read a b c dd: abcdd %MSG %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 Test of format hex: 145 in hex is 91 %MSG %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 -Test of format fill and width:The following should read ++abcdefg $$$12: ++abcdefg $$$12 +Test of format fill and width: The following should read ++abcdefg $$$12: ++abcdefg $$$12 %MSG %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 -Test of format precision:Pi with precision 12 is 3.14159265358 +Test of format precision: Pi with precision 12 is 3.14159265359 %MSG %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/u20_cerr.log b/FWCore/MessageService/test/unit_test_outputs/u20_cerr.log index d8a139019a5db..b0c67a9be6e9a 100644 --- a/FWCore/MessageService/test/unit_test_outputs/u20_cerr.log +++ b/FWCore/MessageService/test/unit_test_outputs/u20_cerr.log @@ -1,6 +1,6 @@ Begin processing the 1st record. Run 1, Event 1, LumiSection 1 on stream 0 at {Timestamp} %MSG-w cat_A: UnitTestClient_G:sendSomeMessages Run: 1 Event: 1 -Test of std::setprecision(p): Pi with precision 12 is 3.14159265358 +Test of std::setprecision(p): Pi with precision 12 is 3.14159265359 %MSG %MSG-w cat_C: UnitTestClient_G:sendSomeMessages Run: 1 Event: 1 Emit Warning level message 1 diff --git a/FWCore/MessageService/test/unit_test_outputs/u6_warnings.log b/FWCore/MessageService/test/unit_test_outputs/u6_warnings.log index 5a1ffffbaa77a..b446f718bcc7c 100644 --- a/FWCore/MessageService/test/unit_test_outputs/u6_warnings.log +++ b/FWCore/MessageService/test/unit_test_outputs/u6_warnings.log @@ -1,24 +1,36 @@ %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 -Test of std::hex:145in hex is91 +Test of std::hex: 145 in hex is 91 %MSG %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 -Test of std::setw(n) and std::setfill('c'):The following should read ++abcdefg $$$12:++abcdefg$$$12 +Test of std::setw(n) and std::setfill('c'): The following should read ++abcdefg $$$12: ++abcdefg$$$12 %MSG %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 -Test of std::setprecision(p):Pi with precision 12 is3.14159265358 +Test of std::setprecision(p): Pi with precision 12 is 3.14159265359 %MSG %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 -Test of spacing:The following should read a b c dd:abcdd +Test of spacing: The following should read a b c dd: abcdd %MSG %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 Test of format hex: 145 in hex is 91 %MSG %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 -Test of format fill and width:The following should read ++abcdefg $$$12: ++abcdefg $$$12 +Test of format fill and width: The following should read ++abcdefg $$$12: ++abcdefg $$$12 %MSG %MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1 -Test of format precision:Pi with precision 12 is 3.14159265358 +Test of format precision: Pi with precision 12 is 3.14159265359 %MSG %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/warnings.log b/FWCore/MessageService/test/unit_test_outputs/warnings.log index c2794bebaab60..012fbb6be81bc 100644 --- a/FWCore/MessageService/test/unit_test_outputs/warnings.log +++ b/FWCore/MessageService/test/unit_test_outputs/warnings.log @@ -1,5 +1,5 @@ %MSG-w cat_A: UnitTestClient_G:sendSomeMessages Run: 1 Event: 1 -Test of std::setprecision(p): Pi with precision 12 is 3.14159265358 +Test of std::setprecision(p): Pi with precision 12 is 3.14159265359 %MSG %MSG-w cat_C: UnitTestClient_G:sendSomeMessages Run: 1 Event: 1 Emit Warning level message 1