Skip to content

Commit

Permalink
Merge pull request cms-sw#30626 from makortel/messageLoggerFormat
Browse files Browse the repository at this point in the history
Add support of std::format()-like formatting to MessageLogger
  • Loading branch information
cmsbuild authored Jul 10, 2020
2 parents cad7e8c + dcedf4b commit 68eb8de
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions FWCore/MessageLogger/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<use name="FWCore/Utilities"/>
<use name="DataFormats/Provenance"/>
<use name="boost"/>
<use name="fmt"/>
<use name="tinyxml2"/>
<use name="tbb"/>
<export>
Expand Down
2 changes: 2 additions & 0 deletions FWCore/MessageLogger/interface/ErrorObj.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ namespace edm {
ErrorObj& opltlt(const char s[]);
inline ErrorObj& operator<<(std::ostream& (*f)(std::ostream&));
inline ErrorObj& operator<<(std::ios_base& (*f)(std::ios_base&));
template <typename... Args>
inline ErrorObj& format(std::string_view fmt, Args const&... args);

virtual ErrorObj& emitToken(const ELstring& txt);

Expand Down
10 changes: 10 additions & 0 deletions FWCore/MessageLogger/interface/ErrorObj.icc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include <sstream>

#include "fmt/ostream.h"

namespace edm {

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -54,6 +56,14 @@ namespace edm {
return *this;
}

template <typename... Args>
inline ErrorObj& ErrorObj::format(std::string_view fmt, Args const&... args) {
auto str = fmt::format(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 @@ -165,6 +165,13 @@ namespace edm {
return *this;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

template <typename F>
LogAbsolute& log(F&& iF) {
iF(ap);
Expand Down Expand Up @@ -531,6 +594,13 @@ namespace edm {
}
// Change log 8: The tests for ap.valid() being null

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

template <typename F>
LogDebug_& log(F&& iF) {
if (ap.valid()) {
Expand Down Expand Up @@ -569,6 +639,13 @@ namespace edm {
}
// Change log 8: The tests for ap.valid() being null

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

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

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

template <typename F>
LogWarningThatSuppressesLikeLogInfo& log(F&& iF) {
if (ap.valid()) {
Expand Down Expand Up @@ -638,6 +722,11 @@ namespace edm {
Suppress_LogDebug_& operator<<(std::ostream& (*)(std::ostream&)) { return *this; } // Change log 12
Suppress_LogDebug_& operator<<(std::ios_base& (*)(std::ios_base&)) { return *this; } // Change log 12

template <typename... Args>
Suppress_LogDebug_& format(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 @@ -42,6 +42,13 @@ namespace edm {
return *this;
}

template <typename... Args>
MessageSender& format(std::string_view fmt, Args const&... args) {
if (valid())
errorobj_p->format(fmt, args...);
return *this;
}

bool valid() { return errorobj_p != nullptr; }

private:
Expand Down
8 changes: 8 additions & 0 deletions FWCore/MessageService/test/UnitTestClient_C.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ namespace edmtest {
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("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 spacing: The following should read a b cc: {} {:+>} {:>2}", "a", "b", "cc");
} // MessageLoggerClient::analyze()

} // namespace edmtest
Expand Down
12 changes: 12 additions & 0 deletions FWCore/MessageService/test/unit_test_outputs/u10_warnings.log
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ Test of std::setprecision(p):Pi with precision 12 is3.14159265358
%MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1
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
%MSG
%MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1
Test of format precision:Pi with precision 12 is 3.14159265358
%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
12 changes: 12 additions & 0 deletions FWCore/MessageService/test/unit_test_outputs/u6_warnings.log
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ Test of std::setprecision(p):Pi with precision 12 is3.14159265358
%MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1
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
%MSG
%MSG-w cat_A: UnitTestClient_C:sendSomeMessages Run: 1 Event: 1
Test of format precision:Pi with precision 12 is 3.14159265358
%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

0 comments on commit 68eb8de

Please sign in to comment.