LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
sqlite_mfPlugin.cc
Go to the documentation of this file.
1 // vim: set sw=2 expandtab :
2 
4 
5 #include "cetlib/sqlite/ConnectionFactory.h"
6 #include "cetlib/sqlite/Ntuple.h"
13 
14 #include <memory>
15 #include <sstream>
16 #include <string>
17 
18 using namespace std;
19 using namespace std::string_literals;
20 
21 namespace {
22 
23  using namespace cet::sqlite;
24 
25  ConnectionFactory factory;
26 
27  class sqlite3Plugin : public mf::service::ELdestination {
28 
29  public:
30  struct Config {
31 
33  fhicl::Atom<string> filename{fhicl::Name{"filename"}};
34  };
35 
36  using Parameters = fhicl::WrappedTable<Config>;
37 
38  public:
39  ~sqlite3Plugin();
40  sqlite3Plugin(Parameters const&);
41 
42  private:
43  void
44  fillPrefix(ostringstream&, mf::ErrorObj const&) override
45  {}
46  void
47  fillSuffix(ostringstream&, mf::ErrorObj const&) override
48  {}
49  void routePayload(ostringstream const&, mf::ErrorObj const&) override;
50 
51  private:
52  std::unique_ptr<Connection> connection_;
53  Ntuple<string,
54  string,
55  string,
56  string,
57  string,
58  string,
59  unsigned,
60  string,
61  string,
62  string>* msgTable_;
63  };
64 
65  sqlite3Plugin::~sqlite3Plugin()
66  {
67  delete msgTable_;
68  msgTable_ = nullptr;
69  }
70 
71  sqlite3Plugin::sqlite3Plugin(Parameters const& ps)
72  : ELdestination{ps().elDestConfig()}
73  , connection_{factory.make_connection(ps().filename())}
74  , msgTable_{new Ntuple<string,
75  string,
76  string,
77  string,
78  string,
79  string,
80  unsigned,
81  string,
82  string,
83  string>{*connection_,
84  "Messages"s,
85  {{"Timestamp"s,
86  "Hostname"s,
87  "Hostaddress"s,
88  "Severity"s,
89  "Category"s,
90  "AppName"s,
91  "ProcessId"s,
92  "RunEventNo"s,
93  "ModuleName"s,
94  "Message"s}}}}
95  {}
96 
97  void
98  sqlite3Plugin::routePayload(ostringstream const& oss, mf::ErrorObj const& msg)
99  {
100  auto const& xid = msg.xid();
101  string const& timestamp = format_.timestamp(msg.timestamp());
102  string const& hostname = xid.hostname();
103  string const& hostaddr = xid.hostaddr();
104  string const& severity = xid.severity().getName();
105  string const& category = xid.id();
106  string const& app = xid.application();
107  long const& pid = xid.pid();
108  string const& iterationNo = mf::GetIteration();
109  string const& modname = xid.module();
110  string const& usrMsg =
111  !oss.str().compare(0, 1, "\n") ? oss.str().erase(0, 1) : oss.str();
112  msgTable_->insert(timestamp,
113  hostname,
114  hostaddr,
115  severity,
116  category,
117  app,
118  pid,
119  iterationNo,
120  modname,
121  usrMsg);
122  }
123 
124 } // unnamed namespace
125 
127 {
128  return make_unique<sqlite3Plugin>(pset);
129 }
130 MAKE_PLUGIN_END
131 
timeval timestamp() const
Definition: ErrorObj.cc:73
STL namespace.
fhicl::Atom< std::string > filename
Definition: FileConfig.h:13
ELextendedID const & xid() const
Definition: ErrorObj.cc:61
string const & GetIteration()
#define FHICL_PROVIDE_ALLOWED_CONFIGURATION(klass)
MAKE_MFPLUGIN_START(, pset)
std::string const & hostname() const
Definition: ELextendedID.cc:51