artdaq_mfextensions  v1_02_02
ANSI_mfPlugin.cc
1 #include "cetlib/PluginTypeDeducer.h"
2 #include "fhiclcpp/ParameterSet.h"
3 
4 #include "messagefacility/MessageService/ELdestination.h"
5 #include "messagefacility/Utilities/ELseverityLevel.h"
6 #if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
7 # include "messagefacility/MessageLogger/MessageDrop.h"
8 # include "messagefacility/MessageService/ELcontextSupplier.h"
9 #endif
10 #include "messagefacility/Utilities/exception.h"
11 #include "messagefacility/Utilities/formatTime.h"
12 #include <iostream>
13 
14 namespace mfplugins
15 {
16  using mf::service::ELdestination;
17  using mf::ELseverityLevel;
18 #if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
19  using mf::service::ELcontextSupplier;
20 #endif
21  using mf::ErrorObj;
22 
26  class ELANSI : public ELdestination
27  {
28 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
29  struct Config
30  {
31  fhicl::TableFragment<ELdestination::Config> elDestConfig;
32  fhicl::Atom<bool> bellOnError{ fhicl::Name{ "bell_on_error" },fhicl::Comment{ "Whether to ring the system bell on error messages" },true };
33  fhicl::Atom<bool> blinkOnError{ fhicl::Name{ "blink_error_messages" },fhicl::Comment{ "Whether to print error messages with blinking text"},false };
34  fhicl::Atom<std::string> errorColor{ fhicl::Name{ "error_ansi_color" },fhicl::Comment{ "ANSI Color string for Error Messages" }, "\033[1m\033[91m" };
35  fhicl::Atom<std::string> warningColor{ fhicl::Name{ "warning_ansi_color" },fhicl::Comment{ "ANSI Color string for Warning Messages" }, "\033[1m\033[93m" };
36  fhicl::Atom<std::string> infoColor{ fhicl::Name{ "info_ansi_color" },fhicl::Comment{ "ANSI Color string for Info Messages" }, "\033[92m" };
37  fhicl::Atom<std::string> debugColor{ fhicl::Name{ "debug_ansi_color" },fhicl::Comment{ "ANSI Color string for Debug Messages" }, "\033[39m" };
38  };
39  using Parameters = fhicl::WrappedTable<Config>;
40 #endif
41  public:
42 #if MESSAGEFACILITY_HEX_VERSION < 0x20103 // v2_01_03 is s58, pre v2_01_03 is s50
43  ELANSI(const fhicl::ParameterSet& pset);
44 #else
45  ELANSI(Parameters const& pset);
46 #endif
47 
48 # if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
49  virtual void routePayload(const std::ostringstream&, const ErrorObj&) override;
50 # else
51  virtual void routePayload(const std::ostringstream&, const ErrorObj&, const ELcontextSupplier&) override;
52 # endif
53 
54  private:
55  bool bellError_;
56  bool blinkError_;
57  std::string errorColor_;
58  std::string warningColor_;
59  std::string infoColor_;
60  std::string debugColor_;
61  };
62 
63  // END DECLARATION
64  //======================================================================
65  // BEGIN IMPLEMENTATION
66 
67 
68  //======================================================================
69  // ELANSI c'tor
70  //======================================================================
71 
72 #if MESSAGEFACILITY_HEX_VERSION < 0x20103 // v2_01_03 is s58, pre v2_01_03 is s50
73  ELANSI::ELANSI(const fhicl::ParameterSet& pset)
74  : ELdestination(pset)
75  , bellError_(pset.get<bool>("bell_on_error", true))
76  , blinkError_(pset.get<bool>("blink_error_messages", false))
77  , errorColor_(pset.get<std::string>("error_ansi_color", "\033[1m\033[91m"))
78  , warningColor_(pset.get<std::string>("warning_ansi_color", "\033[1m\033[93m"))
79  , infoColor_(pset.get<std::string>("info_ansi_color", "\033[92m"))
80  , debugColor_(pset.get<std::string>("debug_ansi_color", "\033[39m"))
81 #else
82  ELANSI::ELANSI(Parameters const& pset)
83  : ELdestination(pset().elDestConfig())
84  , bellError_(pset().bellOnError())
85  , blinkError_(pset().blinkOnError())
86  , errorColor_(pset().errorColor())
87  , warningColor_(pset().warningColor())
88  , infoColor_(pset().infoColor())
89  , debugColor_(pset().debugColor())
90 #endif
91  {
92  //std::cout << "ANSI Plugin configured with ParameterSet: " << pset.to_string() << std::endl;
93  }
94 
95  //======================================================================
96  // Message router ( overriddes ELdestination::routePayload )
97  //======================================================================
98  void ELANSI::routePayload(const std::ostringstream& oss, const ErrorObj& msg
99 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
100  , ELcontextSupplier const&
101 # endif
102  )
103  {
104  const auto& xid = msg.xid();
105 # if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
106  auto level = xid.severity().getLevel();
107 # else
108  auto level = xid.severity.getLevel();
109 # endif
110 
111  switch (level)
112  {
113 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
114  case mf::ELseverityLevel::ELsev_incidental:
115 # endif
116  case mf::ELseverityLevel::ELsev_success:
117  case mf::ELseverityLevel::ELsev_zeroSeverity:
118  case mf::ELseverityLevel::ELsev_unspecified:
119  std::cout << debugColor_;
120  break;
121 
122  case mf::ELseverityLevel::ELsev_info:
123  std::cout << infoColor_;
124  break;
125 
126  case mf::ELseverityLevel::ELsev_warning:
127 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
128  case mf::ELseverityLevel::ELsev_warning2:
129 # endif
130  std::cout << warningColor_;
131  break;
132 
133  case mf::ELseverityLevel::ELsev_error:
134 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
135  case mf::ELseverityLevel::ELsev_error2:
136  case mf::ELseverityLevel::ELsev_next:
137  case mf::ELseverityLevel::ELsev_severe2:
138  case mf::ELseverityLevel::ELsev_abort:
139  case mf::ELseverityLevel::ELsev_fatal:
140 # endif
141  case mf::ELseverityLevel::ELsev_severe:
142  case mf::ELseverityLevel::ELsev_highestSeverity:
143  if (bellError_) { std::cout << "\007"; }
144  if (blinkError_) { std::cout << "\033[5m"; }
145  std::cout << errorColor_;
146  break;
147 
148  default: break;
149  }
150  std::cout << oss.str();
151  std::cout << "\033[0m" << std::endl;
152  }
153 } // end namespace mfplugins
154 
155 //======================================================================
156 //
157 // makePlugin function
158 //
159 //======================================================================
160 
161 extern "C"
162 {
163  auto makePlugin(const std::string&,
164  const fhicl::ParameterSet& pset)
165  {
166  return std::make_unique<mfplugins::ELANSI>(pset);
167  }
168 }
169 
170 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
Message Facility destination which colorizes the console output