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