00001 #include "cetlib/PluginTypeDeducer.h"
00002 #include "fhiclcpp/ParameterSet.h"
00003
00004 #include "messagefacility/MessageService/ELdestination.h"
00005 #include "messagefacility/Utilities/ELseverityLevel.h"
00006 #include "messagefacility/Utilities/exception.h"
00007
00008 #include "cetlib/compiler_macros.h"
00009 #include <iostream>
00010
00011 namespace mfplugins
00012 {
00013 using mf::service::ELdestination;
00014 using mf::ELseverityLevel;
00015 using mf::ErrorObj;
00016
00020 class ELANSI : public ELdestination
00021 {
00022 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
00023 struct Config
00024 {
00025 fhicl::TableFragment<ELdestination::Config> elDestConfig;
00026 fhicl::Atom<bool> bellOnError{ fhicl::Name{ "bell_on_error" },fhicl::Comment{ "Whether to ring the system bell on error messages" },true };
00027 fhicl::Atom<bool> blinkOnError{ fhicl::Name{ "blink_error_messages" },fhicl::Comment{ "Whether to print error messages with blinking text"},false };
00028 fhicl::Atom<std::string> errorColor{ fhicl::Name{ "error_ansi_color" },fhicl::Comment{ "ANSI Color string for Error Messages" }, "\033[1m\033[91m" };
00029 fhicl::Atom<std::string> warningColor{ fhicl::Name{ "warning_ansi_color" },fhicl::Comment{ "ANSI Color string for Warning Messages" }, "\033[1m\033[93m" };
00030 fhicl::Atom<std::string> infoColor{ fhicl::Name{ "info_ansi_color" },fhicl::Comment{ "ANSI Color string for Info Messages" }, "\033[92m" };
00031 fhicl::Atom<std::string> debugColor{ fhicl::Name{ "debug_ansi_color" },fhicl::Comment{ "ANSI Color string for Debug Messages" }, "\033[39m" };
00032 };
00033 using Parameters = fhicl::WrappedTable<Config>;
00034 #endif
00035 public:
00040 #if MESSAGEFACILITY_HEX_VERSION < 0x20103 // v2_01_03 is s58, pre v2_01_03 is s50
00041 ELANSI(const fhicl::ParameterSet& pset);
00042 #else
00043 ELANSI(Parameters const& pset);
00044 #endif
00045
00051 virtual void routePayload(const std::ostringstream& o, const ErrorObj& e) override;
00052
00053 private:
00054 bool bellError_;
00055 bool blinkError_;
00056 std::string errorColor_;
00057 std::string warningColor_;
00058 std::string infoColor_;
00059 std::string debugColor_;
00060 };
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 #if MESSAGEFACILITY_HEX_VERSION < 0x20103 // v2_01_03 is s58, pre v2_01_03 is s50
00072 ELANSI::ELANSI(const fhicl::ParameterSet& pset)
00073 : ELdestination(pset)
00074 , bellError_(pset.get<bool>("bell_on_error", true))
00075 , blinkError_(pset.get<bool>("blink_error_messages", false))
00076 , errorColor_(pset.get<std::string>("error_ansi_color", "\033[1m\033[91m"))
00077 , warningColor_(pset.get<std::string>("warning_ansi_color", "\033[1m\033[93m"))
00078 , infoColor_(pset.get<std::string>("info_ansi_color", "\033[92m"))
00079 , debugColor_(pset.get<std::string>("debug_ansi_color", "\033[39m"))
00080 #else
00081 ELANSI::ELANSI(Parameters const& pset)
00082 : ELdestination(pset().elDestConfig())
00083 , bellError_(pset().bellOnError())
00084 , blinkError_(pset().blinkOnError())
00085 , errorColor_(pset().errorColor())
00086 , warningColor_(pset().warningColor())
00087 , infoColor_(pset().infoColor())
00088 , debugColor_(pset().debugColor())
00089 #endif
00090 {
00091
00092 }
00093
00094
00095
00096
00097 void ELANSI::routePayload(const std::ostringstream& oss, const ErrorObj& msg)
00098 {
00099 const auto& xid = msg.xid();
00100 auto level = xid.severity().getLevel();
00101
00102 switch (level)
00103 {
00104 case mf::ELseverityLevel::ELsev_success:
00105 case mf::ELseverityLevel::ELsev_zeroSeverity:
00106 case mf::ELseverityLevel::ELsev_unspecified:
00107 std::cout << debugColor_;
00108 break;
00109
00110 case mf::ELseverityLevel::ELsev_info:
00111 std::cout << infoColor_;
00112 break;
00113
00114 case mf::ELseverityLevel::ELsev_warning:
00115 std::cout << warningColor_;
00116 break;
00117
00118 case mf::ELseverityLevel::ELsev_error:
00119 case mf::ELseverityLevel::ELsev_severe:
00120 case mf::ELseverityLevel::ELsev_highestSeverity:
00121 if (bellError_) { std::cout << "\007"; }
00122 if (blinkError_) { std::cout << "\033[5m"; }
00123 std::cout << errorColor_;
00124 break;
00125
00126 default: break;
00127 }
00128 std::cout << oss.str();
00129 std::cout << "\033[0m" << std::endl;
00130 }
00131 }
00132
00133
00134
00135
00136
00137
00138
00139 #ifndef EXTERN_C_FUNC_DECLARE_START
00140 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
00141 #endif
00142
00143 EXTERN_C_FUNC_DECLARE_START
00144 auto makePlugin(const std::string&,
00145 const fhicl::ParameterSet& pset)
00146 {
00147 return std::make_unique<mfplugins::ELANSI>(pset);
00148 }
00149 }
00150
00151 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)