artdaq_mfextensions  v1_02_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 <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  public:
29 
30  ELANSI(const fhicl::ParameterSet& pset);
31 
32 # if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
33  virtual void routePayload(const std::ostringstream&, const ErrorObj&) override;
34 # else
35  virtual void routePayload(const std::ostringstream&, const ErrorObj&, const ELcontextSupplier&) override;
36 # endif
37 
38  private:
39  bool bellError_;
40  bool blinkError_;
41  std::string errorColor_;
42  std::string warningColor_;
43  std::string infoColor_;
44  std::string debugColor_;
45  };
46 
47  // END DECLARATION
48  //======================================================================
49  // BEGIN IMPLEMENTATION
50 
51 
52  //======================================================================
53  // ELANSI c'tor
54  //======================================================================
55 
56  ELANSI::ELANSI(const fhicl::ParameterSet& pset)
57  : ELdestination(pset)
58  , bellError_(pset.get<bool>("bell_on_error", true))
59  , blinkError_(pset.get<bool>("blink_error_messages", false))
60  , errorColor_(pset.get<std::string>("error_ansi_color", "\033[1m\033[91m"))
61  , warningColor_(pset.get<std::string>("warning_ansi_color", "\033[1m\033[93m"))
62  , infoColor_(pset.get<std::string>("info_ansi_color", "\033[92m"))
63  , debugColor_(pset.get<std::string>("debug_ansi_color", "\033[39m"))
64  {
65  //std::cout << "ANSI Plugin configured with ParameterSet: " << pset.to_string() << std::endl;
66  }
67 
68  //======================================================================
69  // Message router ( overriddes ELdestination::routePayload )
70  //======================================================================
71  void ELANSI::routePayload(const std::ostringstream& oss, const ErrorObj& msg
72 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
73  , ELcontextSupplier const&
74 # endif
75  )
76  {
77  const auto& xid = msg.xid();
78 # if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
79  auto level = xid.severity().getLevel();
80 # else
81  auto level = xid.severity.getLevel();
82 # endif
83 
84  switch (level)
85  {
86 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
87  case mf::ELseverityLevel::ELsev_incidental:
88 # endif
89  case mf::ELseverityLevel::ELsev_success:
90  case mf::ELseverityLevel::ELsev_zeroSeverity:
91  case mf::ELseverityLevel::ELsev_unspecified:
92  std::cout << debugColor_;
93  break;
94 
95  case mf::ELseverityLevel::ELsev_info:
96  std::cout << infoColor_;
97  break;
98 
99  case mf::ELseverityLevel::ELsev_warning:
100 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
101  case mf::ELseverityLevel::ELsev_warning2:
102 # endif
103  std::cout << warningColor_;
104  break;
105 
106  case mf::ELseverityLevel::ELsev_error:
107 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
108  case mf::ELseverityLevel::ELsev_error2:
109  case mf::ELseverityLevel::ELsev_next:
110  case mf::ELseverityLevel::ELsev_severe2:
111  case mf::ELseverityLevel::ELsev_abort:
112  case mf::ELseverityLevel::ELsev_fatal:
113 # endif
114  case mf::ELseverityLevel::ELsev_severe:
115  case mf::ELseverityLevel::ELsev_highestSeverity:
116  if (bellError_) { std::cout << "\007"; }
117  if (blinkError_) { std::cout << "\033[5m"; }
118  std::cout << errorColor_;
119  break;
120 
121  default: break;
122  }
123  std::cout << oss.str();
124  std::cout << "\033[0m" << std::endl;
125  }
126 } // end namespace mfplugins
127 
128 //======================================================================
129 //
130 // makePlugin function
131 //
132 //======================================================================
133 
134 extern "C"
135 {
136  auto makePlugin(const std::string&,
137  const fhicl::ParameterSet& pset)
138  {
139  return std::make_unique<mfplugins::ELANSI>(pset);
140  }
141 }
142 
143 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
Message Facility destination which colorizes the console output