From 04b3db1f6c21999814c00a7effcd96a6adcd9a3f Mon Sep 17 00:00:00 2001 From: Matej Kenda Date: Thu, 14 Nov 2024 12:11:49 +0100 Subject: [PATCH] sample(DBLogger): Configure demo SQL channel in properties file. --- Data/SQLite/CMakeLists.txt | 4 -- Data/samples/DBLogger/CMakeLists.txt | 2 + Data/samples/DBLogger/DBLogger.properties | 49 ++++++++++++++++-- Data/samples/DBLogger/src/DBLogger.cpp | 53 +++++++++++++------- Data/samples/DBLogger/src/SQLLogInserter.cpp | 8 +-- Data/samples/DBLogger/src/SQLLogInserter.h | 4 +- 6 files changed, 87 insertions(+), 33 deletions(-) diff --git a/Data/SQLite/CMakeLists.txt b/Data/SQLite/CMakeLists.txt index 263acc1ad4..7141112e63 100644 --- a/Data/SQLite/CMakeLists.txt +++ b/Data/SQLite/CMakeLists.txt @@ -13,10 +13,6 @@ else() POCO_SOURCES(SQLITE_SRCS sqlite3 src/sqlite3.c ) - - POCO_HEADERS(SQLITE_SRCS sqlite3 - src/sqlite3.h - ) endif() # Version Resource diff --git a/Data/samples/DBLogger/CMakeLists.txt b/Data/samples/DBLogger/CMakeLists.txt index aaae453d27..74fa2f6799 100644 --- a/Data/samples/DBLogger/CMakeLists.txt +++ b/Data/samples/DBLogger/CMakeLists.txt @@ -10,3 +10,5 @@ POCO_HEADERS_AUTO(DBLOGGER_SRCS ${HDRS_G}) add_executable(DBLogger ${DBLOGGER_SRCS}) target_link_libraries(DBLogger PUBLIC Poco::Util Poco::DataSQLite) + +add_custom_target(DBLogger-properties SOURCES DBLogger.properties) diff --git a/Data/samples/DBLogger/DBLogger.properties b/Data/samples/DBLogger/DBLogger.properties index 45c06c8028..be72e27307 100644 --- a/Data/samples/DBLogger/DBLogger.properties +++ b/Data/samples/DBLogger/DBLogger.properties @@ -1,9 +1,7 @@ + # -# Source parameters: -# -# (Mandatory) Directory to scan for .sql files +# Database inserter (destination) # -DBLogger.dir = . # # Destination database parameters: @@ -15,14 +13,55 @@ DBLogger.connector = sqlite # # (Mandatory) Database connection string (connector specific) # -DBLogger.constring = demo-logs.sqlite +DBLogger.constring = demo-logs.db3 # # (Optional) Number of workers to insert logs into the database in parallel (default = 2) # # DBLogger.workers = 2 +# ---------------------------------------------------------------- + # # (Optional) Create demo SQL messages for testing purposes (default = false) # DBLogger.demo = true + +logging.loggers.root.channel = console +logging.loggers.root.level = information + +logging.channels.console.class = ColorConsoleChannel +logging.channels.console.pattern = %L%Y-%m-%d %H:%M:%S.%i [%p] : %t + +# +# SQL logger to create demo messages +# NOTE: Do not rename the logger. +# + +logging.loggers.sqldemo.channel = sql +logging.loggers.sqldemo.name = SQLDemo +logging.loggers.sqldemo.level = debug + +logging.channels.sql.class = SQLChannel +logging.channels.sql.name = DBLogger + +# +# (Mandatory) Directory to scan for .sql files +# +logging.channels.sql.directory = . + +# +# NOTE: Do not configure database to create SQL logger files +# +# logging.channels.sql.connector = SQLite +# logging.channels.sql.connect = DBLogger.db3 + +logging.channels.sql.timeout = 3000 +logging.channels.sql.minBatch = 5 +logging.channels.sql.maxBatch = 1000 +logging.channels.sql.bulk = true +logging.channels.sql.table = T_LOG + +logging.channels.sql.throw = false +logging.channels.sql.file = ${application.baseName}.sql + diff --git a/Data/samples/DBLogger/src/DBLogger.cpp b/Data/samples/DBLogger/src/DBLogger.cpp index 71e632ad3a..69342798cf 100644 --- a/Data/samples/DBLogger/src/DBLogger.cpp +++ b/Data/samples/DBLogger/src/DBLogger.cpp @@ -36,7 +36,12 @@ class DBLogger: public ServerApplication using WorkSet = std::unordered_set; - DBLogger() = default; + DBLogger() + { + Poco::Data::SQLite::Connector::registerConnector(); + Poco::Data::SQLChannel::registerChannel(); + } + ~DBLogger() override = default; protected: @@ -50,15 +55,13 @@ class DBLogger: public ServerApplication loadConfiguration(); // load default configuration files, if present Application::initialize(self); - Poco::Data::SQLite::Connector::registerConnector(); - const auto& cfg { config() }; - if (!cfg.has("DBLogger.dir")) + if (!cfg.has("logging.channels.sql.directory")) { throw Poco::Util::MissingOptionException ("Missing scanning directory."); } - _inserter.setDirectory( cfg.getString("DBLogger.dir") ); + _inserter.setDirectory( cfg.getString("logging.channels.sql.directory") ); if (!cfg.has("DBLogger.connector")) { @@ -81,6 +84,15 @@ class DBLogger: public ServerApplication if (_demoMessagesRequested) { + auto& dl = Poco::Logger::get("SQLDemo"); + auto* sqlChannel = dynamic_cast(dl.getChannel().get()); + if (sqlChannel == nullptr) + { + throw Poco::Util::UnexpectedArgumentException ("SQLDemo logger does not have SQL channel."); + } + + _tableName = cfg.getString("logging.channels.sql.table"); + // Only delete and create table when creating demo messages logger().information("Demo messages: Creating new table: %s", _tableName); @@ -110,22 +122,21 @@ class DBLogger: public ServerApplication if (_demoMessagesRequested) { - // SQL channel to generate SQL files - _sqlChannel = new Poco::Data::SQLChannel(); - _sqlChannel->setProperty(Poco::Data::SQLChannel::PROP_DIRECTORY, _inserter.directory()); - _sqlChannel->setProperty(Poco::Data::SQLChannel::PROP_TABLE, _tableName); - _sqlSourceThread = std::move(std::thread(&DBLogger::createMessages, this)); logger().information("Started creating demo messages."); } } + void uninitialize() override { if (_helpRequested) { return; } + + logger().information("Request to stop processing."); + if (_demoMessagesRequested) { _sqlSourceThread.join(); @@ -140,11 +151,13 @@ class DBLogger: public ServerApplication Application::uninitialize(); } + void reinitialize(Application& self) override { Application::reinitialize(self); } + void defineOptions(OptionSet& options) override { Application::defineOptions(options); @@ -167,7 +180,7 @@ class DBLogger: public ServerApplication Option("dir", "d", "directory path to scan for SQL log files") .repeatable(false) .argument("dir") - .binding("DBLogger.dir") + .binding("logging.channels.sql.directory") ); options.addOption( @@ -201,6 +214,7 @@ class DBLogger: public ServerApplication } + void handleHelp(const std::string& name, const std::string& value) { _helpRequested = true; @@ -208,10 +222,13 @@ class DBLogger: public ServerApplication stopOptionsProcessing(); } + void handleCreateDemoMessages(const std::string& name, const std::string& value) { config().setBool("DBLogger.demo", true); } + + void displayHelp() { HelpFormatter helpFormatter(options()); @@ -220,19 +237,17 @@ class DBLogger: public ServerApplication helpFormatter.format(std::cout); } + void createMessages() { int i {0}; + auto& dl = Poco::Logger::get("SQLDemo"); + while (_active) { for (int j = 0; j < 100 && _active; ++j) { - const Poco::Message msg( - "SQL Source", - Poco::format("%d Informational message", i), - Poco::Message::PRIO_INFORMATION - ); - _sqlChannel->log(msg); + dl.debug("%d Informational message", i); ++i; ++_created; } @@ -240,6 +255,7 @@ class DBLogger: public ServerApplication } } + int main(const std::vector& args) override { if (!_helpRequested) @@ -262,8 +278,7 @@ class DBLogger: public ServerApplication std::size_t _created{0}; std::thread _sqlSourceThread; - const std::string _tableName{"T_POCO_LOG"}; - Poco::AutoPtr _sqlChannel; + std::string _tableName{"T_POCO_LOG"}; }; diff --git a/Data/samples/DBLogger/src/SQLLogInserter.cpp b/Data/samples/DBLogger/src/SQLLogInserter.cpp index eeac7908e3..e7310f43bf 100644 --- a/Data/samples/DBLogger/src/SQLLogInserter.cpp +++ b/Data/samples/DBLogger/src/SQLLogInserter.cpp @@ -70,6 +70,8 @@ void SQLLogInserter::stop() w.join(); } } + + _dataSession->close(); } @@ -125,7 +127,7 @@ std::string SQLLogInserter::popEntry() } -void SQLLogInserter::removeEntry(std::string entry) +void SQLLogInserter::removeEntry(const std::string& entry) { std::unique_lock l(_workMutex); auto i = _processingSet.find(entry); @@ -136,7 +138,7 @@ void SQLLogInserter::removeEntry(std::string entry) } -void SQLLogInserter::processFile(std::string& entry) +void SQLLogInserter::processFile(const std::string &entry) { if (entry.empty()) { @@ -228,7 +230,7 @@ void SQLLogInserter::runDirectoryScan() if (!_active && !_doneProcessing) { // Last scan directory to clean up - scheduled = scanDirectory(); + (void)scanDirectory(); _doneProcessing = true; _workCondition.notify_all(); break; diff --git a/Data/samples/DBLogger/src/SQLLogInserter.h b/Data/samples/DBLogger/src/SQLLogInserter.h index ae7228e2ee..2308169f48 100644 --- a/Data/samples/DBLogger/src/SQLLogInserter.h +++ b/Data/samples/DBLogger/src/SQLLogInserter.h @@ -52,8 +52,8 @@ class SQLLogInserter std::size_t insertEntries(std::vector& entries); std::string popEntry(); - void removeEntry(std::string entry); - void processFile(std::string& entry); + void removeEntry(const std::string &entry); + void processFile(const std::string& entry); std::size_t scanDirectory(); void runDirectoryScan();