logging 是一种日志,ns-3 系统中将之分为7个等级,以便显示不会复杂纷乱。等级分别如下:
- LOG_ERROR
- LOG_WARN
- LOG_DEBUG
- LOG_INFO
- LOG_FUNCTION
- LOG_LOGIC
- LOG_ALL
LOG_TYPE 只会显示这种 TYPE 的信息
LOG_LEVEL_TYPE 显示该 TYPE 及更低级别的信息
用example里的 first.cc 作为例子,里面
LogComponentEnable("UdpEchoClientApplication",LOG_LEVEL_INFO);
会显示 LOG_LEVEL_INFO 及更低级别的信息。若想显示所有信息,用如下语句。改变当前环境变量。再执行
$ export NS_LOG=UdpEchoClientApplication=level_all
$ export 'NS_LOG=UdpEchoClientApplication=level_all|prefix_func'
// to show the function from which the result comes
可以显示出每个封包怎么产生(从哪个函数产生)。
$ export 'NS_LOG=UdpEchoClientApplication=level_all|prefix_func:UdpEchoServerApplication=level_all|prefix_func'
注意“:”前后都不能有空格,显示结果如下
UdpEchoServerApplication:UdpEchoServer(0x9349228)
UdpEchoClientApplication:UdpEchoClient(0x93495a0)
UdpEchoClientApplication:SetDataSize(0x93495a0, 1024)
UdpEchoServerApplication:StartApplication(0x9349228)
UdpEchoClientApplication:StartApplication(0x93495a0)
UdpEchoClientApplication:ScheduleTransmit(0x93495a0, +0.0ns)
UdpEchoClientApplication:Send(0x93495a0)
UdpEchoClientApplication:Send(): At time 2s client sent 1024 bytes to 10.1.1.2 port 8
UdpEchoServerApplication:HandleRead(0x9349228, 0x9349648)
UdpEchoServerApplication:HandleRead(): At time 2.25732s server received 1024 bytes from 10.1.1.1 port 49153
UdpEchoServerApplication:HandleRead(): Echoing packet
UdpEchoServerApplication:HandleRead(): At time 2.25732s server sent 1024 bytes to 10.1.1.1 port 49153
UdpEchoClientApplication:HandleRead(0x93495a0, 0x93497e8)
UdpEchoClientApplication:HandleRead(): At time 2.51465s client received 1024 bytes from 10.1.1.2 port 8
UdpEchoClientApplication:StopApplication(0x93495a0)
UdpEchoServerApplication:StopApplication(0x9349228)
UdpEchoClientApplication:DoDispose(0x93495a0)
UdpEchoServerApplication:DoDispose(0x9349228)
UdpEchoClientApplication:~UdpEchoClient(0x93495a0)
UdpEchoServerApplication:~UdpEchoServer(0x9349228)
若要看运行时间,将prefix_func 改成 prefix_time。
0s UdpEchoServerApplication:UdpEchoServer(0x89f8228)
0s UdpEchoClientApplication:UdpEchoClient(0x89f85a0)
0s UdpEchoClientApplication:SetDataSize(0x89f85a0, 1024)
1s UdpEchoServerApplication:StartApplication(0x89f8228)
2s UdpEchoClientApplication:StartApplication(0x89f85a0)
2s UdpEchoClientApplication:ScheduleTransmit(0x89f85a0, +0.0ns)
2s UdpEchoClientApplication:Send(0x89f85a0)
2s At time 2s client sent 1024 bytes to 10.1.1.2 port 8
2.25732s UdpEchoServerApplication:HandleRead(0x89f8228, 0x89f8648)
2.25732s At time 2.25732s server received 1024 bytes from 10.1.1.1 port 49153
2.25732s Echoing packet
2.25732s At time 2.25732s server sent 1024 bytes to 10.1.1.1 port 49153
2.51465s UdpEchoClientApplication:HandleRead(0x89f85a0, 0x89f87e8)
2.51465s At time 2.51465s client received 1024 bytes from 10.1.1.2 port 8
10s UdpEchoClientApplication:StopApplication(0x89f85a0)
10s UdpEchoServerApplication:StopApplication(0x89f8228)
UdpEchoClientApplication:DoDispose(0x89f85a0)
UdpEchoServerApplication:DoDispose(0x89f8228)
UdpEchoClientApplication:~UdpEchoClient(0x89f85a0)
UdpEchoServerApplication:~UdpEchoServer(0x89f8228)
将显示结果输入文件
$ ./waf --run scratch/myfirst > log.out 2>&1
在 myfirst.cc 中
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample")
NS_LOG_INFO("Creating Topology");
更改环境变量,结果显示 Creating Topology 字符串。
$ export NS_LOG=FirstScriptExample=info
代码如下
int
main(int argc, char *argv[]){
CommandLine cmd;
cmd.Parse(argc,argv);
}
代码
int
main(int argc, char *argv[]){
CommandLine cmd;
cmd.AddValue("nPackets","Number of packets to echo", nPackets);
cmd.Parse(argc,argv);
}
执行调用
$ ./waf --run "scratch/myfirst --PrintHelp"
$ ./waf --run "scratch/myfirst --nPackets=2"