NS-3 日志的使用

日志函数:

void LogComponentEnable (char const *name, enum LogLevel level);
void LogComponentEnableAll (enum LogLevel level);
void LogComponentDisable (char const *name, enum LogLevel level);
void LogComponentDisableAll (enum LogLevel level);


NS_LOG_ERROR(msg);//Log error messages;
NS_LOG_WARN(msg);//Log warning messages;
NS_LOG_DEBUG(msg);//Log relatively rare, ad-hoc debugging messages;
NS_LOG_INFO(msg);//Log informational messages about program progress;
NS_LOG_FUNCTION(parameters);//Log a message describing each function called;
NS_LOG_LOGIC(msg);//Log messages describing logical flow within a function;
NS_LOG_ALL//Log everything.
NS_LOG_UNCOND(msg);//无条件输出
NS_LOG_FUNCTION_NOARGS();
........

用法:

  1. int main(int argc,char *argv[])前:
`定义日志名字(不能重复)`
NS_LOG_COMPONENT_DEFINE ("ASpecialName");

NS_LOG_COMPONENT_DEFINE的定义在文件 .../ns-3.26/src/core/model/log.h中

  1. 声明完变量,定义好命令行等一堆需要放在程序开头的东西之后:
`启动日志`
LogComponentEnable ("ASpecialName",LOG_LEVEL_INFO);
  1. 之后就可以在合适的地方使用其他日志函数了:

一些别的

在函数中插入

 LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
 LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

运行代码,输出:

NAME@ubuntu:~/source/ns-3.26$ ./waf --run scratch/second
Waf: Entering directory `/home/NAME/source/ns-3.26/build'
Waf: Leaving directory `/home/NAME/source/ns-3.26/build'
Build commands will be stored in build/compile_commands.json
'build' finished successfully (1.884s)
At time 2s client sent 1024 bytes to 10.1.2.4 port 9
At time 2.0078s server received 1024 bytes from 10.1.1.1 port 49153
At time 2.0078s server sent 1024 bytes to 10.1.1.1 port 49153
At time 2.01761s client received 1024 bytes from 10.1.2.4 port 9

后四行“sent”和“received”部分来自UdpEchoClientApplication和UdpEchoServerApplication的日志消息。


一个示例

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("AnExample");

int main (int argc, char *argv[])
{
  uint32_t AVariate = 3;

  CommandLine cmd;
  cmd.Parse (argc,argv);

  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
  
  .......

  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}

Reference

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容