artdaq_mfextensions  v1_03_03
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 #include "messagefacility/Utilities/exception.h"
7 //#include "messagefacility/Utilities/formatTime.h"
8 #include <iostream>
9 #include "cetlib/compiler_macros.h"
10 
11 namespace mfplugins {
12 using mf::ELseverityLevel;
13 using mf::ErrorObj;
14 using mf::service::ELdestination;
15 
19 class ELANSI : public ELdestination {
20  struct Config {
21  fhicl::TableFragment<ELdestination::Config> elDestConfig;
22  fhicl::Atom<bool> bellOnError{fhicl::Name{"bell_on_error"},
23  fhicl::Comment{"Whether to ring the system bell on error messages"}, true};
24  fhicl::Atom<bool> blinkOnError{fhicl::Name{"blink_error_messages"},
25  fhicl::Comment{"Whether to print error messages with blinking text"}, false};
26  fhicl::Atom<std::string> errorColor{fhicl::Name{"error_ansi_color"},
27  fhicl::Comment{"ANSI Color string for Error Messages"}, "\033[1m\033[91m"};
28  fhicl::Atom<std::string> warningColor{fhicl::Name{"warning_ansi_color"},
29  fhicl::Comment{"ANSI Color string for Warning Messages"}, "\033[1m\033[93m"};
30  fhicl::Atom<std::string> infoColor{fhicl::Name{"info_ansi_color"},
31  fhicl::Comment{"ANSI Color string for Info Messages"}, "\033[92m"};
32  fhicl::Atom<std::string> debugColor{fhicl::Name{"debug_ansi_color"},
33  fhicl::Comment{"ANSI Color string for Debug Messages"}, "\033[39m"};
34  };
35  using Parameters = fhicl::WrappedTable<Config>;
36 
37  public:
42  ELANSI(Parameters const& pset);
43 
49  virtual void routePayload(const std::ostringstream& o, const ErrorObj& e) override;
50 
51  private:
52  bool bellError_;
53  bool blinkError_;
54  std::string errorColor_;
55  std::string warningColor_;
56  std::string infoColor_;
57  std::string debugColor_;
58 };
59 
60 // END DECLARATION
61 //======================================================================
62 // BEGIN IMPLEMENTATION
63 
64 //======================================================================
65 // ELANSI c'tor
66 //======================================================================
67 
68 ELANSI::ELANSI(Parameters const& pset)
69  : ELdestination(pset().elDestConfig()),
70  bellError_(pset().bellOnError()),
71  blinkError_(pset().blinkOnError()),
72  errorColor_(pset().errorColor()),
73  warningColor_(pset().warningColor()),
74  infoColor_(pset().infoColor()),
75  debugColor_(pset().debugColor()) {
76  // std::cout << "ANSI Plugin configured with ParameterSet: " << pset.to_string() << std::endl;
77 }
78 
79 //======================================================================
80 // Message router ( overriddes ELdestination::routePayload )
81 //======================================================================
82 void ELANSI::routePayload(const std::ostringstream& oss, const ErrorObj& msg) {
83  const auto& xid = msg.xid();
84  auto level = xid.severity().getLevel();
85 
86  switch (level) {
87  case mf::ELseverityLevel::ELsev_success:
88  case mf::ELseverityLevel::ELsev_zeroSeverity:
89  case mf::ELseverityLevel::ELsev_unspecified:
90  std::cout << debugColor_;
91  break;
92 
93  case mf::ELseverityLevel::ELsev_info:
94  std::cout << infoColor_;
95  break;
96 
97  case mf::ELseverityLevel::ELsev_warning:
98  std::cout << warningColor_;
99  break;
100 
101  case mf::ELseverityLevel::ELsev_error:
102  case mf::ELseverityLevel::ELsev_severe:
103  case mf::ELseverityLevel::ELsev_highestSeverity:
104  if (bellError_) {
105  std::cout << "\007";
106  }
107  if (blinkError_) {
108  std::cout << "\033[5m";
109  }
110  std::cout << errorColor_;
111  break;
112 
113  default:
114  break;
115  }
116  std::cout << oss.str();
117  std::cout << "\033[0m" << std::endl;
118 }
119 } // end namespace mfplugins
120 
121 //======================================================================
122 //
123 // makePlugin function
124 //
125 //======================================================================
126 
127 #ifndef EXTERN_C_FUNC_DECLARE_START
128 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
129 #endif
130 
131 EXTERN_C_FUNC_DECLARE_START
132 auto makePlugin(const std::string&, const fhicl::ParameterSet& pset) {
133  return std::make_unique<mfplugins::ELANSI>(pset);
134 }
135 }
136 
137 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
ELANSI(Parameters const &pset)
ELANSI Constructor
virtual void routePayload(const std::ostringstream &o, const ErrorObj &e) override
Serialize a MessageFacility message to the output.
Message Facility destination which colorizes the console output