Skip to content

Commit

Permalink
sample(DBLogger): Add example DBLogger.properties file.
Browse files Browse the repository at this point in the history
  • Loading branch information
matejk committed Nov 12, 2024
1 parent 09001b9 commit e627a51
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
28 changes: 28 additions & 0 deletions Data/samples/DBLogger/DBLogger.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# Source parameters:
#
# (Mandatory) Directory to scan for .sql files
#
DBLogger.dir =

#
# Destination database parameters:
#
# (Mandatory) Database connector (for example sqlite)
#
DBLogger.connector =

#
# (Mandatory) Database connection string (connector specific)
#
DBLogger.constring =

#
# (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 = false
27 changes: 12 additions & 15 deletions Data/samples/DBLogger/src/DBLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

#include "Poco/Data/SQLChannel.h"
#include "Poco/Data/SQLite/Connector.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionException.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Util/ServerApplication.h"

#include "SQLLogInserter.h"
Expand All @@ -36,14 +36,8 @@ class DBLogger: public ServerApplication

using WorkSet = std::unordered_set<std::string>;

DBLogger():
_helpRequested(false)
{
}

~DBLogger() override
{
}
DBLogger() = default;
~DBLogger() override = default;

protected:
void initialize(Application& self) override
Expand Down Expand Up @@ -79,6 +73,7 @@ class DBLogger: public ServerApplication
_inserter.setConnectionString( cfg.getString("DBLogger.constring") );

_inserter.setNumWorkers( cfg.getUInt64("DBLogger.workers", 2) );
_demoMessagesRequested = cfg.getBool("DBLogger.demo", false);

logger().information("Directory: %s", _inserter.directory());
logger().information("Database: %s, %s", _inserter.connector(), _inserter.connectionString());
Expand All @@ -87,10 +82,7 @@ class DBLogger: public ServerApplication
if (_demoMessagesRequested)
{
// Only delete and create table when creating demo messages
logger().information("Database connector: %s, cs: %s",
_inserter.connector(), _inserter.connectionString());
logger().information("Directory: %s", _inserter.directory());
logger().information("Number of workers: %z", _inserter.numWorkers());
logger().information("Demo messages: Creating new table: %s", _tableName);

Poco::Data::Session session (_inserter.connector(), _inserter.connectionString());

Expand Down Expand Up @@ -199,6 +191,7 @@ class DBLogger: public ServerApplication
Option("demo", "m", "create demo messages")
.required(false)
.repeatable(false)
.binding("DBLogger.demo")
.callback(OptionCallback<DBLogger>(this, &DBLogger::handleCreateDemoMessages)));

}
Expand All @@ -212,7 +205,7 @@ class DBLogger: public ServerApplication

void handleCreateDemoMessages(const std::string& name, const std::string& value)
{
_demoMessagesRequested = true;
config().setString("DBLogger.demo", "true");
}
void displayHelp()
{
Expand All @@ -229,7 +222,11 @@ class DBLogger: public ServerApplication
{
for (int j = 0; j < 100 && _active; ++j)
{
Poco::Message msg("SQL Source", Poco::format("%d Informational message", i), Poco::Message::PRIO_INFORMATION);
const Poco::Message msg(
"SQL Source",
Poco::format("%d Informational message", i),
Poco::Message::PRIO_INFORMATION
);
_sqlChannel->log(msg);
++i;
++_created;
Expand Down
13 changes: 7 additions & 6 deletions Data/samples/DBLogger/src/SQLLogInserter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@

#include "SQLLogInserter.h"

#include "Poco/Util/Application.h"

#include <iostream>
#include <filesystem>
#include <fstream>
#include <iostream>

using namespace Poco::Data::Keywords;

Expand Down Expand Up @@ -150,7 +148,7 @@ void SQLLogInserter::processFile(std::string& entry)
bool success {false};
try
{
std::ifstream is(entry);
const std::ifstream is(entry);
std::stringstream buffer;
buffer << is.rdbuf();
const auto& sql { buffer.str() };
Expand Down Expand Up @@ -187,8 +185,11 @@ std::size_t SQLLogInserter::scanDirectory()
{
std::vector<std::string> newEntries;
newEntries.reserve(1000);
std::filesystem::directory_iterator diriter(_directory, std::filesystem::directory_options::skip_permission_denied);
for (auto& entry: diriter)
const std::filesystem::directory_iterator diriter(
_directory,
std::filesystem::directory_options::skip_permission_denied
);
for (const auto& entry: diriter)
{
if (!_active)
{
Expand Down

0 comments on commit e627a51

Please sign in to comment.