LCOV - code coverage report
Current view: top level - artdaq/ArtModules - ArtdaqMetric_mfPlugin.cc (source / functions) Coverage Total Hit
Test: artdaq.info.cleaned Lines: 0.0 % 69 0
Test Date: 2025-09-04 00:45:34 Functions: 0.0 % 8 0

            Line data    Source code
       1              : #include "artdaq/DAQdata/Globals.hh"
       2              : 
       3              : #include "cetlib/PluginTypeDeducer.h"
       4              : #include "cetlib/ProvideMakePluginMacros.h"
       5              : #include "fhiclcpp/ParameterSet.h"
       6              : #include "fhiclcpp/types/ConfigurationTable.h"
       7              : 
       8              : #include "cetlib/compiler_macros.h"
       9              : #include "messagefacility/MessageService/ELdestination.h"
      10              : #include "messagefacility/Utilities/ELseverityLevel.h"
      11              : #include "messagefacility/Utilities/exception.h"
      12              : 
      13              : #define TRACE_NAME "ArtdaqMetric"
      14              : 
      15              : namespace mfplugins {
      16              : using mf::ELseverityLevel;
      17              : using mf::ErrorObj;
      18              : using mf::service::ELdestination;
      19              : 
      20              : /// <summary>
      21              : /// Message Facility destination which logs messages to a TRACE buffer
      22              : /// </summary>
      23              : class ELArtdaqMetric : public ELdestination
      24              : {
      25              : public:
      26              :         /**
      27              :          * \brief Configuration Parameters for ELArtdaqMetric
      28              :          */
      29              :         struct Config
      30              :         {
      31              :                 /// ELDestination common parameters
      32              :                 fhicl::TableFragment<ELdestination::Config> elDestConfig;
      33              :                 /// "showDebug" (Default: False): Send metrics for Debug messages
      34              :                 fhicl::Atom<bool> showDebug =
      35              :                     fhicl::Atom<bool>{fhicl::Name{"showDebug"}, fhicl::Comment{"Send Metrics for Debug messages"}, false};
      36              :                 /// "showInfo" (Default: False): Send metrics for Info messages
      37              :                 fhicl::Atom<bool> showInfo =
      38              :                     fhicl::Atom<bool>{fhicl::Name{"showInfo"}, fhicl::Comment{"Send Metrics for Info messages"}, false};
      39              :                 /// "showWarning" (Default: true): Send metrics for Warning messages
      40              :                 fhicl::Atom<bool> showWarning =
      41              :                     fhicl::Atom<bool>{fhicl::Name{"showWarning"}, fhicl::Comment{"Send Metrics for Warning messages"}, true};
      42              :                 /// "showError" (Default: true): Send metrics for Error messages
      43              :                 fhicl::Atom<bool> showError =
      44              :                     fhicl::Atom<bool>{fhicl::Name{"showError"}, fhicl::Comment{"Send Metrics for Error messages"}, true};
      45              :                 /// "messageLength" (Default: 40): Number of characters to use for metric title
      46              :                 fhicl::Atom<size_t> messageLength = fhicl::Atom<size_t>(fhicl::Name{"messageLength"}, fhicl::Comment{"Maximum length of metric titles (0 for unlimited)"}, 40);
      47              :                 /// "metricLevelOffset" (Default: 10): Offset for Metric Levels (+0: summary rates, +1 errors, ...)
      48              :                 fhicl::Atom<size_t> metricLevelOffset = fhicl::Atom<size_t>(fhicl::Name{"metricLevelOffset"}, fhicl::Comment{"Offset for Metric Levels (+0: summary rates, +1 errors, ...)"}, 10);
      49              :         };
      50              :         /// Used for ParameterSet validation
      51              :         using Parameters = fhicl::WrappedTable<Config>;
      52              : 
      53              : public:
      54              :         /// <summary>
      55              :         /// ELArtdaqMetric Constructor
      56              :         /// </summary>
      57              :         /// <param name="pset">ParameterSet used to configure ELArtdaqMetric</param>
      58              :         ELArtdaqMetric(Parameters const& pset);
      59              : 
      60              :         /**
      61              :          * \brief Fill the "Prefix" portion of the message
      62              :          * \param o Output stringstream
      63              :          * \param msg MessageFacility object containing header information
      64              :          */
      65              :         void fillPrefix(std::ostringstream& o, const ErrorObj& msg) override;
      66              : 
      67              :         /**
      68              :          * \brief Fill the "User Message" portion of the message
      69              :          * \param o Output stringstream
      70              :          * \param msg MessageFacility object containing header information
      71              :          */
      72              :         void fillUsrMsg(std::ostringstream& o, const ErrorObj& msg) override;
      73              : 
      74              :         /**
      75              :          * \brief Fill the "Suffix" portion of the message (Unused)
      76              :          */
      77            0 :         void fillSuffix(std::ostringstream& /*unused*/, const ErrorObj& /*msg*/) override {}
      78              : 
      79              :         /**
      80              :          * \brief Serialize a MessageFacility message to the output
      81              :          * \param o Stringstream object containing message data
      82              :          * \param msg MessageFacility object containing header information
      83              :          */
      84              :         void routePayload(const std::ostringstream& o, const ErrorObj& msg) override;
      85              : 
      86              : private:
      87              :         std::string makeMetricName_(const ErrorObj& msg);
      88              :         bool showDebug_{false};
      89              :         bool showInfo_{false};
      90              :         bool showWarning_{true};
      91              :         bool showError_{true};
      92              :         size_t messageLength_{20};
      93              :         size_t metricLevelOffset_{10};
      94              : };
      95              : 
      96              : // END DECLARATION
      97              : //======================================================================
      98              : // BEGIN IMPLEMENTATION
      99              : 
     100              : //======================================================================
     101              : // ELArtdaqMetric c'tor
     102              : //======================================================================
     103            0 : ELArtdaqMetric::ELArtdaqMetric(Parameters const& pset)
     104            0 :     : ELdestination(pset().elDestConfig())
     105            0 :     , showDebug_(pset().showDebug())
     106            0 :     , showInfo_(pset().showInfo())
     107            0 :     , showWarning_(pset().showWarning())
     108            0 :     , showError_(pset().showError())
     109            0 :     , messageLength_(pset().messageLength())
     110            0 :     , metricLevelOffset_(pset().metricLevelOffset())
     111              : {
     112            0 :         TLOG(TLVL_INFO) << "ELArtdaqMetric MessageLogger destination plugin initialized.";
     113            0 : }
     114              : 
     115              : //======================================================================
     116              : // Message prefix filler ( overriddes ELdestination::fillPrefix )
     117              : //======================================================================
     118            0 : void ELArtdaqMetric::fillPrefix(std::ostringstream&, const ErrorObj&)
     119              : {
     120            0 : }
     121              : 
     122              : //======================================================================
     123              : // Message filler ( overriddes ELdestination::fillUsrMsg )
     124              : //======================================================================
     125            0 : void ELArtdaqMetric::fillUsrMsg(std::ostringstream& oss, const ErrorObj& msg)
     126              : {
     127            0 :         std::ostringstream tmposs;
     128            0 :         ELdestination::fillUsrMsg(tmposs, msg);
     129              : 
     130              :         // remove "\n" if present
     131            0 :         std::string tmpStr = tmposs.str();
     132            0 :         auto cpos = tmpStr.find(':');
     133            0 :         if (cpos != std::string::npos)
     134              :         {
     135            0 :                 tmpStr.erase(0, cpos + 1);
     136              :         }
     137              : 
     138              :         // remove numbers
     139            0 :         std::string usrMsg;
     140            0 :         std::copy_if(tmpStr.begin(), tmpStr.end(),
     141              :                      std::back_inserter(usrMsg), isalpha);
     142              : 
     143            0 :         if (messageLength_ > 0 && usrMsg.size() > messageLength_)
     144              :         {
     145            0 :                 usrMsg.resize(messageLength_);
     146              :         }
     147              : 
     148            0 :         oss << usrMsg;
     149            0 : }
     150              : 
     151              : //======================================================================
     152              : // Message router ( overriddes ELdestination::routePayload )
     153              : //======================================================================
     154            0 : void ELArtdaqMetric::routePayload(const std::ostringstream& oss, const ErrorObj& msg)
     155              : {
     156            0 :         const auto& xid = msg.xid();
     157            0 :         auto message = oss.str();
     158              : 
     159            0 :         auto level = xid.severity().getLevel();
     160            0 :         int lvlNum = -1;
     161            0 :         bool sendMessageMetric = false;
     162              : 
     163            0 :         switch (level)
     164              :         {
     165            0 :                 case mf::ELseverityLevel::ELsev_success:
     166              :                 case mf::ELseverityLevel::ELsev_zeroSeverity:
     167              :                 case mf::ELseverityLevel::ELsev_unspecified:
     168            0 :                         sendMessageMetric = showDebug_;
     169            0 :                         lvlNum = 3;
     170            0 :                         break;
     171              : 
     172            0 :                 case mf::ELseverityLevel::ELsev_info:
     173            0 :                         sendMessageMetric = showInfo_;
     174            0 :                         lvlNum = 2;
     175            0 :                         break;
     176              : 
     177            0 :                 case mf::ELseverityLevel::ELsev_warning:
     178            0 :                         sendMessageMetric = showWarning_;
     179            0 :                         lvlNum = 1;
     180            0 :                         break;
     181            0 :                 default:
     182            0 :                         sendMessageMetric = showError_;
     183            0 :                         lvlNum = 0;
     184            0 :                         break;
     185              :         }
     186              : 
     187            0 :         if (metricMan)
     188              :         {
     189            0 :                 if (sendMessageMetric)
     190              :                 {
     191            0 :                         metricMan->sendMetric(message, 1, "messages", lvlNum + metricLevelOffset_ + 1, artdaq::MetricMode::Rate);
     192              :                 }
     193            0 :                 switch (lvlNum)
     194              :                 {
     195            0 :                         case 0:
     196            0 :                                 metricMan->sendMetric("Error Message Rate", 1, "messages", metricLevelOffset_, artdaq::MetricMode::Rate);
     197            0 :                                 break;
     198            0 :                         case 1:
     199            0 :                                 metricMan->sendMetric("Warning Message Rate", 1, "messages", metricLevelOffset_, artdaq::MetricMode::Rate);
     200            0 :                                 break;
     201            0 :                         case 2:
     202            0 :                                 metricMan->sendMetric("Info Message Rate", 1, "messages", metricLevelOffset_, artdaq::MetricMode::Rate);
     203            0 :                                 break;
     204            0 :                         case 3:
     205            0 :                                 metricMan->sendMetric("Debug Message Rate", 1, "messages", metricLevelOffset_, artdaq::MetricMode::Rate);
     206            0 :                                 break;
     207              :                 }
     208              :         }
     209            0 : }
     210              : }  // end namespace mfplugins
     211              : 
     212              : //======================================================================
     213              : //
     214              : // makePlugin function
     215              : //
     216              : //======================================================================
     217              : 
     218              : #ifndef EXTERN_C_FUNC_DECLARE_START
     219              : #define EXTERN_C_FUNC_DECLARE_START extern "C" {
     220              : #endif
     221              : 
     222              : EXTERN_C_FUNC_DECLARE_START
     223            0 : auto makePlugin(const std::string& /*unused*/, const fhicl::ParameterSet& pset)
     224              : {
     225            0 :         return std::make_unique<mfplugins::ELArtdaqMetric>(pset);
     226              : }
     227              : }
     228              : 
     229            0 : DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
        

Generated by: LCOV version 2.0-1