A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
log-line-test-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Gabriel Ferreira <gabrielcarvfer@gmail.com>
7 */
8
9#include "ns3/command-line.h"
10#include "ns3/core-module.h"
11#include "ns3/nstime.h"
12#include "ns3/simulator.h"
13
14#include <cstdint>
15#include <cstdlib>
16#include <ostream>
17#include <stdexcept>
18#include <string>
19
20/**
21 * @file
22 * @defgroup log-line-test-example Core example: assembling and interrupting log lines
23 * @ingroup core-examples
24 * @ingroup logging
25 *
26 * Example program exercising how a log line is assembled and emitted:
27 * the time, node, function and context prefixes, the parameters logged by
28 * NS_LOG_FUNCTION, a user-defined `operator<<` which itself logs (nested
29 * logging), and what is emitted when the program dies while a log line is
30 * still being assembled.
31 *
32 * Log lines are assembled in a memory buffer and emitted with a single write
33 * (see ns3::LogLineBegin), so a line interrupted before it is committed would
34 * be lost; ns3::FatalImpl::FlushStreams() emits the truncated line instead.
35 *
36 * Run without arguments to see the uninterrupted output:
37 *
38 * $ ./ns3 run log-line-test-example
39 *
40 * The lines are logged from an event scheduled at `--time` seconds, with the
41 * Time resolution set to seconds, so the time prefix can be exercised up to
42 * the largest time which converts to seconds (about 9.2e18); `--context`
43 * sets the node context of that event, shown by the node prefix, and
44 * `--nested` logs from within the parameter's `operator<<`:
45 *
46 * $ ./ns3 run "log-line-test-example --time=9e18 --context=7 --nested"
47 *
48 * Run with `--mode` to interrupt the second log line:
49 *
50 * $ ./ns3 run "log-line-test-example --mode=assert"
51 * $ ./ns3 run "log-line-test-example --mode=fatal --nested"
52 *
53 * `--mode=throw` reaches std::terminate() without going through the fatal
54 * error macros, and `--mode=abort` calls `std::abort()` directly; the
55 * truncated line is recovered by the terminate and crash signal handlers
56 * installed by ns3::FatalImpl, as it is on a segmentation fault.
57 */
58
59using namespace ns3;
60
61NS_LOG_COMPONENT_DEFINE("LogLineTestExample");
62
63/// How to interrupt the log line: `none`, `assert`, `fatal`, `throw` or `abort`.
64std::string g_mode{"none"};
65
66/// The label appended to every line by NS_LOG_APPEND_CONTEXT, when not empty.
67std::string g_label;
68
69/** Append a label to each log line, as models do to identify the logging node. */
70#undef NS_LOG_APPEND_CONTEXT
71#define NS_LOG_APPEND_CONTEXT \
72 if (!g_label.empty()) \
73 { \
74 ns3LogStream << "[" << g_label << "] "; \
75 }
76
77/**
78 * @ingroup log-line-test-example
79 * A parameter whose `operator<<` optionally logs, then interrupts the program.
80 */
82{
83 bool nested; ///< Whether to log from within operator<<
84 bool interrupt; ///< Whether to interrupt the program according to the mode
85};
86
87/**
88 * @ingroup log-line-test-example
89 * Stream a Parameter, logging and dying in the middle of the enclosing log line.
90 *
91 * @param [in,out] os The output stream.
92 * @param [in] param The parameter to stream.
93 * @return The stream.
94 */
95std::ostream&
96operator<<(std::ostream& os, const Parameter& param)
97{
98 os << "PARAM-BEGIN";
99 if (param.nested)
100 {
101 NS_LOG_INFO("nested line logged from operator<<");
102 }
103 os << "PARAM-END";
104
105 if (!param.interrupt)
106 {
107 os << " PARAM-TAIL";
108 return os;
109 }
110
111 if (g_mode == "assert")
112 {
113 NS_ASSERT_MSG(false, "assertion in the middle of a log line");
114 }
115 else if (g_mode == "fatal")
116 {
117 NS_FATAL_ERROR("fatal error in the middle of a log line");
118 }
119 else if (g_mode == "throw")
120 {
121 throw std::runtime_error("uncaught exception in the middle of a log line");
122 }
123 else if (g_mode == "abort")
124 {
125 std::abort();
126 }
127
128 os << " PARAM-TAIL";
129 return os;
130}
131
132/**
133 * @ingroup log-line-test-example
134 * Log three lines, the second one interrupted according to the mode.
135 *
136 * @param [in] nested Whether the parameter logs from within its operator<<.
137 * @param [in] count A parameter logged by NS_LOG_FUNCTION.
138 */
139void
140LogLines(bool nested, uint32_t count)
141{
142 NS_LOG_FUNCTION(nested << count << (Parameter{false, false}));
143 NS_LOG_INFO("line 1: logged before the interruption");
144 NS_LOG_INFO("line 2: outer line with " << (Parameter{nested, true}) << ", end of outer line");
145 NS_LOG_INFO("line 3: logged after the interruption");
146}
147
148int
149main(int argc, char** argv)
150{
151 bool nested = false;
152 double time = 0;
154
155 CommandLine cmd(__FILE__);
156 cmd.AddValue("mode",
157 "How to interrupt the log line: none, assert, fatal, throw or abort",
158 g_mode);
159 cmd.AddValue("nested", "Log from within the parameter's operator<<", nested);
160 cmd.AddValue("time", "Simulation time, in seconds, at which the lines are logged", time);
161 cmd.AddValue("context", "Node context of the event logging the lines", context);
162 cmd.AddValue("label", "Label appended to every line by NS_LOG_APPEND_CONTEXT", g_label);
163 cmd.Parse(argc, argv);
164
166
168 Simulator::ScheduleWithContext(context, Seconds(time), &LogLines, nested, 3);
171
172 return 0;
173}
Parse command-line arguments.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:597
static void Run()
Run the simulation.
Definition simulator.cc:161
@ NO_CONTEXT
Flag for events not associated with any particular context.
Definition simulator.h:207
@ S
second
Definition nstime.h:106
static void SetResolution(Unit resolution)
Definition time.cc:214
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:76
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
void LogLines(bool nested, uint32_t count)
Log three lines, the second one interrupted according to the mode.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:274
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1313
std::string g_mode
How to interrupt the log line: none, assert, fatal, throw or abort.
std::string g_label
The label appended to every line by NS_LOG_APPEND_CONTEXT, when not empty.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:491
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
LogLevel
Logging severity classes and levels.
Definition log.h:93
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:115
@ LOG_PREFIX_ALL
All prefixes.
Definition log.h:121
A parameter whose operator<< optionally logs, then interrupts the program.
bool nested
Whether to log from within operator<<.
bool interrupt
Whether to interrupt the program according to the mode.