[AGENT++] Log Messages
Michelsen Robert
Robert.Michelsen at vitk.vossloh.com
Thu Nov 20 10:36:17 CET 2003
Hi,
>Is there a simple way to redirect Agent++ log messages into an application's own >logic. For instance, my application has its own logging subsystem and it runs >as a daemon. Is there a good way to grab Agent++'s logging messages on their >way to stdout?
i had the same problem (log4cpp)
I implemented the following technique (looking at log.h and log.cpp):
--------
derive from "AgentLogImpl":
e.g. class AgentppToLog4cpp : public AgentLogImpl
override
virtual LogEntry* create_log_entry(unsigned char _type) const;
virtual AgentLog& operator+=(const LogEntry* log);
--------
derive from "LogEntryImpl":
class Log4cppLogEntryImpl : public LogEntryImpl
override
Log4cppLogEntryImpl( unsigned char _type) : LogEntryImpl( _type) {}
virtual ~Log4cppLogEntryImpl() {};
virtual void init(void) {}
to customize the log entry initialization itself (like you want)
--------
In "create_log_entry" you return an instance of (your own) LogEntryImpl class impl
e.g.
LogEntry* AgentppToLog4cpp::create_log_entry( unsigned char t) const
{
// return our own LogEntryImpl instance (message text only)
return new Log4cppLogEntryImpl (t);
}
by overriding "operator+=" you can reroute the whole stuff to your own logging hierarchy.
e.g.
AgentLog& AgentppToLog4cpp::operator+=( const LogEntry* log)
{
switch( log->get_class())
{
case ERROR_LOG:
sm_Logger.error( "%s\n", log->get_value());
break;
case WARNING_LOG:
sm_Logger.warn( "%s\n", log->get_value());
break;
// and so forth ....
// (sm_Logger is a static instance of my own logger)
}
}
--------
Before you initialize the Agent++/SNMP++ network subsystems, initialize the logging with your own log impl:
DefaultLog::init( new Agentpp2Log4cpp());
Be aware when instanciating any agent++ objects (like MIB -> ThreadPool -> TaskManager on calling LOG_BEGIN) before the logging they will initialize the default (static logger) with default agent++ impl resulting in "loss" of initial messages.
Be aware of global (static) object destruction order, keep your (custom) logging system alive as long as any (static) agent++ instances live (that might call LOG).
There might be better ways but this worked for me...
Regards,
Robert Michelsen
More information about the AGENTPP
mailing list