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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
18 */
19
20/**
21 * \file
22 * \ingroup core-examples
23 * \ingroup logging
24 * Example program illustrating the various logging functions.
25 */
26
27/** File-local context string */
28#define NS_LOG_APPEND_CONTEXT \
29 { \
30 std::clog << "(local context) "; \
31 }
32
33#include "ns3/core-module.h"
34#include "ns3/network-module.h"
35
36using namespace ns3;
37
38NS_LOG_COMPONENT_DEFINE("LogExample");
39
40/**
41 * Unnamed namespace for log-example.cc
42 */
43namespace
44{
45
46/** A free function with logging. */
47void
49{
51
52 NS_LOG_ERROR("FreeEvent: error msg");
53 NS_LOG_WARN("FreeEvent: warning msg");
54 NS_LOG_INFO("FreeEvent: info msg");
55 NS_LOG_LOGIC("FreeEvent: logic msg");
56 NS_LOG_DEBUG("FreeEvent: debug msg");
57}
58
59/** Simple object to aggregate to a node.
60 * This helps demonstrate the logging node prefix.
61 */
62
63class MyEventObject : public Object
64{
65 public:
66 /**
67 * \brief Get the type ID.
68 * \return the object TypeId
69 */
71 {
72 static TypeId tid =
73 TypeId("MyEventObject").SetParent<Object>().AddConstructor<MyEventObject>();
74 return tid;
75 }
76
77 /** Constructor. */
79 {
80 NS_LOG_FUNCTION(this);
81 }
82
83 /** Destructor. */
84 ~MyEventObject() override
85 {
86 NS_LOG_FUNCTION(this);
87 }
88
89 /** Class member function with logging. */
90 void Event()
91 {
92 NS_LOG_FUNCTION(this);
93
94 NS_LOG_ERROR("MyEventObject:Event: error msg");
95 NS_LOG_WARN("MyEventObject:Event: warning msg");
96 NS_LOG_INFO("MyEventObject:Event: info msg");
97 NS_LOG_LOGIC("MyEventObject:Event: logic msg");
98 NS_LOG_DEBUG("MyEventObject:Event: debug msg");
99 }
100
101}; // MyEventObject
102
103NS_OBJECT_ENSURE_REGISTERED(MyEventObject);
104
105} // Unnamed namespace
106
107int
108main(int argc, char** argv)
109{
111 cmd.Parse(argc, argv);
112
113 NS_LOG_DEBUG("Creating a Node");
114 auto node = CreateObject<Node>();
115
116 NS_LOG_DEBUG("Creating MyEventObject");
117 auto myObj = CreateObject<MyEventObject>();
118
119 NS_LOG_DEBUG("Aggregating MyEventObject to Node");
120 node->AggregateObject(myObj);
121
122 NS_LOG_INFO("Scheduling the MyEventObject::Event with node context");
123 Simulator::ScheduleWithContext(node->GetId(), Seconds(3), &MyEventObject::Event, &(*myObj));
124
125 NS_LOG_INFO("Scheduling FreeEvent");
127
128 NS_LOG_DEBUG("Starting run...");
130 NS_LOG_DEBUG("... run complete");
132 NS_LOG_DEBUG("Goodbye");
133
134 return 0;
135}
Simple object to aggregate to a node.
Definition: log-example.cc:64
void Event()
Class member function with logging.
Definition: log-example.cc:90
static TypeId GetTypeId()
Get the type ID.
Definition: log-example.cc:70
Parse command-line arguments.
Definition: command-line.h:232
A base class which provides memory management and object aggregation.
Definition: object.h:89
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static void Run()
Run the simulation.
Definition: simulator.cc:178
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#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:261
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
void FreeEvent()
A free function with logging.
Definition: log-example.cc:48
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns cmd
Definition: second.py:40