Belle II Software  release-06-01-15
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 
20 using namespace Belle2;
21 
22 bool DAQLogDB::createLog(DBInterface& db, const std::string& tablename,
23  const DAQLogMessage& log)
24 {
25  std::stringstream ss;
26  try {
27  if (!db.isConnected()) db.connect();
28  } catch (const DBHandlerException& e) {
29  LogFile::error(e.what());
30  return false;
31  }
32  try {
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"
38  "UNIQUE(name));");
39  db.execute("create index daqlog_id_index on daqlog(id);",
40  tablename.c_str(), tablename.c_str());
41  }
42  if (!db.checkTable(tablename_date)) {
43  try {
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"
49  "id bigserial, \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());
55  }
56  db.execute("create index %s_id_index on %s(id);",
57  tablename_date.c_str(), tablename_date.c_str());
58  }
59  db.execute("select id,category from log_node where name = '" + log.getNodeName() + "';");
60  DBRecordList record(db.loadRecords());
61  int id = 1;
62  //int cid = 1;
63  if (record.size() > 0) {
64  id = record[0].getInt("id");
65  //cid = record[0].getInt("category");
66  } else {
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");
71  //cid = record[0].getInt("category");
72  }
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());
77  } catch (const DBHandlerException& e) {
78  LogFile::error(e.what());
79  return false;
80  }
81  return true;
82 }
83 
84 DAQLogMessageList DAQLogDB::getLogs(DBInterface& db, const std::string& tablename,
85  const std::string& nodename, int max)
86 {
87  std::string tablename_date = tablename + "_" + Date().toString("%Y");
88  DAQLogMessageList logs;
89  try {
90  if (!db.isConnected()) db.connect();
91  if (nodename.size() > 0) {
92  if (max > 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);
96  } else {
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());
100  }
101  } else {
102  if (max > 0) {
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);
106  } else {
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());
110  }
111  }
112  DBRecordList record_v(db.loadRecords());
113  for (size_t i = 0; i < record_v.size(); i++) {
114  DBRecord& record(record_v[i]);
115  logs.push_back(DAQLogMessage(record.get("n.name"),
116  (LogFile::Priority)(record.getInt("priority")),
117  record.get("l.message"), Date(record.getInt("date"))));
118  }
119  } catch (const DBHandlerException& e) {
120  LogFile::error(e.what());
121  }
122  return logs;
123 }
124 
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)
128 {
129  std::string tablename_date = tablename + "_" + Date().toString("%Y");
130  DAQLogMessageList logs;
131  try {
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 << "' ";
138  }
139  if (begin_date.size() > 0) {
140  ss << "and l.date >= (timestamp '" << begin_date << "') ";
141  }
142  if (end_date.size() > 0) {
143  ss << "and l.date <= (timestamp '" << end_date << "') ";
144  }
145  if (priority > 0) {
146  ss << "and l.priority >= " << priority << " ";
147  }
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++) {
153  DBRecord& record(record_v[i]);
154  logs.push_back(DAQLogMessage(record.get("name"),
155  (LogFile::Priority)(record.getInt("priority")),
156  record.get("message"), Date(record.getInt("date"))));
157  }
158  } catch (const DBHandlerException& e) {
159  LogFile::error(e.what());
160  }
161  return logs;
162 }
Abstract base class for different kinds of events.