Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
mf::service::ELadministrator Class Reference

#include <ELadministrator.h>

Public Types

using destination_collection_t = std::map< std::string, cet::propagate_const< std::unique_ptr< ELdestination >>>
 

Public Member Functions

void log (ErrorObj &msg)
 
 ELadministrator (ELadministrator const &)=delete
 
ELadministratoroperator= (ELadministrator const &)=delete
 
 ELadministrator (ELadministrator &&)=delete
 
ELadministratoroperator= (ELadministrator &&)=delete
 
void setApplication (std::string const &application)
 
void setHighSeverity (ELseverityLevel const sev)
 
destination_collection_t const & destinations () const
 
destination_collection_tdestinations ()
 
bool hasDestination (std::string const &) const
 
ELseverityLevel checkSeverity ()
 
int severityCount (ELseverityLevel sev) const
 
int severityCount (ELseverityLevel from, ELseverityLevel to) const
 
void resetSeverityCount (ELseverityLevel sev)
 
void resetSeverityCount (ELseverityLevel from, ELseverityLevel to)
 
void resetSeverityCount ()
 
void finish ()
 
std::string const & application () const
 
ELseverityLevel abortThreshold () const
 
ELseverityLevel exitThreshold () const
 
ELseverityLevel highSeverity () const
 
std::string const & hostname () const
 
std::string const & hostaddr () const
 
long pid () const
 
void incrementSeverityCount (int const sev)
 
template<typename DEST >
std::enable_if_t< std::is_base_of< ELdestination, DEST >::value, ELdestination & > attach (std::string const &outputId, std::unique_ptr< DEST > &&dest)
 

Private Member Functions

 ELadministrator (std::string const &applicationName)
 
void updateMsg_ (ErrorObj &msg) const
 
template<typename F >
void for_all_destinations (F f)
 

Private Attributes

std::array< int, ELseverityLevel::nLevelsseverityCounts_
 
ELseverityLevel highSeverity_ {ELseverityLevel::ELsev_zeroSeverity}
 
std::string hostname_ {}
 
std::string hostaddr_ {}
 
std::string application_ {}
 
long pid_ {}
 
destination_collection_t destinations_ {}
 

Detailed Description

Definition at line 32 of file ELadministrator.h.

Member Typedef Documentation

Definition at line 37 of file ELadministrator.h.

Constructor & Destructor Documentation

mf::service::ELadministrator::ELadministrator ( ELadministrator const &  )
delete
mf::service::ELadministrator::ELadministrator ( ELadministrator &&  )
delete
mf::service::ELadministrator::ELadministrator ( std::string const &  applicationName)
private

Definition at line 104 of file ELadministrator.cc.

106 {
107  // hostname
108  char hostname[1024];
109  hostname_ = (gethostname(hostname, 1023) == 0) ? hostname : "Unkonwn Host";
110 
111  // host ip address
112  hostent* host = nullptr;
113  host = gethostbyname(hostname);
114 
115  if (host != nullptr) {
116  // ip address from hostname if the entry exists in /etc/hosts
117  char* ip = inet_ntoa(*(struct in_addr*)host->h_addr);
118  hostaddr_ = ip;
119  } else {
120  // enumerate all network interfaces
121  struct ifaddrs* ifAddrStruct = nullptr;
122  struct ifaddrs* ifa = nullptr;
123  void* tmpAddrPtr = nullptr;
124 
125  if (getifaddrs(&ifAddrStruct)) {
126  // failed to get addr struct
127  hostaddr_ = "127.0.0.1";
128  } else {
129  // iterate through all interfaces
130  for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) {
131  if (ifa->ifa_addr->sa_family == AF_INET) {
132  // a valid IPv4 addres
133  tmpAddrPtr = &((struct sockaddr_in*)ifa->ifa_addr)->sin_addr;
134  char addressBuffer[INET_ADDRSTRLEN];
135  inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
136  hostaddr_ = addressBuffer;
137  } else if (ifa->ifa_addr->sa_family == AF_INET6) {
138  // a valid IPv6 address
139  tmpAddrPtr = &((struct sockaddr_in6*)ifa->ifa_addr)->sin6_addr;
140  char addressBuffer[INET6_ADDRSTRLEN];
141  inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
142  hostaddr_ = addressBuffer;
143  }
144 
145  // find first non-local address
146  if (!hostaddr_.empty() && hostaddr_.compare("127.0.0.1") &&
147  hostaddr_.compare("::1"))
148  break;
149  }
150 
151  if (hostaddr_.empty()) // failed to find anything
152  hostaddr_ = "127.0.0.1";
153  }
154  }
155 
156  // process id
157  pid_ = static_cast<long>(getpid());
158 
159  if (applicationName.empty()) {
160  // get process name from '/proc/pid/cmdline'
161  std::stringstream ss;
162  ss << "//proc//" << pid_ << "//cmdline";
163  std::ifstream procfile{ss.str().c_str()};
164 
165  std::string procinfo;
166 
167  if (procfile.is_open()) {
168  procfile >> procinfo;
169  procfile.close();
170  }
171 
172  size_t end = procinfo.find('\0');
173  size_t start = procinfo.find_last_of('/', end);
174 
175  application_ = procinfo.substr(start + 1, end - start - 1);
176  } else {
177  application_ = applicationName;
178  }
179 } // ELadministrator()
std::string string
Definition: nybbler.cc:12
std::string const & hostname() const
start
Definition: test.py:4
end
Definition: test.py:8

