24{
25 try {
26 if (!db.isConnected()) db.connect();
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
62 if (record.size() > 0) {
63 id = record[0].getInt("id");
64
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
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());
77 LogFile::error(e.what());
78 return false;
79 }
80 return true;
81}