Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of printf()-like formatting to MessageLogger #30629

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions FWCore/MessageLogger/interface/ErrorObj.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ namespace edm {
inline ErrorObj& operator<<(std::ios_base& (*f)(std::ios_base&));
template <typename... Args>
inline ErrorObj& format(std::string_view fmt, Args const&... args);
template <typename... Args>
inline ErrorObj& printf(std::string_view fmt, Args const&... args);

virtual ErrorObj& emitToken(const ELstring& txt);

Expand Down
9 changes: 9 additions & 0 deletions FWCore/MessageLogger/interface/ErrorObj.icc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <sstream>

#include "fmt/ostream.h"
#include "fmt/printf.h"

namespace edm {

Expand Down Expand Up @@ -64,6 +65,14 @@ namespace edm {
return *this;
}

template <typename... Args>
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
89 changes: 89 additions & 0 deletions FWCore/MessageLogger/interface/MessageLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ namespace edm {
return *this;
}

template <typename... Args>
LogWarning& printf(std::string_view fmt, Args const&... args) {
if (ap.valid())
ap.printf(fmt, args...);
return *this;
}

template <typename F>
LogWarning& log(F&& iF) {
if (ap.valid()) {
Expand Down Expand Up @@ -218,6 +225,13 @@ namespace edm {
return *this;
}

template <typename... Args>
LogError& printf(std::string_view fmt, Args const&... args) {
if (ap.valid())
ap.printf(fmt, args...);
return *this;
}

template <typename F>
LogError& log(F&& iF) {
if (ap.valid()) {
Expand Down Expand Up @@ -259,6 +273,13 @@ namespace edm {
return *this;
}

template <typename... Args>
LogSystem& printf(std::string_view fmt, Args const&... args) {
if (ap.valid())
ap.printf(fmt, args...);
return *this;
}

template <typename F>
LogSystem& log(F&& iF) {
iF(ap);
Expand Down Expand Up @@ -306,6 +327,13 @@ namespace edm {
return *this;
}

template <typename... Args>
LogInfo& printf(std::string_view fmt, Args const&... args) {
if (ap.valid())
ap.printf(fmt, args...);
return *this;
}

template <typename F>
LogInfo& log(F&& iF) {
if (ap.valid()) {
Expand Down Expand Up @@ -358,6 +386,13 @@ namespace edm {
return *this;
}

template <typename... Args>
LogVerbatim& printf(std::string_view fmt, Args&... args) {
if (ap.valid())
ap.printf(fmt, args...);
return *this;
}

template <typename F>
LogVerbatim& log(F&& iF) {
if (ap.valid()) {
Expand Down Expand Up @@ -410,6 +445,13 @@ namespace edm {
return *this;
}

template <typename... Args>
LogPrint& printf(std::string_view fmt, Args const&... args) {
if (ap.valid())
ap.printf(fmt, args...);
return *this;
}

template <typename F>
LogPrint& log(F&& iF) {
if (ap.valid()) {
Expand Down Expand Up @@ -458,6 +500,13 @@ namespace edm {
return *this;
}

template <typename... Args>
LogProblem& printf(std::string_view fmt, Args const&... args) {
if (ap.valid())
ap.printf(fmt, args...);
return *this;
}

template <typename F>
LogProblem& log(F&& iF) {
if (ap.valid()) {
Expand Down Expand Up @@ -506,6 +555,13 @@ namespace edm {
return *this;
}

template <typename... Args>
LogImportant& printf(std::string_view fmt, Args const&... args) {
if (ap.valid())
ap.printf(fmt, args...);
return *this;
}

template <typename F>
LogImportant& log(F&& iF) {
if (ap.valid()) {
Expand Down Expand Up @@ -551,6 +607,13 @@ namespace edm {
return *this;
}

template <typename... Args>
LogAbsolute& printf(std::string_view fmt, Args const&... args) {
if (ap.valid())
ap.printf(fmt, args...);
return *this;
}

template <typename F>
LogAbsolute& log(F&& iF) {
iF(ap);
Expand Down Expand Up @@ -601,6 +664,13 @@ namespace edm {
return *this;
}

template <typename... Args>
LogDebug_& printf(std::string_view fmt, Args const&... args) {
if (ap.valid())
ap.printf(fmt, args...);
return *this;
}

template <typename F>
LogDebug_& log(F&& iF) {
if (ap.valid()) {
Expand Down Expand Up @@ -646,6 +716,13 @@ namespace edm {
return *this;
}

template <typename... Args>
LogTrace_& printf(std::string_view fmt, Args const&... args) {
if (ap.valid())
ap.printf(fmt, args...);
return *this;
}

template <typename F>
LogTrace_& log(F&& iF) {
if (ap.valid()) {
Expand Down Expand Up @@ -694,6 +771,13 @@ namespace edm {
return *this;
}

template <typename... Args>
LogWarningThatSuppressesLikeLogInfo& printf(std::string_view fmt, Args const&... args) {
if (ap.valid())
ap.printf(fmt, args...);
return *this;
}

template <typename F>
LogWarningThatSuppressesLikeLogInfo& log(F&& iF) {
if (ap.valid()) {
Expand Down Expand Up @@ -727,6 +811,11 @@ namespace edm {
return *this;
}

template <typename... Args>
Suppress_LogDebug_& printf(std::string_view fmt, Args const&... args) {
return *this;
}

template <typename F>
Suppress_LogDebug_& log(F&& iF) {
return *this;
Expand Down
7 changes: 7 additions & 0 deletions FWCore/MessageLogger/interface/MessageSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ namespace edm {
return *this;
}

template <typename... Args>
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:
Expand Down
3 changes: 2 additions & 1 deletion FWCore/MessageService/bin/Standalone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

----------------------------------------------------------------------*/

#include <cmath>
#include <exception>
#include <iostream>
#include <iomanip>
Expand Down Expand Up @@ -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;

Expand Down
42 changes: 25 additions & 17 deletions FWCore/MessageService/test/UnitTestClient_C.cc
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
#include "FWCore/MessageService/test/UnitTestClient_C.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <iomanip>

#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/MessageService/test/UnitTestClient_C.h"

namespace edmtest {

void UnitTestClient_C::analyze(edm::Event const& /*unused*/
,
void UnitTestClient_C::analyze(edm::Event const&, /*unused*/
edm::EventSetup const& /*unused*/
) {
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");
} // MessageLoggerClient::analyze()

} // namespace edmtest
Expand Down
16 changes: 8 additions & 8 deletions FWCore/MessageService/test/UnitTestClient_G.cc
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#include "FWCore/MessageService/test/UnitTestClient_G.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <iomanip>

#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/MessageService/test/UnitTestClient_G.h"

namespace edmtest {

void UnitTestClient_G::analyze(edm::Event const& /*unused*/
,
void UnitTestClient_G::analyze(edm::Event const&, /*unused*/
edm::EventSetup const& /*unused*/
) {
if (!edm::isMessageProcessingSetUp()) {
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;

Expand Down
2 changes: 1 addition & 1 deletion FWCore/MessageService/test/unit_test_outputs/infos.log
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 18 additions & 6 deletions FWCore/MessageService/test/unit_test_outputs/u10_warnings.log
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion FWCore/MessageService/test/unit_test_outputs/u20_cerr.log
Original file line number Diff line number Diff line change
@@ -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
Expand Down