LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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 30 of file syslog_mfPlugin.cc.

Member Typedef Documentation

Member Enumeration Documentation

Constructor & Destructor Documentation

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

Definition at line 50 of file syslog_mfPlugin.cc.

51  : ELdestination{pset().elDestConfig()}
52  {
53  openlog("MF", 0, LOG_LOCAL0);
54  }
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 408 of file ELdestination.cc.

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

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

412  {
413  string indent(6, ' ');
414  if (s.empty()) {
415  if (nl) {
416  os << '\n';
417  charsOnLine_ = 0UL;
418  }
419  return;
420  }
421  if (!preambleMode) {
422  os << s;
423  return;
424  }
425  char const first = s[0];
426  char const second = (s.length() < 2) ? '\0' : s[1];
427  char const last = (s.length() < 2) ? '\0' : s[s.length() - 1];
428  char const last2 = (s.length() < 3) ? '\0' : s[s.length() - 2];
429  // checking -2 because the very last char is sometimes a ' '
430  // inserted by ErrorLog::operator<<
431  // Accounts for newline @ the beginning of the string
432  if (first == '\n' || (charsOnLine_ + s.length()) > format_.lineLength) {
433  charsOnLine_ = 0UL;
434  if (second != ' ') {
435  os << ' ';
436  ++charsOnLine_;
437  }
438  if (first == '\n') {
439  os << s.substr(1);
440  } else {
441  os << s;
442  }
443  } else {
444  os << s;
445  }
446  if ((last == '\n') || (last2 == '\n')) {
447  os << indent;
448  if (last != ' ') {
449  os << ' ';
450  }
451  charsOnLine_ = indent.length() + 1UL;
452  }
453  if (nl) {
454  os << '\n';
455  charsOnLine_ = 0UL;
456  } else {
457  charsOnLine_ += s.length();
458  }
459  }
Float_t s
Definition: plot.C:23
std::string indent(std::size_t const i)
MsgFormatSettings format_
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 57 of file syslog_mfPlugin.cc.

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

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

Reimplemented from mf::service::ELdestination.

Definition at line 77 of file syslog_mfPlugin.cc.

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

Reimplemented from mf::service::ELdestination.

Definition at line 67 of file syslog_mfPlugin.cc.

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

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

Definition at line 723 of file ELdestination.cc.

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

Definition at line 735 of file ELdestination.cc.

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

736  {
737  ErrorObj msg{ELwarning, "ELdestination"s};
738  msg << "Call to unimplemented flush()!";
739  log(msg);
740  }
Float_t s
Definition: plot.C:23
virtual void log(mf::ErrorObj &)
ELslProxy< ELwarningGen > constexpr ELwarning
string mf::service::ELdestination::formSummary ( )
protectedinherited

Definition at line 594 of file ELdestination.cc.

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

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

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

Reimplemented in mf::service::ELstatistics.

Definition at line 565 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().

566  {
567  if (msg.xid().severity() < threshold_) {
568  if (outputStatistics_) {
569  statsMap_[msg.xid()].add(summarizeContext(msg.context()), false);
570  updatedStats_ = true;
571  }
572  return;
573  }
574  if (skipMsg(msg.xid())) {
575  if (outputStatistics_) {
576  statsMap_[msg.xid()].add(summarizeContext(msg.context()), false);
577  updatedStats_ = true;
578  }
579  return;
580  }
581  msg.setReactedTo(true);
582  ostringstream payload;
583  fillPrefix(payload, msg);
584  fillUsrMsg(payload, msg);
585  fillSuffix(payload, msg);
586  routePayload(payload, msg);
587  if (outputStatistics_) {
588  statsMap_[msg.xid()].add(summarizeContext(msg.context()), true);
589  updatedStats_ = true;
590  }
591  }
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 840 of file ELdestination.cc.

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

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

841  {
842  xidLimiters_.clear();
843  }
std::map< ELextendedID const, XidLimiter > xidLimiters_
void mf::service::ELdestination::resetMsgCounters ( )
protectedinherited

Definition at line 834 of file ELdestination.cc.

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

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

835  {
836  statsMap_.clear();
837  }
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 81 of file syslog_mfPlugin.cc.

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

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

Definition at line 729 of file ELdestination.cc.

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

730  {
731  threshold_ = sv;
732  }
ELseverityLevel threshold_
bool mf::service::ELdestination::skipMsg ( ELextendedID const &  xid)
protectedinherited

Definition at line 746 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_, 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().

