Skip to content

Commit

Permalink
sample(DBLogger): Create messages via SQL logger in a thread (#4750)
Browse files Browse the repository at this point in the history
  • Loading branch information
matejk committed Nov 6, 2024
1 parent aab19a9 commit 569b189
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
63 changes: 56 additions & 7 deletions Data/samples/DBLogger/src/DBLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Poco/DirectoryIterator.h"
#include "Poco/DirectoryWatcher.h"
#include "Poco/Glob.h"
#include "Poco/Thread.h"
#include "Poco/Util/Application.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionException.h"
Expand Down Expand Up @@ -64,11 +65,25 @@ class DBLogger: public Application
_sqlChannel = new Poco::Data::SQLChannel();
_sqlChannel->setProperty("directory", _directory);

_active = true;
_startTime.update();

_sqlSourceThread.startFunc(
[this]() { this->createMessages(); }
);

logger().information("Scanning started: %s", _dirWatcher->directory().absolutePath());
}

void uninitialize()
{
_sqlSourceThread.join();

logger().information(
"Created %z messages, processed %z messages in %Ld ms.",
_created, _processed, (_startTime.elapsed() / 1000)
);

Application::uninitialize();
}

Expand Down Expand Up @@ -126,7 +141,7 @@ class DBLogger: public Application

void onItemAdded(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
logger().information("Added: %s", ev.item.path());
logger().trace("Added: %s", ev.item.path());
processFile(ev.item);
}

Expand All @@ -138,18 +153,18 @@ class DBLogger: public Application

void onItemMovedFrom(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
logger().information("Moved from: %s", ev.item.path());
logger().trace("Moved from: %s", ev.item.path());
processFile(ev.item);
}

void onItemMovedTo(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
logger().information("Moved to: %s", ev.item.path());
logger().trace("Moved to: %s", ev.item.path());
}

void onItemRemoved(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
logger().information("Removed: %s", ev.item.path());
logger().trace("Removed: %s", ev.item.path());
}

void processFile(const Poco::File& file)
Expand All @@ -160,16 +175,24 @@ class DBLogger: public Application

if (!file.isFile())
{
logger().information("Not a file: %s", file.absolutePath());
logger().trace("Not a file: %s", file.absolutePath());
return;
}
if (!_sqlNameGlob.match(file.absolutePath()))
{
logger().information("Not an SQL file: %s", file.absolutePath());
logger().trace("Not an SQL file: %s", file.absolutePath());
return;
}
logger().information("Will process: %s", file.absolutePath());
logger().debug("Will process: %s", file.absolutePath());

Poco::File f(file);
if (!f.exists())
{
logger().information("File does not exist: %s", file.absolutePath());
return;
}
f.remove();
++_processed;
}

void scanDirectory()
Expand All @@ -184,22 +207,48 @@ class DBLogger: public Application
}
}

void createMessages()
{
int i {0};
while (_active)
{
for (int j = 0; j < 100 && _active; ++j)
{
Poco::Message msg("SQL Source", Poco::format("%d Informational message", i), Poco::Message::PRIO_INFORMATION);
_sqlChannel->log(msg);
++i;
++_created;
}
Poco::Thread::sleep(50);
}
}

int main(const std::vector<std::string>& args)
{
if (!_helpRequested)
{
logger().information("Press any key to stop scanning.");
std::cin.get();

_active = false;
}
return Application::EXIT_OK;
}

private:
bool _helpRequested;
bool _active {false};

std::size_t _created{0};
std::size_t _processed{0};

Poco::Timestamp _startTime;

std::string _directory;
Poco::Glob _sqlNameGlob;
std::shared_ptr<Poco::DirectoryWatcher> _dirWatcher;
Poco::AutoPtr<Poco::Data::SQLChannel> _sqlChannel;
Poco::Thread _sqlSourceThread;
};


Expand Down
1 change: 1 addition & 0 deletions Data/samples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ projects:
$(MAKE) -C RowFormatter $(MAKECMDGOALS)
$(MAKE) -C Tuple $(MAKECMDGOALS)
$(MAKE) -C WebNotifier $(MAKECMDGOALS)
$(MAKE) -C DBLogger $(MAKECMDGOALS)
2 changes: 1 addition & 1 deletion Foundation/include/Poco/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class Foundation_API Thread: private ThreadImpl
/// The Thread ensures that the given target stays
/// alive while the thread is running.

void start(Callable target, void* pData = 0);
void start(Callable target, void* pData = nullptr);
/// Starts the thread with the given target and parameter.

template <class Functor>
Expand Down
2 changes: 1 addition & 1 deletion Foundation/src/Timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void Timestamp::update()
#else

struct timeval tv;
if (gettimeofday(&tv, NULL))
if (gettimeofday(&tv, nullptr))
throw SystemException("cannot get time of day");
_ts = TimeVal(tv.tv_sec)*resolution() + tv.tv_usec;

Expand Down

0 comments on commit 569b189

Please sign in to comment.