Skip to content

Commit

Permalink
Add support of printf() to MessageLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
fwyzard committed Jul 10, 2020
1 parent a9dd000 commit 58a3ab1
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 0 deletions.
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
8 changes: 8 additions & 0 deletions FWCore/MessageService/test/UnitTestClient_C.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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
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 @@ -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

0 comments on commit 58a3ab1

Please sign in to comment.