1 #include "cetlib/PluginTypeDeducer.h"
2 #include "fhiclcpp/ParameterSet.h"
4 #include "cetlib/compiler_macros.h"
5 #include "messagefacility/MessageLogger/MessageLogger.h"
6 #include "messagefacility/MessageService/ELdestination.h"
7 #include "messagefacility/Utilities/ELseverityLevel.h"
8 #include "messagefacility/Utilities/exception.h"
11 #include <arpa/inet.h>
14 #include <netinet/in.h>
21 #define TRACE_NAME "OTS_mfPlugin"
25 #include <boost/algorithm/string.hpp>
28 using mf::ELseverityLevel;
30 using mf::service::ELdestination;
36 class ELOTS :
public ELdestination
48 fhicl::Name{
"format_string"}, fhicl::Comment{
"Format specifier for printing to console. %% => '%' ... "},
52 fhicl::Atom<std::string>{fhicl::Name{
"filename_delimit"},
53 fhicl::Comment{
"Grab path after this. \"/srcs/\" /x/srcs/y/z.cc => y/z.cc"},
"/"};
70 virtual void fillPrefix(std::ostringstream& o,
const ErrorObj& e)
override;
77 virtual void fillUsrMsg(std::ostringstream& o,
const ErrorObj& e)
override;
82 virtual void fillSuffix(std::ostringstream&,
const ErrorObj&)
override {}
89 virtual void routePayload(
const std::ostringstream& o,
const ErrorObj& e)
override;
94 std::string hostname_;
95 std::string hostaddr_;
97 std::string format_string_;
98 std::string filename_delimit_;
110 : ELdestination(pset().elDestConfig()), pid_(static_cast<long>(getpid())), format_string_(pset().format_string()), filename_delimit_(pset().filename_delimit())
113 char hostname_c[1024];
114 hostname_ = (gethostname(hostname_c, 1023) == 0) ? hostname_c :
"Unkonwn Host";
117 hostent* host =
nullptr;
118 host = gethostbyname(hostname_c);
123 char* ip = inet_ntoa(*(
struct in_addr*)host->h_addr);
129 struct ifaddrs* ifAddrStruct =
nullptr;
130 struct ifaddrs* ifa =
nullptr;
131 void* tmpAddrPtr =
nullptr;
133 if (getifaddrs(&ifAddrStruct))
136 hostaddr_ =
"127.0.0.1";
141 for (ifa = ifAddrStruct; ifa !=
nullptr; ifa = ifa->ifa_next)
143 if (ifa->ifa_addr->sa_family == AF_INET)
146 tmpAddrPtr = &((
struct sockaddr_in*)ifa->ifa_addr)->sin_addr;
147 char addressBuffer[INET_ADDRSTRLEN];
148 inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
149 hostaddr_ = addressBuffer;
152 else if (ifa->ifa_addr->sa_family == AF_INET6)
155 tmpAddrPtr = &((
struct sockaddr_in6*)ifa->ifa_addr)->sin6_addr;
156 char addressBuffer[INET6_ADDRSTRLEN];
157 inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
158 hostaddr_ = addressBuffer;
162 if (!hostaddr_.empty() && hostaddr_.compare(
"127.0.0.1") && hostaddr_.compare(
"::1"))
break;
165 if (hostaddr_.empty())
166 hostaddr_ =
"127.0.0.1";
171 std::stringstream ss;
172 ss <<
"//proc//" << pid_ <<
"//cmdline";
173 std::ifstream procfile{ss.str().c_str()};
175 std::string procinfo;
177 if (procfile.is_open())
179 procfile >> procinfo;
183 size_t end = procinfo.find(
'\0');
184 size_t start = procinfo.find_last_of(
'/', end);
186 app_ = procinfo.substr(start + 1, end - start - 1);
194 const auto& xid = msg.xid();
197 auto module = xid.module();
199 char* cp = &format_string_[0];
201 bool msg_printed =
false;
230 if (filename_delimit_.size() == 0)
231 oss << msg.filename();
232 else if (filename_delimit_.size() == 1)
233 oss << (strrchr(&msg.filename()[0], filename_delimit_[0])
234 ? strrchr(&msg.filename()[0], filename_delimit_[0]) + 1
237 oss << (strstr(&msg.filename()[0], &filename_delimit_[0])
238 ? strstr(&msg.filename()[0], &filename_delimit_[0]) + filename_delimit_.size()
245 oss << xid.severity().getName();
249 for (
auto const& val : msg.items()) ossstr += val;
252 if (ossstr.compare(0, 1,
"\n") == 0) ossstr.erase(0, 1);
253 if (ossstr.compare(ossstr.size() - 1, 1,
"\n") == 0)
254 ossstr.erase(ossstr.size() - 1, 1);
266 oss << mf::GetIteration();
269 sev = xid.severity().getName()[0] | 0x20;
273 oss << format_.timestamp(msg.timestamp());
276 oss << std::to_string(msg.lineNumber());
288 for (
auto const& val : msg.items()) ossstr += val;
289 if (ossstr.compare(0, 1,
"\n") == 0) ossstr.erase(0, 1);
290 if (ossstr.compare(ossstr.size() - 1, 1,
"\n") == 0)
291 ossstr.erase(ossstr.size() - 1, 1);
300 const ErrorObj& msg __attribute__((__unused__)))
308 void ELOTS::routePayload(
const std::ostringstream& oss,
const ErrorObj&) { std::cout << oss.str() << std::endl; }
316 #ifndef EXTERN_C_FUNC_DECLARE_START
317 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
320 EXTERN_C_FUNC_DECLARE_START
auto makePlugin(
const std::string&,
const fhicl::ParameterSet& pset)
322 return std::make_unique<mfplugins::ELOTS>(pset);
326 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
Configuration Parameters for ELOTS.
fhicl::Atom< std::string > filename_delimit
filename_delimit (Default: "/"): Grab path after this. "/srcs/" /x/srcs/y/z.cc => y/z...
virtual void fillUsrMsg(std::ostringstream &o, const ErrorObj &e) override
Fill the "User Message" portion of the message.
fhicl::TableFragment< ELdestination::Config > elDestConfig
ELDestination common config parameters.
virtual void routePayload(const std::ostringstream &o, const ErrorObj &e) override
Serialize a MessageFacility message to the output.
fhicl::Atom< std::string > format_string
format_string (Default: "%L:%N:%f [%u] %m"): Format specifier for printing to console. %% => '' ...
Message Facility OTS Console Destination Formats messages into Ryan's favorite format for OTS ...
virtual void fillPrefix(std::ostringstream &o, const ErrorObj &e) override
Fill the "Prefix" portion of the message.
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation.
virtual void fillSuffix(std::ostringstream &, const ErrorObj &) override
Fill the "Suffix" portion of the message (Unused)
ELOTS(Parameters const &pset)
ELOTS Constructor