747  {
748  auto xl_iter = xidLimiters_.find(xid);
749  if (xl_iter == xidLimiters_.end()) {
750  // Need to create and initialize a limiter for this xid.
751  int limit{defaultLimit_};
752  int reportEvery{defaultReportEvery_};
753  int timespan{defaultTimespan_};
754  // Does the category of this xid have explicit limits?
755  auto cp_iter = categoryParams_.find(xid.id());
756  if (cp_iter != categoryParams_.end()) {
757  // Category limits found, use them.
758  auto const& cp = cp_iter->second;
759  limit = (cp.limit_ < 0) ? defaultLimit_ : cp.limit_;
760  reportEvery =
761  (cp.reportEvery_ < 0) ? defaultReportEvery_ : cp.reportEvery_;
762  timespan = (cp.timespan_ < 0) ? defaultTimespan_ : cp.timespan_;
763  }
764  XidLimiter& limiter = xidLimiters_[xid];
765  limiter.previousTimestamp_ = time(0);
766  limiter.msgCount_ = 0;
767  limiter.skippedMsgCount_ =
768  reportEvery -
769  1; // So that the FIRST of the prescaled messages emerges
770  limiter.limit_ = limit;
771  limiter.reportEvery_ = reportEvery;
772  limiter.timespan_ = timespan;
773  xl_iter = xidLimiters_.find(xid);
774  }
775  XidLimiter& limiter = xl_iter->second;
776  time_t now = time(0);
777  // Has it been so long that we should restart counting toward the limit?
778  if ((limiter.timespan_ >= 0) &&
779  (difftime(now, limiter.previousTimestamp_) >= limiter.timespan_)) {
780  limiter.msgCount_ = 0;
781  if (limiter.reportEvery_ > 0) {
782  // So this message will be reacted to
783  limiter.skippedMsgCount_ = limiter.reportEvery_ - 1;
784  } else {
785  limiter.skippedMsgCount_ = 0;
786  }
787  }
788  limiter.previousTimestamp_ = now;
789  ++limiter.msgCount_;
790  ++limiter.skippedMsgCount_;
791  if (limiter.skippedMsgCount_ < limiter.reportEvery_) {
792  // Skip message.
793  return true;
794  }
795  if (limiter.limit_ == 0) {
796  // Zero limit - never react to this
797  // Skip message.
798  return true;
799  }
800  if ((limiter.limit_ < 0) || (limiter.msgCount_ <= limiter.limit_)) {
801  limiter.skippedMsgCount_ = 0;
802  // Accept message.
803  return false;
804  }
805  // Now we are over the limit - have we exceeded limit by 2^N * limit?
806  long diff = limiter.msgCount_ - limiter.limit_;
807  long r = diff / limiter.limit_;
808  if (r * limiter.limit_ != diff) {
809  // Not a multiple of limit - don't react
810  // Skip message.
811  return true;
812  }
813  if (r == 1) {
814  // Exactly twice limit - react
815  limiter.skippedMsgCount_ = 0;
816  // Accept message.
817  return false;
818  }
819  while (r > 1) {
820  if ((r & 1) != 0) {
821  // Not 2**msgCount_ times limit - don't react
822  // Skip message.
823  return true;
824  }
825  r >>= 1;
826  }
827  // If you never get an odd number till one, r is 2**msgCount_ so react
828  limiter.skippedMsgCount_ = 0;
829  // Accept message.
830  return false;
831  }
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 378 of file ELdestination.cc.

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

379  {
380  if (c.substr(0, 4) != "Run:") {
381  return c;
382  }
383  istringstream is(c);
384  string runWord;
385  int run;
386  is >> runWord >> run;
387  if (!is) {
388  return c;
389  }
390  if (runWord != "Run:") {
391  return c;
392  }
393  string eventWord;
394  int event;
395  is >> eventWord >> event;
396  if (!is) {
397  return c;
398  }
399  if (eventWord != "Event:") {
400  return c;
401  }
402  ostringstream os;
403  os << run << "/" << event;
404  return os.str();
405  }
Event finding and building.
void mf::service::ELdestination::summary ( )
virtualinherited

Reimplemented in mf::service::ELstatistics.

Definition at line 706 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(), s, and mf::service::ELdestination::updatedStats_.

707  {
709  ostringstream payload;
710  payload << "\n=============================================\n\n"
711  << "MessageLogger Summary\n"
712  << formSummary();
713  routePayload(payload, ErrorObj{ELzeroSeverity, "No ostream"s});
714  }
715  updatedStats_ = false;
716  if (reset_) {
718  resetLimiters();
719  }
720  }
Float_t s
Definition: plot.C:23
ELslProxy< ELzeroSeverityGen > constexpr ELzeroSeverity
virtual void routePayload(std::ostringstream const &, mf::ErrorObj const &msg)
int mfplugins::ELsyslog::syslogLevel ( ELseverityLevel  severity)
private

Definition at line 88 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(), LOG_DEBUG, LOG_INFO, LOG_WARNING, and mf::errors::LogicError.

Referenced by routePayload().

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

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: