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:
00036 #if MESSAGEFACILITY_HEX_VERSION < 0x20103 // v2_01_03 is s58, pre v2_01_03 is s50
00037 ELANSI(const fhicl::ParameterSet& pset);
00038 #else
00039 ELANSI(Parameters const& pset);
00040 #endif
00041
00042 virtual void routePayload(const std::ostringstream&, const ErrorObj&) override;
00043
00044 private:
00045 bool bellError_;
00046 bool blinkError_;
00047 std::string errorColor_;
00048 std::string warningColor_;
00049 std::string infoColor_;
00050 std::string debugColor_;
00051 };
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 #if MESSAGEFACILITY_HEX_VERSION < 0x20103 // v2_01_03 is s58, pre v2_01_03 is s50
00063 ELANSI::ELANSI(const fhicl::ParameterSet& pset)
00064 : ELdestination(pset)
00065 , bellError_(pset.get<bool>("bell_on_error", true))
00066 , blinkError_(pset.get<bool>("blink_error_messages", false))
00067 , errorColor_(pset.get<std::string>("error_ansi_color", "\033[1m\033[91m"))
00068 , warningColor_(pset.get<std::string>("warning_ansi_color", "\033[1m\033[93m"))
00069 , infoColor_(pset.get<std::string>("info_ansi_color", "\033[92m"))
00070 , debugColor_(pset.get<std::string>("debug_ansi_color", "\033[39m"))
00071 #else
00072 ELANSI::ELANSI(Parameters const& pset)
00073 : ELdestination(pset().elDestConfig())
00074 , bellError_(pset().bellOnError())
00075 , blinkError_(pset().blinkOnError())
00076 , errorColor_(pset().errorColor())
00077 , warningColor_(pset().warningColor())
00078 , infoColor_(pset().infoColor())
00079 , debugColor_(pset().debugColor())
00080 #endif
00081 {
00082
00083 }
00084
00085
00086
00087
00088 void ELANSI::routePayload(const std::ostringstream& oss, const ErrorObj& msg)
00089 {
00090 const auto& xid = msg.xid();
00091 auto level = xid.severity().getLevel();
00092
00093 switch (level)
00094 {
00095 case mf::ELseverityLevel::ELsev_success:
00096 case mf::ELseverityLevel::ELsev_zeroSeverity:
00097 case mf::ELseverityLevel::ELsev_unspecified:
00098 std::cout << debugColor_;
00099 break;
00100
00101 case mf::ELseverityLevel::ELsev_info:
00102 std::cout << infoColor_;
00103 break;
00104
00105 case mf::ELseverityLevel::ELsev_warning:
00106 std::cout << warningColor_;
00107 break;
00108
00109 case mf::ELseverityLevel::ELsev_error:
00110 case mf::ELseverityLevel::ELsev_severe:
00111 case mf::ELseverityLevel::ELsev_highestSeverity:
00112 if (bellError_) { std::cout << "\007"; }
00113 if (blinkError_) { std::cout << "\033[5m"; }
00114 std::cout << errorColor_;
00115 break;
00116
00117 default: break;
00118 }
00119 std::cout << oss.str();
00120 std::cout << "\033[0m" << std::endl;
00121 }
00122 }
00123
00124
00125
00126
00127
00128
00129
00130 #ifndef EXTERN_C_FUNC_DECLARE_START
00131 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
00132 #endif
00133
00134 EXTERN_C_FUNC_DECLARE_START
00135 auto makePlugin(const std::string&,
00136 const fhicl::ParameterSet& pset)
00137 {
00138 return std::make_unique<mfplugins::ELANSI>(pset);
00139 }
00140 }
00141
00142 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)