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 #37245

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 @@ -79,6 +79,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(std::string_view 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
12 changes: 12 additions & 0 deletions FWCore/MessageLogger/interface/MessageLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ namespace edm {
return *this;
}

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

template <typename F>
ThisLog& log(F&& iF) {
if (ap.valid()) {
Expand Down Expand Up @@ -202,6 +209,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 @@ -44,6 +44,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() const noexcept { 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
35 changes: 22 additions & 13 deletions FWCore/MessageService/test/UnitTestClient_C.cc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <cmath>
#include <iomanip>

#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 <iomanip>

namespace edmtest {

class UnitTestClient_C : public edm::global::EDAnalyzer<> {
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions FWCore/MessageService/test/UnitTestClient_G.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include <cmath>
#include <iomanip>
#include <iostream>

#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 <iomanip>
#include <iostream>

namespace edmtest {

class UnitTestClient_G : public edm::global::EDAnalyzer<> {
Expand All @@ -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;

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
24 changes: 18 additions & 6 deletions FWCore/MessageService/test/unit_test_outputs/u6_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/warnings.log
Original file line number Diff line number Diff line change
@@ -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
Expand Down