26 #include <type_traits> 30 #include <arpa/inet.h> 33 #include <netinet/in.h> 49 if (
getenv(
"MF_PLUGIN_PATH") !=
nullptr) {
58 atomic<bool> isStarted{
false};
59 map<string const, unique_ptr<service::ELdestination>> destinations_;
60 bool cleanSlateConfiguration_{
true};
61 atomic<bool> purgeMode_{
false};
62 atomic<int> count_{0};
63 std::recursive_mutex msgMutex_{};
71 thread_local
string module_ =
"Early";
76 thread_local
string iteration_ =
"pre-events";
79 initGlobalVars(
string const& applicationName =
"")
81 char hostname[1024] = {0};
83 (gethostname(hostname, 1023) == 0) ? hostname :
"Unknown Host";
84 hostent* host =
nullptr;
85 host = gethostbyname(hostname);
86 if (host !=
nullptr) {
88 char* ip = inet_ntoa(*(
struct in_addr*)host->h_addr);
93 ifaddrs* ifAddrStruct =
nullptr;
94 if (getifaddrs(&ifAddrStruct)) {
96 hostaddr_ =
"127.0.0.1";
99 for (ifaddrs* ifa = ifAddrStruct; ifa !=
nullptr;
100 ifa = ifa->ifa_next) {
101 if (ifa->ifa_addr->sa_family == AF_INET) {
103 void* tmpAddrPtr = &((sockaddr_in*)ifa->ifa_addr)->sin_addr;
104 char addressBuffer[INET_ADDRSTRLEN];
105 inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
106 hostaddr_ = addressBuffer;
107 }
else if (ifa->ifa_addr->sa_family == AF_INET6) {
109 void* tmpAddrPtr = &((sockaddr_in6*)ifa->ifa_addr)->sin6_addr;
110 char addressBuffer[INET6_ADDRSTRLEN];
111 inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
112 hostaddr_ = addressBuffer;
115 if (!
empty(hostaddr_) && hostaddr_.compare(
"127.0.0.1") &&
116 hostaddr_.compare(
"::1")) {
120 if (
empty(hostaddr_)) {
122 hostaddr_ =
"127.0.0.1";
126 if (!
empty(applicationName)) {
127 application_ = applicationName;
131 ss <<
"//proc//" << pid_ <<
"//cmdline";
132 ifstream procfile{ss.str().c_str()};
134 if (procfile.is_open()) {
136 procfile >> procinfo;
139 auto end = procinfo.find(
'\0');
140 auto start = procinfo.find_last_of(
'/',
end);
141 application_ = procinfo.substr(start + 1,
end - start - 1);
143 pid_ =
static_cast<long>(getpid());
150 unique_ptr<service::ELdestination>
152 string const& libspec,
153 string const& psetname,
156 unique_ptr<service::ELdestination>
result;
158 auto const pluginType = plugin_factory.
pluginType(libspec);
162 plugin_factory.
makePlugin<unique_ptr<service::ELdestination>>(
163 libspec, psetname, pset);
166 <<
"unrecognized plugin type " << pluginType <<
"for plugin " 172 <<
"Exception caught while processing plugin spec.\n";
177 string const default_destination_config_string =
" type: cerr" 184 default_destination_config()
190 default_destination_set_config()
192 string const config{
"cerr: { "s + default_destination_config_string +
200 map<
string const, unique_ptr<service::ELdestination>>& destinations)
211 if (msg.
xid().
pid() == 0) {
214 if (
empty(destinations)) {
215 cerr <<
"\nERROR LOGGED WITHOUT DESTINATION!\nAttaching destination " 216 "\"cerr\" by default\n\n";
217 destinations.emplace(
219 make_unique<service::ELostreamOutput>(
222 for (
auto& destid_and_destination : destinations) {
223 destid_and_destination.second->log(msg);
232 vector<string> config_errors;
235 if (dest_pset.is_empty()) {
236 dest_pset = default_destination_config();
240 if (!dest_pset.get_if_present(
"type", dest_type)) {
242 <<
"No 'type' specified for destination '" << psetname <<
"'.\n";
245 if ((dest_type !=
"cout"s) && (dest_type !=
"cerr"s) &&
246 (dest_type !=
"file"s)) {
249 <<
"Unsupported type [ " << dest_type
250 <<
" ] chosen for statistics printout.\n" 251 <<
"Must choose ostream type: \"cout\", \"cerr\", or \"file\"" 255 string outputId{dest_type};
256 if ((dest_type !=
"cout"s) && (dest_type !=
"cerr"s) &&
257 (dest_type !=
"syslog"s)) {
258 outputId +=
":" + dest_pset.get<
string>(
"filename", psetname);
260 if (!ids.emplace(outputId).second) {
264 <<
" Output identifier: \"" << outputId <<
"\"" 265 <<
" already specified within ordinary/statistics block in FHiCL " 270 auto iter_id_dest = destinations_.find(outputId);
271 if (iter_id_dest != destinations_.end()) {
273 string const hrule{
'\n' + rule(
'=') +
'\n'};
274 ostringstream except_msg;
275 except_msg << hrule <<
"\n Duplicate name for a ";
276 if (configuration == destination_kind::ordinary) {
277 except_msg <<
"MessageLogger";
279 except_msg <<
"MessageLogger Statistics";
281 except_msg <<
" destination: \"" << outputId <<
'"';
282 ostringstream orig_config_msg;
284 <<
"\n Only original configuration instructions are used. \n" 286 if (cleanSlateConfiguration_) {
288 << except_msg.str() << orig_config_msg.str();
291 << except_msg.str() << orig_config_msg.str();
295 string const& libspec = dest_type;
297 pluginStatsFactory_ :
300 destinations_[outputId] =
301 makePlugin_(plugin_factory, libspec, psetname, dest_pset);
304 string msg{
"Configuration error for destination: " +
307 config_errors.push_back(
move(msg));
310 if (!
empty(config_errors)) {
311 string msg{
"\nThe following messagefacility destinations have " 312 "configuration errors:\n\n"};
316 auto start =
cbegin(config_errors);
319 for (
auto it = start,
e =
cend(config_errors); it !=
e; ++it) {
333 if (destinations_.size() > 1) {
335 <<
"The message logger has been configured multiple times";
336 cleanSlateConfiguration_ =
false;
341 dest_psets = default_destination_set_config();
343 ordinaryDests = dest_psets;
344 ordinaryDests.
erase(
"statistics");
350 string const default_config{
"file_stats: {\n" 352 " filename: \"err.log\"\n" 353 " threshold: WARNING\n" 357 makeDestinations(ordinaryDests, destination_kind::ordinary);
359 "statistics", default_statistics_config);
371 std::lock_guard sentry{msgMutex_};
374 unique_ptr<ErrorObj> msgHolder{msg};
376 sendMsgToDests(*msg, destinations_);
380 cerr <<
"MessageLoggerScribe caught " << count_
381 <<
" cet::exceptions, text = \n" 384 cerr <<
"MessageLogger will no longer be processing messages due to " 385 "errors (entering purge mode).\n";
390 cerr <<
"MessageLoggerScribe caught an unknown exception and will no " 391 "longer be processing messages. (entering purge mode)\n";
398 for (
auto& destid_and_dest : destinations_) {
399 auto&
dest = *destid_and_dest.second;
404 cerr <<
"MessageLoggerScribe caught exception during summarize:\n" 408 cerr <<
"MessageLoggerScribe caught unknown exception type during " 409 "summarize. (Ignored)\n";
418 return isStarted.load();
425 if (isStarted.load()) {
448 if (isStarted.load()) {
461 string const& applicationName)
463 if (isStarted.load()) {
468 initGlobalVars(applicationName);
470 destinations_[
"cerr_early"s] = makePlugin_(
471 pluginFactory_,
"cerr",
"cerr_early", default_destination_config());
474 string msg{
"\nConfiguration error for destination: " +
488 "Exception from MessageLoggerScribe::configure",
491 isStarted.store(
true);
498 isStarted.store(
false);
501 for (
auto& category_and_destination : destinations_) {
502 category_and_destination.second->finish();
510 application_ = applicationName;
517 hostname_ = hostname;
524 hostaddr_ = hostaddr;
643 if (isStarted.load()) {
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
fhicl::OptionalDelegatedParameter destinations
ELseverityLevel severity() const
void SetIteration(string const &val)
std::optional< T > get_if_present() const
void SetHostAddr(string const &hostaddr)
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
void msg(const char *fmt,...)
void EndMessageFacility()
static ParameterSet make(intermediate_table const &tbl)
std::string const & application() const
virtual void setPID(long)
string const & GetModuleName()
std::enable_if_t<!std::is_function_v< RESULT_TYPE >, RESULT_TYPE > makePlugin(std::string const &libspec, ARGS &&...args) const
static struct mf::FinalShutdown ensureShutdown
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
ELslProxy< ELhighestSeverityGen > constexpr ELhighestSeverity
virtual void setHostName(std::string const &)
void SetContextIteration(string const &val)
constexpr char const * plugin_libpath()
cet::coded_exception< errors::error, detail::translate > Exception
std::string const & idOverflow() const
bool isMessageProcessingSetUp()
std::vector< std::string > get_pset_names() const
void StartMessageFacility(fhicl::ParameterSet const &pset, string const &applicationName)
std::string const & hostaddr() const
ELextendedID const & xid() const
std::string getenv(std::string const &name)
T get(std::string const &key) const
std::string bold_fontify(std::string const &s)
virtual void setHostAddr(std::string const &)
std::string const & id() const
std::string fullText() const
virtual void setApplication(std::string const &)
void SetHostName(string const &hostname)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
string const & GetHostName()
char const * what() const noexcept override
string const & GetIteration()
std::string pluginType(std::string const &libspec) const
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
void SetContextSinglet(string const &val)
decltype(auto) constexpr cbegin(T &&obj)
ADL-aware version of std::cbegin.
virtual void setReactedTo(bool)
bool erase(std::string const &key)
void LogErrorObj(ErrorObj *msg)
void SetModuleName(string const &val)
void SetApplicationName(string const &applicationName)
std::string const & hostname() const
void SetPid(long const pid)
cet::coded_exception< error, detail::translate > exception
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
std::string getSymbol() const
string const & GetApplicationName()
string const & GetHostAddr()