Quick Start – Tracing in an SNMP Agent
NuDesign Visual xAgentBuilder provides easy way to debug SNMP communications. In main.cpp of the “EXE” project
simply uncomment
//#define NDTRACE 1
and rebuild the console agent. When the a message arrives at the agent, console output should look like:
ReceiveData: [1] <– from 127.0.0.1:1134
ProcessRcvdData [get,32] <– from 127.0.0.1:1134
ReturnResponsePdu [response,32] to –> 127.0.0.1:1134
By defining the NDTRACE macro the following two lines are included in the build:
BMsgLogStdOut dbgLogStdout;
NDSNMPTRACE_SUBSCRIBE(&dbgLogStdout);
BMsgLogStdOut is class that implements IMessageSubscriber interface. This interface defines a single method:
void Inform(const BMessage& msg);
The message dispatcher calls this method for each subscriber of this particular type of message. NDSNMPTRACE_SUBSCRIBE macro is actually a replacement for the function NDSnmpTraceSubscribe. Along with registering with the message dispatcher, it also sets the trace level to 1 (print only incoming/outgoing addresses, as shown in the example above).
The trace level can be changed while the agent is running. Set the level to zero to disable tracing, and to 2 to print raw packets along with the address info. Here is an example:
NDSNMPTRACE_SETLEVEL(2);
If the agent is running as a service (i.e. does not have stdout), we can use a different subscriber. For example, this one will log everything to a file:
class MsgLog : public IMessageSubscriber
{
public:
MsgLog(const tChar* pszFile)
: fpLog(NULL)
{
fpLog = NDFopen(pszFile, “a”); // append!
}
~MsgLog()
{
if (fpLog)
NDFclose(fpLog);
}
// Implementation of IMessageSubscriber interface
void Inform(const BMessage& msg)
{
TTraceGuard guard;
if (fpLog)
NDFprintf(fpLog, “%s\n”, msg.text.c_str());
}
private:
FILE* fpLog;
};
In (service) main
MsgLog dbgLogFile(“trace.txt”);
NDSNMPTRACE_SUBSCRIBE(&dbgLogFile);
Note that we can have multiple subscribers at the same time:
BMsgLogStdOut dbgLogStdout;
MsgLog dbgLogFile(“trace.txt”);
NDSNMPTRACE_SUBSCRIBE(&dbgLogStdout);
NDSNMPTRACE_SUBSCRIBE(&dbgLogFile);
What if we want to trace our own messages? Simply create the message and “publish” it:
BSnmpTraceMsg msg;
msg.text = “my message”;
BMessageDispatcher::Instance()->Publish(msg);
Please proceed to Quick Start – Using Remote SubAgents.