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