Leosac  0.8.0
Open Source Access Control
DatabaseLogSink.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "DatabaseLogSink.hpp"
21 #include "GenGuid.h"
22 #include "log.hpp"
24 #include "tools/LogEntry_odb.h"
26 #include "tools/db/database.hpp"
27 
28 using namespace Leosac;
29 using namespace Leosac::Tools;
30 
32  : database_(database)
33 {
34  std::cout << "ENABLING SQL DATABASE LOGGER." << std::endl;
35  ASSERT_LOG(database_, "No database object.");
36  // Generate a "run id"
38 }
39 
40 void DatabaseLogSink::log(const spdlog::details::log_msg &msg)
41 {
42  LogEntry entry;
43 
44  entry.level_ = msg.level;
45  entry.msg_ = msg.formatted.str();
46  entry.thread_id_ = msg.thread_id;
47  entry.timestamp_ = time_point_ptime(msg.time);
48  entry.run_id_ = run_id_;
49 
50  try
51  {
52  using namespace odb;
53  using namespace odb::core;
55 
56  database_->persist(entry);
57  t.commit();
58  }
59  catch (const odb::exception &e)
60  {
61  std::cerr << "DatabaseLogSink encountered odb::exception: " << e.what()
62  << std::endl;
63  }
64 }
Leosac::Tools::LogEntry::thread_id_
size_t thread_id_
Definition: LogEntry.hpp:66
time_point_ptime
boost::posix_time::ptime time_point_ptime(const std::chrono::time_point< std::chrono::system_clock, Duration > &from)
Definition: DateTimeConverter.hpp:26
database.hpp
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
odb
Provide ODB magic to be able to store an Leosac::Audit::EventType (FlagSet) object.
Definition: AuditEventMaskODB.hpp:31
DatabaseLogSink.hpp
Leosac::Tools::LogEntry::msg_
std::string msg_
Definition: LogEntry.hpp:52
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::db::MultiplexedTransaction::commit
void commit()
Commit the transaction.
Definition: MultiplexedTransaction.cpp:47
MultiplexedTransaction.hpp
Leosac::db::MultiplexedTransaction
Acts like an odb::transaction, with the exception that it will becomes the active transaction at cons...
Definition: MultiplexedTransaction.hpp:35
Leosac::Tools::LogEntry::level_
uint8_t level_
Definition: LogEntry.hpp:63
Leosac::Tools::LogEntry::timestamp_
boost::posix_time::ptime timestamp_
Definition: LogEntry.hpp:49
GenGuid.h
Leosac::Tools::LogEntry::run_id_
std::string run_id_
The run_id is generated when Leosac starts, and it is used to identify "runs" (ie if Leosac restarts)...
Definition: LogEntry.hpp:60
Leosac::gen_uuid
std::string gen_uuid()
Generate a new UUID.
Definition: GenGuid.cpp:26
Leosac::Tools::DatabaseLogSink::log
virtual void log(const spdlog::details::log_msg &msg) override
Definition: DatabaseLogSink.cpp:40
Leosac::Tools::LogEntry
A log entry.
Definition: LogEntry.hpp:40
log.hpp
Leosac::Tools::DatabaseLogSink::database_
DBPtr database_
Definition: DatabaseLogSink.hpp:50
DateTimeConverter.hpp
Leosac::Tools
Definition: DatabaseLogSink.hpp:27
Leosac::Tools::DatabaseLogSink::DatabaseLogSink
DatabaseLogSink(DBPtr database)
Construct a SQLite backed log sink.
Definition: DatabaseLogSink.cpp:31
Leosac::Tools::DatabaseLogSink::run_id_
std::string run_id_
Definition: DatabaseLogSink.hpp:51