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>
22bool DAQLogDB::createLog(
DBInterface& db,
const std::string& tablename,
26 if (!db.isConnected()) db.connect();
28 LogFile::error(e.what());
32 std::string tablename_date = tablename +
"_" +
Date().toString(
"%Y");
33 if (!db.checkTable(
"daqlog")) {
34 db.execute(
"create table daqlog \n"
35 "(name text not null, \n"
36 "id bigserial, lastupdate timestamp, \n"
38 db.execute(
"create index daqlog_id_index on daqlog(id);",
39 tablename.c_str(), tablename.c_str());
41 if (!db.checkTable(tablename_date)) {
43 db.execute(
"insert into daqlog (name, lastupdate) values "
44 "('%s', current_timestamp);", tablename_date.c_str());
45 db.execute(
"create table %s \n"
46 "(node int not null, \n"
47 "priority int not null, \n"
49 "date timestamp with time zone, \n"
50 "message text not null); ", tablename_date.c_str());
51 }
catch (
const std::exception& e) {
52 db.execute(
"update daqlog set lastupdate = current_timestamp where name = '%s';",
53 tablename_date.c_str());
55 db.execute(
"create index %s_id_index on %s(id);",
56 tablename_date.c_str(), tablename_date.c_str());
58 db.execute(
"select id,category from log_node where name = '" + log.getNodeName() +
"';");
59 DBRecordList record(db.loadRecords());
62 if (record.size() > 0) {
63 id = record[0].getInt(
"id");
66 db.execute(
"insert into log_node (name, category) values ('" + log.getNodeName() +
67 "', " + StringUtil::form(
"%d", log.getCategory()) +
") returning id,category;");
68 DBRecordList recordNow(db.loadRecords());
69 id = recordNow[0].getInt(
"id");
72 db.execute(
"insert into %s (node, priority, date, message) values "
73 "(%d, %d, to_timestamp(%d), '%s');",
74 tablename_date.c_str(),
id, log.getPriority(),
75 log.getDateInt(), log.getMessage().c_str());
77 LogFile::error(e.what());
83DAQLogMessageList DAQLogDB::getLogs(
DBInterface& db,
const std::string& tablename,
84 const std::string& nodename,
int max)
86 std::string tablename_date = tablename +
"_" +
Date().toString(
"%Y");
87 DAQLogMessageList logs;
89 if (!db.isConnected()) db.connect();
90 if (nodename.size() > 0) {
92 db.execute(
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
93 " 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;",
94 tablename_date.c_str(), nodename.c_str(), max);
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 and n.name = '%s' order by l.id desc;",
98 tablename_date.c_str(), nodename.c_str());
102 db.execute(
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
103 " 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;",
104 tablename_date.c_str(), max);
106 db.execute(
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
107 " 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;",
108 tablename_date.c_str());
111 DBRecordList record_v(db.loadRecords());
112 for (
size_t i = 0; i < record_v.size(); i++) {
115 (LogFile::Priority)(record.getInt(
"priority")),
116 record.get(
"l.message"),
Date(record.getInt(
"date"))));
119 LogFile::error(e.what());
124DAQLogMessageList DAQLogDB::getLogs(
DBInterface& db,
const std::string& tablename,
125 const std::string& nodename,
const std::string& begin_date,
126 const std::string& end_date,
int max,
int priority)
128 std::string tablename_date = tablename +
"_" +
Date().toString(
"%Y");
129 DAQLogMessageList logs;
131 if (!db.isConnected()) db.connect();
132 std::stringstream ss;
133 ss <<
"select n.name, extract(epoch from l.date) date, l.priority, l.message"
134 <<
" from " << tablename_date <<
" as l, log_node as n where l.node=n.id ";
135 if (nodename.size() > 0) {
136 ss <<
"and n.name = '" << nodename <<
"' ";
138 if (begin_date.size() > 0) {
139 ss <<
"and l.date >= (timestamp '" << begin_date <<
"') ";
141 if (end_date.size() > 0) {
142 ss <<
"and l.date <= (timestamp '" << end_date <<
"') ";
145 ss <<
"and l.priority >= " << priority <<
" ";
147 ss <<
"order by l.id desc ";
148 if (max > 0) ss <<
"limit " << max;
149 db.execute(ss.str().c_str());
150 DBRecordList record_v(db.loadRecords());
151 for (
size_t i = 0; i < record_v.size(); i++) {
154 (LogFile::Priority)(record.getInt(
"priority")),
155 record.get(
"message"),
Date(record.getInt(
"date"))));
158 LogFile::error(e.what());
Abstract base class for different kinds of events.