8 #include "daq/slc/database/DAQLogDB.h"
10 #include <daq/slc/system/LogFile.h>
12 #include <daq/slc/base/StringUtil.h>
13 #include <daq/slc/database/DBHandlerException.h>
22 bool DAQLogDB::createLog(
DBInterface& db,
const std::string& tablename,
27 if (!db.isConnected()) db.connect();
29 LogFile::error(e.what());
33 std::string tablename_date = tablename +
"_" +
Date().toString(
"%Y");
34 if (!db.checkTable(
"daqlog")) {
35 db.execute(
"create table daqlog \n"
36 "(name text not null, \n"
37 "id bigserial, lastupdate timestamp, \n"
39 db.execute(
"create index daqlog_id_index on daqlog(id);",
40 tablename.c_str(), tablename.c_str());
42 if (!db.checkTable(tablename_date)) {
44 db.execute(
"insert into daqlog (name, lastupdate) values "
45 "('%s', current_timestamp);", tablename_date.c_str());
46 db.execute(
"create table %s \n"
47 "(node int not null, \n"
48 "priority int not null, \n"
50 "date timestamp with time zone, \n"
51 "message text not null); ", tablename_date.c_str());
52 }
catch (
const std::exception& e) {
53 db.execute(
"update daqlog set lastupdate = current_timestamp where name = '%s';",
54 tablename_date.c_str());
56 db.execute(
"create index %s_id_index on %s(id);",
57 tablename_date.c_str(), tablename_date.c_str());
59 db.execute(
"select id,category from log_node where name = '" + log.getNodeName() +
"';");
60 DBRecordList record(db.loadRecords());
63 if (record.size() > 0) {
64 id = record[0].getInt(
"id");
67 db.execute(
"insert into log_node (name, category) values ('" + log.getNodeName() +
68 "', " + StringUtil::form(
"%d", log.getCategory()) +
") returning id,category;");
69 DBRecordList record(db.loadRecords());
70 id = record[0].getInt(
"id");
73 db.execute(
"insert into %s (node, priority, date, message) values "
74 "(%d, %d, to_timestamp(%d), '%s');",
75 tablename_date.c_str(),
id, log.getPriority(),
76 log.getDateInt(), log.getMessage().c_str());
78 LogFile::error(e.what());
84 DAQLogMessageList DAQLogDB::getLogs(
DBInterface& db,
const std::string& tablename,
85 const std::string& nodename,
int max)
87 std::string tablename_date = tablename +
"_" +
Date().toString(
"%Y");
88 DAQLogMessageList logs;
90 if (!db.isConnected()) db.connect();
91 if (nodename.size() > 0) {
93 db.execute(
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
94 " 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;",
95 tablename_date.c_str(), nodename.c_str(), max);
97 db.execute(
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
98 " 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;",
99 tablename_date.c_str(), nodename.c_str());
103 db.execute(
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
104 " 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;",
105 tablename_date.c_str(), max);
107 db.execute(
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
108 " 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;",
109 tablename_date.c_str());
112 DBRecordList record_v(db.loadRecords());
113 for (
size_t i = 0; i < record_v.size(); i++) {
116 (LogFile::Priority)(record.getInt(
"priority")),
117 record.get(
"l.message"),
Date(record.getInt(
"date"))));
120 LogFile::error(e.what());
125 DAQLogMessageList DAQLogDB::getLogs(
DBInterface& db,
const std::string& tablename,
126 const std::string& nodename,
const std::string& begin_date,
127 const std::string& end_date,
int max,
int priority)
129 std::string tablename_date = tablename +
"_" +
Date().toString(
"%Y");
130 DAQLogMessageList logs;
132 if (!db.isConnected()) db.connect();
133 std::stringstream ss;
134 ss <<
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
135 <<
" from " << tablename_date <<
" as l, log_node as n where l.node=n.id ";
136 if (nodename.size() > 0) {
137 ss <<
"and n.name = '" << nodename <<
"' ";
139 if (begin_date.size() > 0) {
140 ss <<
"and l.date >= (timestamp '" << begin_date <<
"') ";
142 if (end_date.size() > 0) {
143 ss <<
"and l.date <= (timestamp '" << end_date <<
"') ";
146 ss <<
"and l.priority >= " << priority <<
" ";
148 ss <<
"order by l.id desc ";
149 if (max > 0) ss <<
"limit " << max;
150 db.execute(ss.str().c_str());
151 DBRecordList record_v(db.loadRecords());
152 for (
size_t i = 0; i < record_v.size(); i++) {
155 (LogFile::Priority)(record.getInt(
"priority")),
156 record.get(
"message"),
Date(record.getInt(
"date"))));
159 LogFile::error(e.what());
Abstract base class for different kinds of events.