1 #include "daq/slc/database/DAQLogDB.h"
3 #include <daq/slc/system/LogFile.h>
5 #include <daq/slc/base/StringUtil.h>
6 #include <daq/slc/database/DBHandlerException.h>
15 bool DAQLogDB::createLog(
DBInterface& db,
const std::string& tablename,
20 if (!db.isConnected()) db.connect();
22 LogFile::error(
e.what());
26 std::string tablename_date = tablename +
"_" +
Date().toString(
"%Y");
27 if (!db.checkTable(
"daqlog")) {
28 db.execute(
"create table daqlog \n"
29 "(name text not null, \n"
30 "id bigserial, lastupdate timestamp, \n"
32 db.execute(
"create index daqlog_id_index on daqlog(id);",
33 tablename.c_str(), tablename.c_str());
35 if (!db.checkTable(tablename_date)) {
37 db.execute(
"insert into daqlog (name, lastupdate) values "
38 "('%s', current_timestamp);", tablename_date.c_str());
39 db.execute(
"create table %s \n"
40 "(node int not null, \n"
41 "priority int not null, \n"
43 "date timestamp with time zone, \n"
44 "message text not null); ", tablename_date.c_str());
45 }
catch (
const std::exception& e) {
46 db.execute(
"update daqlog set lastupdate = current_timestamp where name = '%s';",
47 tablename_date.c_str());
49 db.execute(
"create index %s_id_index on %s(id);",
50 tablename_date.c_str(), tablename_date.c_str());
52 db.execute(
"select id,category from log_node where name = '" + log.getNodeName() +
"';");
53 DBRecordList record(db.loadRecords());
56 if (record.size() > 0) {
57 id = record[0].getInt(
"id");
60 db.execute(
"insert into log_node (name, category) values ('" + log.getNodeName() +
61 "', " + StringUtil::form(
"%d", log.getCategory()) +
") returning id,category;");
62 DBRecordList record(db.loadRecords());
63 id = record[0].getInt(
"id");
66 db.execute(
"insert into %s (node, priority, date, message) values "
67 "(%d, %d, to_timestamp(%d), '%s');",
68 tablename_date.c_str(),
id, log.getPriority(),
69 log.getDateInt(), log.getMessage().c_str());
71 LogFile::error(
e.what());
77 DAQLogMessageList DAQLogDB::getLogs(
DBInterface& db,
const std::string& tablename,
78 const std::string& nodename,
int max)
80 std::string tablename_date = tablename +
"_" +
Date().toString(
"%Y");
81 DAQLogMessageList logs;
83 if (!db.isConnected()) db.connect();
84 if (nodename.size() > 0) {
86 db.execute(
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
87 " from %s as l, log_node as n, log_priority as p where l.node=n.id and p.id=l.priority and n.name = '%s' order by l.id desc limit %d;",
88 tablename_date.c_str(), nodename.c_str(), max);
90 db.execute(
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
91 " from %s as l, log_node as n, log_priority as p where l.node=n.id and p.id=l.priority and n.name = '%s' order by l.id desc;",
92 tablename_date.c_str(), nodename.c_str());
96 db.execute(
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
97 " from %s as l, log_node as n, log_priority as p where l.node=n.id and p.id=l.priority order by l.id desc limit %d;",
98 tablename_date.c_str(), max);
100 db.execute(
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
101 " from %s as l, log_node as n, log_priority as p where l.node=n.id and p.id=l.priority order by l.id desc;",
102 tablename_date.c_str());
105 DBRecordList record_v(db.loadRecords());
106 for (
size_t i = 0; i < record_v.size(); i++) {
109 (LogFile::Priority)(record.getInt(
"priority")),
110 record.get(
"l.message"),
Date(record.getInt(
"date"))));
113 LogFile::error(
e.what());
118 DAQLogMessageList DAQLogDB::getLogs(
DBInterface& db,
const std::string& tablename,
119 const std::string& nodename,
const std::string& begin_date,
120 const std::string& end_date,
int max,
int priority)
122 std::string tablename_date = tablename +
"_" +
Date().toString(
"%Y");
123 DAQLogMessageList logs;
125 if (!db.isConnected()) db.connect();
126 std::stringstream ss;
127 ss <<
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
128 <<
" from " << tablename_date <<
" as l, log_node as n where l.node=n.id ";
129 if (nodename.size() > 0) {
130 ss <<
"and n.name = '" << nodename <<
"' ";
132 if (begin_date.size() > 0) {
133 ss <<
"and l.date >= (timestamp '" << begin_date <<
"') ";
135 if (end_date.size() > 0) {
136 ss <<
"and l.date <= (timestamp '" << end_date <<
"') ";
139 ss <<
"and l.priority >= " << priority <<
" ";
141 ss <<
"order by l.id desc ";
142 if (max > 0) ss <<
"limit " << max;
143 db.execute(ss.str().c_str());
144 DBRecordList record_v(db.loadRecords());
145 for (
size_t i = 0; i < record_v.size(); i++) {
148 (LogFile::Priority)(record.getInt(
"priority")),
149 record.get(
"message"),
Date(record.getInt(
"date"))));
152 LogFile::error(
e.what());