问题
I have written following code to send logs to remote syslog(rsyslog on remote unix machine
) from my windows machine
using boost-log
. I am using syslog_backend
.
#include <boost/config.hpp>
#if !defined(BOOST_WINDOWS)
#define BOOST_LOG_USE_NATIVE_SYSLOG
#endif
#include <string>
#include <iostream>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/syslog_backend.hpp>
namespace logging = boost::log;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
using boost::shared_ptr;
using namespace std;
class AlertsForSyslog
{
string hostname;
string message;
src::severity_logger<> lg;
boost::shared_ptr< sinks::synchronous_sink< sinks::syslog_backend > > sink;
public :
AlertsForSyslog(string hostname);
int sendMessageToSyslog(string message);
void dd();
};
AlertsForSyslog::AlertsForSyslog(string hostname)
{
this->hostname = hostname;
sink = boost::shared_ptr<sinks::synchronous_sink< sinks::syslog_backend >>(new sinks::synchronous_sink< sinks::syslog_backend >());
}
int AlertsForSyslog::sendMessageToSyslog(string message)
{
this->message = message;
sink->set_formatter
(
expr::format("%1%")
%expr::smessage
);
sink->locked_backend()->set_target_address(hostname);
// Add the sink to the core
logging::core::get()->add_sink(sink);
return 0;
}
void AlertsForSyslog::dd()
{
BOOST_LOG_SEV(lg, sinks::syslog::alert) << message ;
}
int main()
{
cout << "hello";
AlertsForSyslog n1 = AlertsForSyslog("172.16.72.239");
n1.sendMessageToSyslog("from n1");
AlertsForSyslog n2 = AlertsForSyslog("172.16.72.239");
n2.sendMessageToSyslog("from n2");
AlertsForSyslog n3 = AlertsForSyslog("172.16.72.239");
n3.sendMessageToSyslog("from n3");
n1.dd();
n2.dd();
n3.dd();
return 0;
}
Above code gives me folowing output in /var/log/messages
of my unix machine:
Apr 4 21:33:58 mypc from n1
Apr 4 21:33:58 mypc from n1
Apr 4 21:33:58 mypc from n1
Apr 4 21:33:58 mypc from n2
Apr 4 21:33:58 mypc from n2
Apr 4 21:33:58 mypc from n3
Apr 4 21:33:58 mypc from n3
Apr 4 21:33:58 mypc from n2
Apr 4 21:33:58 mypc from n3
1) why same message("from n1") is sent over all 3 sinks?
2) My application requirement is : application will start multiple threads and each thread will create an object of AlertsForSyslog
and hence each thread wants to send logs independently.
3) Is there any way I can achieve this? In my case what exactly shared_ptr
is doing? Can someone please help?
I want to write a code which will give output in above case :
Apr 4 21:33:58 mypc from n1
Apr 4 21:33:58 mypc from n2
Apr 4 21:33:58 mypc from n3
can someone please suggest any correction in above code? In my application, instead of main function, thread1 will be creating object n1, thread2 will be creating n2 and so on...
来源:https://stackoverflow.com/questions/55520886/why-syslog-sink-sends-same-records-on-all-the-sinks-when-a-sink-is-member-of-cla