Member Function Documentation

ELseverityLevel mf::service::ELadministrator::abortThreshold ( ) const
std::string const & mf::service::ELadministrator::application ( ) const
inline

Definition at line 150 of file ELadministrator.h.

151 {
152  return application_;
153 }
template<typename DEST >
std::enable_if_t<std::is_base_of<ELdestination, DEST>::value, ELdestination&> mf::service::ELadministrator::attach ( std::string const &  outputId,
std::unique_ptr< DEST > &&  dest 
)
inline

Definition at line 92 of file ELadministrator.h.

93  {
94  return *(destinations_[outputId] = std::move(dest));
95  // If you don't like the above, an alternative is something equivalent to:
96  // return *destinations_.emplace(outputId, std::move(dest)).first->second;
97  }
destination_collection_t destinations_
mf::ELseverityLevel mf::service::ELadministrator::checkSeverity ( )

Definition at line 58 of file ELadministrator.cc.

59 {
60  ELseverityLevel const retval{highSeverity_};
62  return retval;
63 }
ELslProxy< ELzeroSeverityGen > constexpr ELzeroSeverity
auto mf::service::ELadministrator::destinations ( ) const
inline

Definition at line 125 of file ELadministrator.h.

127 {
128  return destinations_;
129 }
destination_collection_t destinations_
auto mf::service::ELadministrator::destinations ( )
inline

Definition at line 132 of file ELadministrator.h.

133 {
134  return destinations_;
135 }
destination_collection_t destinations_
ELseverityLevel mf::service::ELadministrator::exitThreshold ( ) const
void mf::service::ELadministrator::finish ( )
inline

Definition at line 168 of file ELadministrator.h.

169 {
170  for_all_destinations([](auto& d) { d.finish(); });
171 }
template<typename F >
void mf::service::ELadministrator::for_all_destinations ( f)
inlineprivate

Definition at line 116 of file ELadministrator.h.

117  {
118  for (auto& pr : destinations_)
119  f(*pr.second);
120  }
destination_collection_t destinations_
f
Make Macro files for next generation.
bool mf::service::ELadministrator::hasDestination ( std::string const &  name) const

Definition at line 52 of file ELadministrator.cc.

53 {
54  return destinations_.find(name) != destinations_.end();
55 }
destination_collection_t destinations_
mf::ELseverityLevel mf::service::ELadministrator::highSeverity ( ) const
inline

Definition at line 162 of file ELadministrator.h.

163 {
164  return highSeverity_;
165 }
std::string const & mf::service::ELadministrator::hostaddr ( ) const
inline

Definition at line 144 of file ELadministrator.h.

145 {
146  return hostaddr_;
147 }
std::string const & mf::service::ELadministrator::hostname ( ) const
inline

Definition at line 138 of file ELadministrator.h.

139 {
140  return hostname_;
141 }
void mf::service::ELadministrator::incrementSeverityCount ( int const  sev)
inline

Definition at line 83 of file ELadministrator.h.

84  {
85  ++severityCounts_[sev];
86  }
std::array< int, ELseverityLevel::nLevels > severityCounts_
void mf::service::ELadministrator::log ( ErrorObj msg)

Definition at line 19 of file ELadministrator.cc.

20 {
21  auto const severity = msg.xid().severity();
22  updateMsg_(msg);
23  int const lev = severity.getLevel();
24  ++severityCounts_[lev];
25  if (severity > highSeverity_)
26  highSeverity_ = severity;
27 
28  if (destinations_.empty()) {
29  std::cerr
30  << "\nERROR LOGGED WITHOUT DESTINATION!\n"
31  << "Attaching destination \"cerr\" to ELadministrator by default\n\n";
32  destinations_.emplace(
33  "cerr",
34  std::make_unique<ELostreamOutput>(default_destinations_config(),
35  cet::ostream_handle{std::cerr}));
36  }
37 
38  for_all_destinations([&msg](auto& d) { d.log(msg); });
39 }
void updateMsg_(ErrorObj &msg) const
destination_collection_t destinations_
std::array< int, ELseverityLevel::nLevels > severityCounts_
ELadministrator& mf::service::ELadministrator::operator= ( ELadministrator const &  )
delete
ELadministrator& mf::service::ELadministrator::operator= ( ELadministrator &&  )
delete
long mf::service::ELadministrator::pid ( ) const
inline

Definition at line 156 of file ELadministrator.h.

157 {
158  return pid_;
159 }
void mf::service::ELadministrator::resetSeverityCount ( ELseverityLevel  sev)

Definition at line 85 of file ELadministrator.cc.

86 {
87  severityCounts_[sev.getLevel()] = 0;
88 }
std::array< int, ELseverityLevel::nLevels > severityCounts_
void mf::service::ELadministrator::resetSeverityCount ( ELseverityLevel  from,
ELseverityLevel  to 
)

Definition at line 91 of file ELadministrator.cc.

93 {
94  for (int k = from.getLevel(); k <= to.getLevel(); ++k)
95  severityCounts_[k] = 0;
96 }
std::array< int, ELseverityLevel::nLevels > severityCounts_
void mf::service::ELadministrator::resetSeverityCount ( )

Definition at line 99 of file ELadministrator.cc.

100 {
102 }
ELslProxy< ELhighestSeverityGen > constexpr ELhighestSeverity
ELslProxy< ELzeroSeverityGen > constexpr ELzeroSeverity
void mf::service::ELadministrator::setApplication ( std::string const &  application)

Definition at line 46 of file ELadministrator.cc.

47 {
49 }
std::string const & application() const
void mf::service::ELadministrator::setHighSeverity ( ELseverityLevel const  sev)
inline

Definition at line 51 of file ELadministrator.h.

52  {
53  highSeverity_ = sev;
54  }
int mf::service::ELadministrator::severityCount ( ELseverityLevel  sev) const

Definition at line 66 of file ELadministrator.cc.

67 {
68  return severityCounts_[sev.getLevel()];
69 }
std::array< int, ELseverityLevel::nLevels > severityCounts_
int mf::service::ELadministrator::severityCount ( ELseverityLevel  from,
ELseverityLevel  to 
) const

Definition at line 72 of file ELadministrator.cc.

74 {
75  int k = from.getLevel();
76  int sum = severityCounts_[k];
77 
78  while (++k <= to.getLevel())
79  sum += severityCounts_[k];
80 
81  return sum;
82 }
std::array< int, ELseverityLevel::nLevels > severityCounts_
void mf::service::ELadministrator::updateMsg_ ( ErrorObj msg) const
private

Definition at line 182 of file ELadministrator.cc.

183 {
184  auto const& xid = msg.xid();
185  if (xid.hostname().empty()) {
186  msg.setHostName(hostname());
187  }
188  if (xid.hostaddr().empty()) {
189  msg.setHostAddr(hostaddr());
190  }
191  if (xid.application().empty()) {
192  msg.setApplication(application());
193  }
194  if (xid.pid() == 0) {
195  msg.setPID(pid());
196  }
197 }
std::string const & hostname() const
std::string const & hostaddr() const
std::string const & application() const

Member Data Documentation

std::string mf::service::ELadministrator::application_ {}
private

Definition at line 109 of file ELadministrator.h.

destination_collection_t mf::service::ELadministrator::destinations_ {}
private

Definition at line 112 of file ELadministrator.h.

ELseverityLevel mf::service::ELadministrator::highSeverity_ {ELseverityLevel::ELsev_zeroSeverity}
private

Definition at line 104 of file ELadministrator.h.

std::string mf::service::ELadministrator::hostaddr_ {}
private

Definition at line 108 of file ELadministrator.h.

std::string mf::service::ELadministrator::hostname_ {}
private

Definition at line 107 of file ELadministrator.h.

long mf::service::ELadministrator::pid_ {}
private

Definition at line 110 of file ELadministrator.h.

std::array<int, ELseverityLevel::nLevels> mf::service::ELadministrator::severityCounts_
private
Initial value:
{
{0}}

Definition at line 102 of file ELadministrator.h.


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