SyslogChannel example for linux syslog #4228
-
Does anyone have an example for using SyslogChannel on a linux platform to get log messages sent to the syslog? I have a simple server application that I either run as a regular command line for testing or with the --daemon option to run as a background task. Here's a snippet from one attempt (not that messages always appear nicely in the terminal window, just not in the syslog) void initialize(Application& self)
{
loadConfiguration(); // load default configuration files, if present
ServerApplication::initialize(self);
AutoPtr<SyslogChannel> pChannel(new SyslogChannel("bridge"));
//Logger::root().setChannel(pChannel);
Logger& sLog = Logger::create("LogChan", pChannel, Message::PRIO_TRACE);
Logger& logger1 = Logger::get("logChan");
logger().information("starting up");
logger1.information("starting up");
if (config().getBool("application.runAsDaemon", false))
{
logger().information("We are running as a daemon!");
Logger::get("logChan").information("We are running as a daemon!");
}
else
{
logger().information("We are running as an ordinary process!");
Logger::get("logChan").information("We are running as an ordinary process!");
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
It was quite easy in the end.... Note sure why open() had to be called on the newly created channel when it isn't in the POCO logging examples, but this is what was missing. Here is a full listing of a the server application sample program with the few extra bits in to make it log to the linux syslog which it will do if run as a regular command line app or as a daemon with the --daemon option: //
// SampleServer.cpp
//
// This sample demonstrates the ServerApplication class.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Util/ServerApplication.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Task.h"
#include "Poco/TaskManager.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/Logger.h"
#include "Poco/SyslogChannel.h"
#include "Poco/AutoPtr.h"
#include "Poco/Message.h"
#include <iostream>
using Poco::Util::Application;
using Poco::Util::ServerApplication;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::OptionCallback;
using Poco::Util::HelpFormatter;
using Poco::Task;
using Poco::TaskManager;
using Poco::DateTimeFormatter;
using Poco::AutoPtr;
using Poco::SyslogChannel;
using Poco::Logger;
using Poco::Message;
class SampleTask: public Task
{
public:
SampleTask(): Task("SampleTask")
{
}
void runTask()
{
Application& app = Application::instance();
while (!sleep(5000))
{
Logger::get("logChan").information("busy doing nothing... " + DateTimeFormatter::format(app.uptime()));
}
}
};
class SampleServer: public ServerApplication
{
public:
SampleServer(): _helpRequested(false)
{
}
~SampleServer()
{
}
protected:
void initialize(Application& self)
{
loadConfiguration(); // load default configuration files, if present
ServerApplication::initialize(self);
AutoPtr<SyslogChannel> pChannel(new SyslogChannel("bridge"));
pChannel->open();
Logger::root().setChannel(pChannel);
Logger& log = Logger::get("logChan");
log.information("starting up");
if (config().getBool("application.runAsDaemon", false))
{
log.information("We are running as a daemon!");
}
else
{
log.information("We are running as an ordinary process!");
}
}
void uninitialize()
{
logger().information("shutting down");
ServerApplication::uninitialize();
}
void defineOptions(OptionSet& options)
{
ServerApplication::defineOptions(options);
options.addOption(
Option("help", "h", "display help information on command line arguments")
.required(false)
.repeatable(false)
.callback(OptionCallback<SampleServer>(this, &SampleServer::handleHelp)));
}
void handleHelp(const std::string& name, const std::string& value)
{
_helpRequested = true;
displayHelp();
stopOptionsProcessing();
}
void displayHelp()
{
HelpFormatter helpFormatter(options());
helpFormatter.setCommand(commandName());
helpFormatter.setUsage("OPTIONS");
helpFormatter.setHeader("A sample server application that demonstrates some of the features of the Util::ServerApplication class.");
helpFormatter.format(std::cout);
}
int main(const ArgVec& args)
{
if (!_helpRequested)
{
TaskManager tm;
tm.start(new SampleTask);
waitForTerminationRequest();
tm.cancelAll();
tm.joinAll();
}
return Application::EXIT_OK;
}
private:
bool _helpRequested;
};
POCO_SERVER_MAIN(SampleServer) |
Beta Was this translation helpful? Give feedback.
It was quite easy in the end....
Note sure why open() had to be called on the newly created channel when it isn't in the POCO logging examples, but this is what was missing. Here is a full listing of a the server application sample program with the few extra bits in to make it log to the linux syslog which it will do if run as a regular command line app or as a daemon with the --daemon option: