A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
log-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Lawrence Livermore National Laboratory
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
7 */
8
9/**
10 * \file
11 * \ingroup core-examples
12 * \ingroup logging
13 * Example program illustrating the various logging functions.
14 */
15
16/** File-local context string */
17#define NS_LOG_APPEND_CONTEXT \
18 { \
19 std::clog << "(local context) "; \
20 }
21
22#include "ns3/core-module.h"
23#include "ns3/network-module.h"
24
25using namespace ns3;
26
27NS_LOG_COMPONENT_DEFINE("LogExample");
28
29/**
30 * Unnamed namespace for log-example.cc
31 */
32namespace
33{
34
35/** A free function with logging. */
36void
38{
40
41 NS_LOG_ERROR("FreeEvent: error msg");
42 NS_LOG_WARN("FreeEvent: warning msg");
43 NS_LOG_INFO("FreeEvent: info msg");
44 NS_LOG_LOGIC("FreeEvent: logic msg");
45 NS_LOG_DEBUG("FreeEvent: debug msg");
46}
47
48/** Simple object to aggregate to a node.
49 * This helps demonstrate the logging node prefix.
50 */
51
52class MyEventObject : public Object
53{
54 public:
55 /**
56 * \brief Get the type ID.
57 * \return the object TypeId
58 */
60 {
61 static TypeId tid =
62 TypeId("MyEventObject").SetParent<Object>().AddConstructor<MyEventObject>();
63 return tid;
64 }
65
66 /** Constructor. */
68 {
69 NS_LOG_FUNCTION(this);
70 }
71
72 /** Destructor. */
73 ~MyEventObject() override
74 {
75 NS_LOG_FUNCTION(this);
76 }
77
78 /** Class member function with logging. */
79 void Event()
80 {
81 NS_LOG_FUNCTION(this);
82
83 NS_LOG_ERROR("MyEventObject:Event: error msg");
84 NS_LOG_WARN("MyEventObject:Event: warning msg");
85 NS_LOG_INFO("MyEventObject:Event: info msg");
86 NS_LOG_LOGIC("MyEventObject:Event: logic msg");
87 NS_LOG_DEBUG("MyEventObject:Event: debug msg");
88 }
89
90}; // MyEventObject
91
93
94} // Unnamed namespace
95
96int
97main(int argc, char** argv)
98{
100 cmd.Parse(argc, argv);
101
102 NS_LOG_DEBUG("Creating a Node");
103 auto node = CreateObject<Node>();
104
105 NS_LOG_DEBUG("Creating MyEventObject");
106 auto myObj = CreateObject<MyEventObject>();
107
108 NS_LOG_DEBUG("Aggregating MyEventObject to Node");
109 node->AggregateObject(myObj);
110
111 NS_LOG_INFO("Scheduling the MyEventObject::Event with node context");
112 Simulator::ScheduleWithContext(node->GetId(), Seconds(3), &MyEventObject::Event, &(*myObj));
113
114 NS_LOG_INFO("Scheduling FreeEvent");
116
117 NS_LOG_DEBUG("Starting run...");
119 NS_LOG_DEBUG("... run complete");
121 NS_LOG_DEBUG("Goodbye");
122
123 return 0;
124}
Simple object to aggregate to a node.
void Event()
Class member function with logging.
static TypeId GetTypeId()
Get the type ID.
Parse command-line arguments.
A base class which provides memory management and object aggregation.
Definition object.h:78
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static void Run()
Run the simulation.
Definition simulator.cc:167
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:243
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:250
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
void FreeEvent()
A free function with logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.