artdaq_mfextensions  v1_04_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 #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 {
21 public:
25  struct Config
26  {
28  fhicl::TableFragment<ELdestination::Config> elDestConfig;
30  fhicl::Atom<bool> bellOnError = fhicl::Atom<bool>{
31  fhicl::Name{"bell_on_error"}, fhicl::Comment{"Whether to ring the system bell on error messages"}, true};
33  fhicl::Atom<bool> blinkOnError =
34  fhicl::Atom<bool>{fhicl::Name{"blink_error_messages"},
35  fhicl::Comment{"Whether to print error messages with blinking text"}, false};
37  fhicl::Atom<std::string> errorColor = fhicl::Atom<std::string>{
38  fhicl::Name{"error_ansi_color"}, fhicl::Comment{"ANSI Color string for Error Messages"}, "\033[1m\033[91m"};
40  fhicl::Atom<std::string> warningColor = fhicl::Atom<std::string>{
41  fhicl::Name{"warning_ansi_color"}, fhicl::Comment{"ANSI Color string for Warning Messages"}, "\033[1m\033[93m"};
43  fhicl::Atom<std::string> infoColor = fhicl::Atom<std::string>{
44  fhicl::Name{"info_ansi_color"}, fhicl::Comment{"ANSI Color string for Info Messages"}, "\033[92m"};
46  fhicl::Atom<std::string> debugColor = fhicl::Atom<std::string>{
47  fhicl::Name{"debug_ansi_color"}, fhicl::Comment{"ANSI Color string for Debug Messages"}, "\033[39m"};
48  };
50  using Parameters = fhicl::WrappedTable<Config>;
51 
52 public:
57  ELANSI(Parameters const& pset);
58 
64  virtual void routePayload(const std::ostringstream& o, const ErrorObj& e) override;
65 
66 private:
67  bool bellError_;
68  bool blinkError_;
69  std::string errorColor_;
70  std::string warningColor_;
71  std::string infoColor_;
72  std::string debugColor_;
73 };
74 
75 // END DECLARATION
76 //======================================================================
77 // BEGIN IMPLEMENTATION
78 
79 //======================================================================
80 // ELANSI c'tor
81 //======================================================================
82 
84  : ELdestination(pset().elDestConfig()), bellError_(pset().bellOnError()), blinkError_(pset().blinkOnError()), errorColor_(pset().errorColor()), warningColor_(pset().warningColor()), infoColor_(pset().infoColor()), debugColor_(pset().debugColor())
85 {
86  // std::cout << "ANSI Plugin configured with ParameterSet: " << pset.to_string() << std::endl;
87 }
88 
89 //======================================================================
90 // Message router ( overriddes ELdestination::routePayload )
91 //======================================================================
92 void ELANSI::routePayload(const std::ostringstream& oss, const ErrorObj& msg)
93 {
94  const auto& xid = msg.xid();
95  auto level = xid.severity().getLevel();
96 
97  switch (level)
98  {
99  case mf::ELseverityLevel::ELsev_success:
100  case mf::ELseverityLevel::ELsev_zeroSeverity:
101  case mf::ELseverityLevel::ELsev_unspecified:
102  std::cout << debugColor_;
103  break;
104 
105  case mf::ELseverityLevel::ELsev_info:
106  std::cout << infoColor_;
107  break;
108 
109  case mf::ELseverityLevel::ELsev_warning:
110  std::cout << warningColor_;
111  break;
112 
113  case mf::ELseverityLevel::ELsev_error:
114  case mf::ELseverityLevel::ELsev_severe:
115  case mf::ELseverityLevel::ELsev_highestSeverity:
116  if (bellError_)
117  {
118  std::cout << "\007";
119  }
120  if (blinkError_)
121  {
122  std::cout << "\033[5m";
123  }
124  std::cout << errorColor_;
125  break;
126 
127  default:
128  break;
129  }
130  std::cout << oss.str();
131  std::cout << "\033[0m" << std::endl;
132 }
133 } // end namespace mfplugins
134 
135 //======================================================================
136 //
137 // makePlugin function
138 //
139 //======================================================================
140 
141 #ifndef EXTERN_C_FUNC_DECLARE_START
142 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
143 #endif
144 
145 EXTERN_C_FUNC_DECLARE_START
146 auto makePlugin(const std::string&, const fhicl::ParameterSet& pset)
147 {
148  return std::make_unique<mfplugins::ELANSI>(pset);
149 }
150 }
151 
152 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
ELANSI(Parameters const &pset)
ELANSI Constructor
Configuration parameters for ELANSI.
virtual void routePayload(const std::ostringstream &o, const ErrorObj &e) override
Serialize a MessageFacility message to the output.
fhicl::Atom< std::string > debugColor
&quot;debug_ansi_color&quot; (Default: &quot;\033[39m&quot;): ANSI Color string for ErrDebugor Messages ...
fhicl::Atom< std::string > errorColor
&quot;error_ansi_color&quot; (Default: &quot;\033[1m\033[91m&quot;): ANSI Color string for Error Messages ...
fhicl::Atom< bool > bellOnError
&quot;bell_on_error&quot; (Default: true): Whether to ring the system bell on error messages ...
fhicl::TableFragment< ELdestination::Config > elDestConfig
ELdestination common config parameters.
Message Facility destination which colorizes the console output
fhicl::Atom< bool > blinkOnError
&quot;blink_error_messages&quot; (Default: false): Whether to print error messages with blinking text ...
fhicl::Atom< std::string > infoColor
&quot;info_ansi_color&quot; (Default: &quot;\033[92m&quot;): ANSI Color string for Info Messages
fhicl::Atom< std::string > warningColor
&quot;warning_ansi_color&quot; (Default: &quot;\033[1m\033[93m&quot;): ANSI Color string for Warning Messages ...
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation.