LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
mfplugins::ELsyslog Class Reference
Inheritance diagram for mfplugins::ELsyslog:
mf::service::ELdestination

Classes

struct  Config
 

Public Types

using Parameters = fhicl::WrappedTable< Config >
 
enum  flag_enum {
  NO_LINE_BREAKS, TIMESTAMP, MILLISECOND, MODULE,
  SUBROUTINE, TEXT, SOME_CONTEXT, SERIAL,
  FULL_CONTEXT, TIME_SEPARATE, EPILOGUE_SEPARATE, NFLAGS
}
 

Public Member Functions

 ELsyslog (Parameters const &pset)
 
void setThreshold (ELseverityLevel sv)
 
virtual void log (mf::ErrorObj &)
 
virtual void finish ()
 
virtual void summary ()
 

Protected Member Functions

std::string summarizeContext (std::string const &)
 
void emitToken (std::ostream &os, std::string const &s, bool nl=false, bool preambleMode=false)
 
std::string formSummary ()
 
bool skipMsg (ELextendedID const &)
 
void resetLimiters ()
 
void resetMsgCounters ()
 
virtual void flush ()
 

Protected Attributes

ELseverityLevel threshold_
 
MsgFormatSettings format_
 
int defaultLimit_
 
int defaultReportEvery_
 
int defaultTimespan_
 
std::map< std::string const, CategoryParamscategoryParams_
 
std::map< ELextendedID const, XidLimiterxidLimiters_
 
std::map< ELextendedID const, StatsCountstatsMap_
 
bool outputStatistics_ {false}
 
bool updatedStats_ {false}
 
bool reset_ {false}
 

Private Member Functions

void fillPrefix (ostringstream &, ErrorObj const &) override
 
void fillUsrMsg (ostringstream &, ErrorObj const &) override
 
void fillSuffix (ostringstream &, ErrorObj const &) override
 
void routePayload (ostringstream const &, ErrorObj const &) override
 
int syslogLevel (ELseverityLevel)
 

Detailed Description

Definition at line 28 of file syslog_mfPlugin.cc.

Member Typedef Documentation

Member Enumeration Documentation

Constructor & Destructor Documentation

mfplugins::ELsyslog::ELsyslog ( Parameters const &  pset)

Definition at line 48 of file syslog_mfPlugin.cc.

49  : ELdestination{pset().elDestConfig()}
50  {
51  openlog("MF", 0, LOG_LOCAL0);
52  }
ELdestination(Config const &)

Member Function Documentation

void mf::service::ELdestination::emitToken ( std::ostream &  os,
std::string const &  s,
bool  nl = false,
bool  preambleMode = false 
)
protectedinherited

Definition at line 390 of file ELdestination.cc.

References mf::service::ELdestination::charsOnLine_, mf::service::ELdestination::format_, art::detail::indent(), and mf::service::ELdestination::MsgFormatSettings::lineLength.

Referenced by mf::service::ELostreamOutput::ELostreamOutput(), mf::service::ELdestination::fillPrefix(), mf::service::ELdestination::fillSuffix(), and mf::service::ELdestination::fillUsrMsg().

394  {
395  string const indent(6, ' ');
396  if (s.empty()) {
397  if (nl) {
398  os << '\n';
399  charsOnLine_ = 0UL;
400  }
401  return;
402  }
403  if (!preambleMode) {
404  os << s;
405  return;
406  }
407  char const first = s[0];
408  char const second = (s.length() < 2) ? '\0' : s[1];
409  char const last = (s.length() < 2) ? '\0' : s[s.length() - 1];
410  char const last2 = (s.length() < 3) ? '\0' : s[s.length() - 2];
411  // checking -2 because the very last char is sometimes a ' '
412  // inserted by ErrorLog::operator<<
413  // Accounts for newline @ the beginning of the string
414  if (first == '\n' || (charsOnLine_ + s.length()) > format_.lineLength) {
415  charsOnLine_ = 0UL;
416  if (second != ' ') {
417  os << ' ';
418  ++charsOnLine_;
419  }
420  if (first == '\n') {
421  os << s.substr(1);
422  } else {
423  os << s;
424  }
425  } else {
426  os << s;
427  }
428  if ((last == '\n') || (last2 == '\n')) {
429  os << indent;
430  if (last != ' ') {
431  os << ' ';
432  }
433  charsOnLine_ = indent.length() + 1UL;
434  }
435  if (nl) {
436  os << '\n';
437  charsOnLine_ = 0UL;
438  } else {
439  charsOnLine_ += s.length();
440  }
441  }
std::string indent(std::size_t const i)
MsgFormatSettings format_
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:82
std::string nl(std::size_t i=1)
void mfplugins::ELsyslog::fillPrefix ( ostringstream &  oss,
ErrorObj const &  msg 
)
overrideprivatevirtual

Reimplemented from mf::service::ELdestination.

Definition at line 55 of file syslog_mfPlugin.cc.

References mf::service::ELdestination::format_, mf::GetIteration(), mf::ErrorObj::timestamp(), mf::service::ELdestination::MsgFormatSettings::timestamp(), and mf::ErrorObj::xid().

56  {
57  auto const& xid = msg.xid();
58  oss << format_.timestamp(msg.timestamp()) << '|' << xid.hostname() << '|'
59  << xid.hostaddr() << '|' << xid.severity().getName() << '|' << xid.id()
60  << '|' << xid.application() << '|' << xid.pid() << '|'
61  << mf::GetIteration() << '|' << xid.module() << '|';
62  }
string const & GetIteration()
MsgFormatSettings format_
void mfplugins::ELsyslog::fillSuffix ( ostringstream &  ,
ErrorObj const &   
)
overrideprivatevirtual

Reimplemented from mf::service::ELdestination.

Definition at line 75 of file syslog_mfPlugin.cc.

76  {}
void mfplugins::ELsyslog::fillUsrMsg ( ostringstream &  oss,
ErrorObj const &  msg 
)
overrideprivatevirtual

Reimplemented from mf::service::ELdestination.

Definition at line 65 of file syslog_mfPlugin.cc.

References mf::service::ELdestination::fillUsrMsg().

66  {
67  ostringstream buf;
68  ELdestination::fillUsrMsg(buf, msg);
69  string const& usrMsg =
70  !buf.str().compare(0, 1, "\n") ? buf.str().erase(0, 1) : buf.str();
71  oss << usrMsg;
72  }
virtual void fillUsrMsg(std::ostringstream &, mf::ErrorObj const &msg)
void mf::service::ELdestination::finish ( )
virtualinherited

Definition at line 703 of file ELdestination.cc.

704  {}
void mf::service::ELdestination::flush ( )
protectedvirtualinherited

Definition at line 715 of file ELdestination.cc.

References mf::ELwarning, and mf::service::ELdestination::log().

716  {
717  ErrorObj msg{ELwarning, "ELdestination"s};
718  msg << "Call to unimplemented flush()!";
719  log(msg);
720  }
virtual void log(mf::ErrorObj &)
ELslProxy< ELwarningGen > constexpr ELwarning
string mf::service::ELdestination::formSummary ( )
protectedinherited

Definition at line 576 of file ELdestination.cc.

References art::left(), n, mf::ELseverityLevel::nLevels, art::right(), and mf::service::ELdestination::statsMap_.

Referenced by mf::service::ELstatistics::summary(), and mf::service::ELdestination::summary().

577  {
578  ostringstream s;
579  int n{};
580  // ----- Summary part I:
581  //
582  bool ftnote{false};
583  struct part3 {
584  long n{}, t{};
586  for (auto const& [xid, count] : statsMap_) {
587  auto const& cat = xid.id();
588  // ----- Emit new part I header, if needed:
589  //
590  if (n == 0) {
591  s << "\n";
592  s << " type category sev module subroutine "
593  " count total\n"
594  << " ---- -------------------- -- ---------------- "
595  "---------------- ----- -----\n";
596  }
597  // ----- Emit detailed message information:
598  //
599  s
600  // Type
601  << right << setw(5) << ++n
602  << ' '
603  // Category
604  << left << setw(20) << cat.substr(0, 20)
605  << ' '
606  // Severity
607  << left << setw(2) << xid.severity().getSymbol()
608  << ' '
609  // Module
610  << left << setw(16) << xid.module().substr(0, 16)
611  << ' '
612  // Subroutine
613  << left << setw(16)
614  << xid.subroutine().substr(0, 16)
615  // Count
616  << right << setw(7) << count.n_ << left << setw(1)
617  << (count.ignoredFlag_ ? '*' : ' ')
618  // Total Count
619  << right << setw(8) << count.aggregateN_ << '\n';
620  ftnote = ftnote || count.ignoredFlag_;
621  // ----- Obtain information for Part III, below:
622  //
623  p3[xid.severity().getLevel()].n += count.n_;
624  p3[xid.severity().getLevel()].t += count.aggregateN_;
625  }
626  // ----- Provide footnote to part I, if needed:
627  //
628  if (ftnote) {
629  s << "\n* Some occurrences of this message were suppressed in all "
630  "logs, due to limits.\n";
631  }
632  // ----- Summary part II:
633  //
634  n = 0;
635  for (auto const& pr : statsMap_) {
636  auto const& xid = pr.first;
637  auto const& count = pr.second;
638  string const& cat = xid.id();
639  if (n == 0) {
640  s << '\n'
641  << " type category Examples: run/evt run/evt "
642  "run/evt\n"
643  << " ---- -------------------- ---------------- ---------------- "
644  "----------------\n";
645  }
646  s
647  // Type
648  << right << setw(5) << ++n
649  << ' '
650  // Category
651  << left << setw(20) << cat
652  << ' '
653  // Context1
654  << left << setw(16) << count.context1_.c_str()
655  << ' '
656  // Context2
657  << left << setw(16) << count.context2_.c_str()
658  << ' '
659  // ContextLast
660  << count.contextLast_ << '\n';
661  }
662  // ----- Summary part III:
663  //
664  s << '\n'
665  << "Severity # Occurrences Total Occurrences\n"
666  << "-------- ------------- -----------------\n";
667  for (int k = 0; k < ELseverityLevel::nLevels; ++k) {
668  if (p3[k].n != 0 || p3[k].t != 0) {
669  s
670  // Severity
671  << left << setw(8)
672  << ELseverityLevel{ELseverityLevel::ELsev_(k)}.getName().c_str()
673  // Occurrences
674  << right << setw(17)
675  << p3[k].n
676  // Total Occurrences
677  << right << setw(20) << p3[k].t << '\n';
678  }
679  }
680  return s.str();
681  }
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:102
std::map< ELextendedID const, StatsCount > statsMap_
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
Char_t n[5]
void mf::service::ELdestination::log ( mf::ErrorObj msg)
virtualinherited

Reimplemented in mf::service::ELstatistics.

Definition at line 547 of file ELdestination.cc.

References mf::ErrorObj::context(), mf::service::ELdestination::fillPrefix(), mf::service::ELdestination::fillSuffix(), mf::service::ELdestination::fillUsrMsg(), mf::service::ELdestination::outputStatistics_, mf::service::ELdestination::routePayload(), mf::ErrorObj::setReactedTo(), mf::ELextendedID::severity(), mf::service::ELdestination::skipMsg(), mf::service::ELdestination::statsMap_, mf::service::ELdestination::summarizeContext(), mf::service::ELdestination::threshold_, mf::service::ELdestination::updatedStats_, and mf::ErrorObj::xid().

Referenced by mf::service::ELdestination::flush().

548  {
549  if (msg.xid().severity() < threshold_) {
550  if (outputStatistics_) {
551  statsMap_[msg.xid()].add(summarizeContext(msg.context()), false);
552  updatedStats_ = true;
553  }
554  return;
555  }
556  if (skipMsg(msg.xid())) {
557  if (outputStatistics_) {
558  statsMap_[msg.xid()].add(summarizeContext(msg.context()), false);
559  updatedStats_ = true;
560  }
561  return;
562  }
563  msg.setReactedTo(true);
564  ostringstream payload;
565  fillPrefix(payload, msg);
566  fillUsrMsg(payload, msg);
567  fillSuffix(payload, msg);
568  routePayload(payload, msg);
569  if (outputStatistics_) {
570  statsMap_[msg.xid()].add(summarizeContext(msg.context()), true);
571  updatedStats_ = true;
572  }
573  }
ELseverityLevel severity() const
Definition: ELextendedID.cc:33
virtual void fillUsrMsg(std::ostringstream &, mf::ErrorObj const &msg)
bool skipMsg(ELextendedID const &)
virtual void fillSuffix(std::ostringstream &, mf::ErrorObj const &msg)
std::string const & context() const
Definition: ErrorObj.cc:98
std::map< ELextendedID const, StatsCount > statsMap_
std::string summarizeContext(std::string const &)
ELextendedID const & xid() const
Definition: ErrorObj.cc:61
ELseverityLevel threshold_
virtual void fillPrefix(std::ostringstream &, mf::ErrorObj const &msg)
virtual void setReactedTo(bool)
Definition: ErrorObj.cc:177
virtual void routePayload(std::ostringstream const &, mf::ErrorObj const &msg)
void mf::service::ELdestination::resetLimiters ( )
protectedinherited

Definition at line 819 of file ELdestination.cc.

References mf::service::ELdestination::xidLimiters_.

Referenced by mf::service::ELstatistics::summary(), and mf::service::ELdestination::summary().

820  {
821  xidLimiters_.clear();
822  }
std::map< ELextendedID const, XidLimiter > xidLimiters_
void mf::service::ELdestination::resetMsgCounters ( )
protectedinherited

Definition at line 813 of file ELdestination.cc.

References mf::service::ELdestination::statsMap_.

Referenced by mf::service::ELstatistics::summary(), and mf::service::ELdestination::summary().

814  {
815  statsMap_.clear();
816  }
std::map< ELextendedID const, StatsCount > statsMap_
void mfplugins::ELsyslog::routePayload ( ostringstream const &  oss,
ErrorObj const &  msg 
)
overrideprivatevirtual

Reimplemented from mf::service::ELdestination.

Definition at line 79 of file syslog_mfPlugin.cc.

References mf::ELextendedID::severity(), syslogLevel(), and mf::ErrorObj::xid().

80  {
81  int const severity = syslogLevel(msg.xid().severity());
82  syslog(severity, "%s", oss.str().data());
83  }
int syslogLevel(ELseverityLevel)
void mf::service::ELdestination::setThreshold ( ELseverityLevel  sv)
inherited

Definition at line 709 of file ELdestination.cc.

References mf::service::ELdestination::threshold_.

710  {
711  threshold_ = sv;
712  }
ELseverityLevel threshold_
bool mf::service::ELdestination::skipMsg ( ELextendedID const &  xid)
protectedinherited

Definition at line 726 of file ELdestination.cc.

References mf::service::ELdestination::categoryParams_, mf::service::ELdestination::defaultLimit_, mf::service::ELdestination::defaultReportEvery_, mf::service::ELdestination::defaultTimespan_, mf::ELextendedID::id(), mf::service::ELdestination::XidLimiter::limit_, mf::service::ELdestination::XidLimiter::msgCount_, mf::service::ELdestination::XidLimiter::previousTimestamp_, r, mf::service::ELdestination::XidLimiter::reportEvery_, mf::service::ELdestination::XidLimiter::skippedMsgCount_, mf::service::ELdestination::XidLimiter::timespan_, and mf::service::ELdestination::xidLimiters_.

Referenced by mf::service::ELstatistics::log(), and mf::service::ELdestination::log().

727  {
728  auto xl_iter = xidLimiters_.find(xid);
729  if (xl_iter == xidLimiters_.end()) {
730  // Need to create and initialize a limiter for this xid.
731  int limit{defaultLimit_};
732  int reportEvery{defaultReportEvery_};
733  int timespan{defaultTimespan_};
734  // Does the category of this xid have explicit limits?
735  auto cp_iter = categoryParams_.find(xid.id());
736  if (cp_iter != categoryParams_.end()) {
737  // Category limits found, use them.
738  auto const& cp = cp_iter->second;
739  limit = (cp.limit_ < 0) ? defaultLimit_ : cp.limit_;
740  reportEvery =
741  (cp.reportEvery_ < 0) ? defaultReportEvery_ : cp.reportEvery_;
742  timespan = (cp.timespan_ < 0) ? defaultTimespan_ : cp.timespan_;
743  }
744  XidLimiter& limiter = xidLimiters_[xid];
745  limiter.previousTimestamp_ = time(0);
746  limiter.msgCount_ = 0;
747  limiter.skippedMsgCount_ =
748  reportEvery - 1; // So that the FIRST of the prescaled messages emerges
749  limiter.limit_ = limit;
750  limiter.reportEvery_ = reportEvery;
751  limiter.timespan_ = timespan;
752  xl_iter = xidLimiters_.find(xid);
753  }
754  XidLimiter& limiter = xl_iter->second;
755  time_t now = time(0);
756  // Has it been so long that we should restart counting toward the limit?
757  if ((limiter.timespan_ >= 0) &&
758  (difftime(now, limiter.previousTimestamp_) >= limiter.timespan_)) {
759  limiter.msgCount_ = 0;
760  if (limiter.reportEvery_ > 0) {
761  // So this message will be reacted to
762  limiter.skippedMsgCount_ = limiter.reportEvery_ - 1;
763  } else {
764  limiter.skippedMsgCount_ = 0;
765  }
766  }
767  limiter.previousTimestamp_ = now;
768  ++limiter.msgCount_;
769  ++limiter.skippedMsgCount_;
770  if (limiter.skippedMsgCount_ < limiter.reportEvery_) {
771  // Skip message.
772  return true;
773  }
774  if (limiter.limit_ == 0) {
775  // Zero limit - never react to this
776  // Skip message.
777  return true;
778  }
779  if ((limiter.limit_ < 0) || (limiter.msgCount_ <= limiter.limit_)) {
780  limiter.skippedMsgCount_ = 0;
781  // Accept message.
782  return false;
783  }
784  // Now we are over the limit - have we exceeded limit by 2^N * limit?
785  long diff = limiter.msgCount_ - limiter.limit_;
786  long r = diff / limiter.limit_;
787  if (r * limiter.limit_ != diff) {
788  // Not a multiple of limit - don't react
789  // Skip message.
790  return true;
791  }
792  if (r == 1) {
793  // Exactly twice limit - react
794  limiter.skippedMsgCount_ = 0;
795  // Accept message.
796  return false;
797  }
798  while (r > 1) {
799  if ((r & 1) != 0) {
800  // Not 2**msgCount_ times limit - don't react
801  // Skip message.
802  return true;
803  }
804  r >>= 1;
805  }
806  // If you never get an odd number till one, r is 2**msgCount_ so react
807  limiter.skippedMsgCount_ = 0;
808  // Accept message.
809  return false;
810  }
TRandom r
Definition: spectrum.C:23
std::map< std::string const, CategoryParams > categoryParams_
std::map< ELextendedID const, XidLimiter > xidLimiters_
string mf::service::ELdestination::summarizeContext ( std::string const &  )
protectedinherited

Definition at line 360 of file ELdestination.cc.

Referenced by mf::service::ELstatistics::log(), and mf::service::ELdestination::log().

361  {
362  if (c.substr(0, 4) != "Run:") {
363  return c;
364  }
365  istringstream is(c);
366  string runWord;
367  int run;
368  is >> runWord >> run;
369  if (!is) {
370  return c;
371  }
372  if (runWord != "Run:") {
373  return c;
374  }
375  string eventWord;
376  int event;
377  is >> eventWord >> event;
378  if (!is) {
379  return c;
380  }
381  if (eventWord != "Event:") {
382  return c;
383  }
384  ostringstream os;
385  os << run << "/" << event;
386  return os.str();
387  }
Event finding and building.
void mf::service::ELdestination::summary ( )
virtualinherited

Reimplemented in mf::service::ELstatistics.

Definition at line 686 of file ELdestination.cc.

References mf::ELzeroSeverity, mf::service::ELdestination::formSummary(), mf::service::ELdestination::outputStatistics_, mf::service::ELdestination::reset_, mf::service::ELdestination::resetLimiters(), mf::service::ELdestination::resetMsgCounters(), mf::service::ELdestination::routePayload(), and mf::service::ELdestination::updatedStats_.

687  {
689  ostringstream payload;
690  payload << "\n=============================================\n\n"
691  << "MessageLogger Summary\n"
692  << formSummary();
693  routePayload(payload, ErrorObj{ELzeroSeverity, "No ostream"s});
694  }
695  updatedStats_ = false;
696  if (reset_) {
698  resetLimiters();
699  }
700  }
ELslProxy< ELzeroSeverityGen > constexpr ELzeroSeverity
virtual void routePayload(std::ostringstream const &, mf::ErrorObj const &msg)
int mfplugins::ELsyslog::syslogLevel ( ELseverityLevel  severity)
private

