Quick Start – Tracing in an SNMP Agent
NuDesign Visual xAgentBuilder provides easy way to debug SNMP communications. In main.cpp of the “EXE” project
//#define NDTRACE 1
and rebuild the console agent. When the a message arrives at the agent, console output should look like:
ReceiveData:  <– 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 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:
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
MsgLog(const tChar* pszFile)
fpLog = NDFopen(pszFile, “a”); // append!
// Implementation of IMessageSubscriber interface
void Inform(const BMessage& msg)
NDFprintf(fpLog, “%s\n”, msg.text.c_str());
In (service) main
Note that we can have multiple subscribers at the same time:
What if we want to trace our own messages? Simply create the message and “publish” it:
msg.text = “my message”;
Please proceed to Quick Start – Using Remote SubAgents.