Belle II Software development
DAQLogDB.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include "daq/slc/database/DAQLogDB.h"
9
10#include <daq/slc/system/LogFile.h>
11
12#include <daq/slc/base/StringUtil.h>
13#include <daq/slc/database/DBHandlerException.h>
14
15#include <iostream>
16#include <sstream>
17#include <cstdio>
18#include <stdexcept>
19
20using namespace Belle2;
21
22bool DAQLogDB::createLog(DBInterface& db, const std::string& tablename,
23 const DAQLogMessage& log)
24{
25 try {
26 if (!db.isConnected()) db.connect();
27 } catch (const DBHandlerException& e) {
28 LogFile::error(e.what());
29 return false;
30 }
31 try {
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"
37 "UNIQUE(name));");
38 db.execute("create index daqlog_id_index on daqlog(id);",
39 tablename.c_str(), tablename.c_str());
40 }
41 if (!db.checkTable(tablename_date)) {
42 try {
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"
48 "id bigserial, \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());
54 }
55 db.execute("create index %s_id_index on %s(id);",
56 tablename_date.c_str(), tablename_date.c_str());
57 }
58 db.execute("select id,category from log_node where name = '" + log.getNodeName() + "';");
59 DBRecordList record(db.loadRecords());
60 int id = 1;
61 //int cid = 1;
62 if (record.size() > 0) {
63 id = record[0].getInt("id");
64 //cid = record[0].getInt("category");
65 } else {
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");
70 //cid = record[0].getInt("category");
71 }
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());
76 } catch (const DBHandlerException& e) {
77 LogFile::error(e.what());
78 return false;
79 }
80 return true;
81}
82
83DAQLogMessageList DAQLogDB::getLogs(DBInterface& db, const std::string& tablename,
84 const std::string& nodename, int max)
85{
86 std::string tablename_date = tablename + "_" + Date().toString("%Y");
87 DAQLogMessageList logs;
88 try {
89 if (!db.isConnected()) db.connect();
90 if (nodename.size() > 0) {
91 if (max > 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);
95 } else {
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());
99 }
100 } else {
101 if (max > 0) {
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);
105 } else {
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());
109 }
110 }
111 DBRecordList record_v(db.loadRecords());
112 for (size_t i = 0; i < record_v.size(); i++) {
113 DBRecord& record(record_v[i]);
114 logs.push_back(DAQLogMessage(record.get("n.name"),
115 (LogFile::Priority)(record.getInt("priority")),
116 record.get("l.message"), Date(record.getInt("date"))));
117 }
118 } catch (const DBHandlerException& e) {
119 LogFile::error(e.what());
120 }
121 return logs;
122}
123
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)
127{
128 std::string tablename_date = tablename + "_" + Date().toString("%Y");
129 DAQLogMessageList logs;
130 try {
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 << "' ";
137 }
138 if (begin_date.size() > 0) {
139 ss << "and l.date >= (timestamp '" << begin_date << "') ";
140 }
141 if (end_date.size() > 0) {
142 ss << "and l.date <= (timestamp '" << end_date << "') ";
143 }
144 if (priority > 0) {
145 ss << "and l.priority >= " << priority << " ";
146 }
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++) {
152 DBRecord& record(record_v[i]);
153 logs.push_back(DAQLogMessage(record.get("name"),
154 (LogFile::Priority)(record.getInt("priority")),
155 record.get("message"), Date(record.getInt("date"))));
156 }
157 } catch (const DBHandlerException& e) {
158 LogFile::error(e.what());
159 }
160 return logs;
161}
Abstract base class for different kinds of events.