Skip to content

Commit

Permalink
sample(DBLogger): Configure demo SQL channel in properties file.
Browse files Browse the repository at this point in the history
  • Loading branch information
matejk committed Nov 14, 2024
1 parent 94ebcfa commit 04b3db1
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 33 deletions.
4 changes: 0 additions & 4 deletions Data/SQLite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ else()
POCO_SOURCES(SQLITE_SRCS sqlite3
src/sqlite3.c
)

POCO_HEADERS(SQLITE_SRCS sqlite3
src/sqlite3.h
)
endif()

# Version Resource
Expand Down
2 changes: 2 additions & 0 deletions Data/samples/DBLogger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
49 changes: 44 additions & 5 deletions Data/samples/DBLogger/DBLogger.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

#
# Source parameters:
#
# (Mandatory) Directory to scan for .sql files
# Database inserter (destination)
#
DBLogger.dir = .

#
# Destination database parameters:
Expand All @@ -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

53 changes: 34 additions & 19 deletions Data/samples/DBLogger/src/DBLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ class DBLogger: public ServerApplication

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

DBLogger() = default;
DBLogger()
{
Poco::Data::SQLite::Connector::registerConnector();
Poco::Data::SQLChannel::registerChannel();
}

~DBLogger() override = default;

protected:
Expand All @@ -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"))
{
Expand All @@ -81,6 +84,15 @@ class DBLogger: public ServerApplication

if (_demoMessagesRequested)
{
auto& dl = Poco::Logger::get("SQLDemo");
auto* sqlChannel = dynamic_cast<Poco::Data::SQLChannel*>(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);

Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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(
Expand Down Expand Up @@ -201,17 +214,21 @@ class DBLogger: public ServerApplication

}


void handleHelp(const std::string& name, const std::string& value)
{
_helpRequested = true;
displayHelp();
stopOptionsProcessing();
}


void handleCreateDemoMessages(const std::string& name, const std::string& value)
{
config().setBool("DBLogger.demo", true);
}


void displayHelp()
{
HelpFormatter helpFormatter(options());
Expand All @@ -220,26 +237,25 @@ 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;
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}


int main(const std::vector<std::string>& args) override
{
if (!_helpRequested)
Expand All @@ -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<Poco::Data::SQLChannel> _sqlChannel;
std::string _tableName{"T_POCO_LOG"};
};


Expand Down
8 changes: 5 additions & 3 deletions Data/samples/DBLogger/src/SQLLogInserter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ void SQLLogInserter::stop()
w.join();
}
}

_dataSession->close();
}


Expand Down Expand Up @@ -125,7 +127,7 @@ std::string SQLLogInserter::popEntry()
}


void SQLLogInserter::removeEntry(std::string entry)
void SQLLogInserter::removeEntry(const std::string& entry)
{
std::unique_lock<std::mutex> l(_workMutex);
auto i = _processingSet.find(entry);
Expand All @@ -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())
{
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Data/samples/DBLogger/src/SQLLogInserter.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class SQLLogInserter

std::size_t insertEntries(std::vector<std::string>& 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();
Expand Down

0 comments on commit 04b3db1

Please sign in to comment.