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