Definition at line 86 of file syslog_mfPlugin.cc.

References mf::ELseverityLevel::ELsev_error, mf::ELseverityLevel::ELsev_info, mf::ELseverityLevel::ELsev_severe, mf::ELseverityLevel::ELsev_success, mf::ELseverityLevel::ELsev_warning, mf::ELseverityLevel::getLevel(), and mf::errors::LogicError.

Referenced by routePayload().

87  {
88  // Following syslog levels not used:
89  // LOG_EMERG ; //0
90  // LOG_ALERT ; //1
91  // LOG_NOTICE ; //5
92  switch (severity.getLevel()) { // Used by:
94  return LOG_CRIT; // LogAbsolute, LogSystem
96  return LOG_ERR; // LogError, LogProblem
98  return LOG_WARNING; // LogPrint, LogWarning
100  return LOG_INFO; // LogInfo, LogVerbatim
102  return LOG_DEBUG; // LogDebug, LogTrace
103  default:
105  << "ELseverityLevel: " << severity
106  << " not currently supported for syslog destination\n";
107  }
108  return -1;
109  }
cet::coded_exception< errors::error, detail::translate > Exception
Definition: exception.h:17

Member Data Documentation

std::map<std::string const, CategoryParams> mf::service::ELdestination::categoryParams_
protectedinherited
int mf::service::ELdestination::defaultLimit_
protectedinherited
int mf::service::ELdestination::defaultReportEvery_
protectedinherited
int mf::service::ELdestination::defaultTimespan_
protectedinherited
bool mf::service::ELdestination::outputStatistics_ {false}
protectedinherited
bool mf::service::ELdestination::reset_ {false}
protectedinherited
ELseverityLevel mf::service::ELdestination::threshold_
protectedinherited
bool mf::service::ELdestination::updatedStats_ {false}
protectedinherited
std::map<ELextendedID const, XidLimiter> mf::service::ELdestination::xidLimiters_
protectedinherited

The documentation for this class was generated from the following file: