A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
Core example: assembling and interrupting log lines

Example program exercising how a log line is assembled and emitted: the time, node, function and context prefixes, the parameters logged by NS_LOG_FUNCTION, a user-defined operator<< which itself logs (nested logging), and what is emitted when the program dies while a log line is still being assembled. More...

Collaboration diagram for Core example: assembling and interrupting log lines:

Classes

struct  Parameter
 A parameter whose operator<< optionally logs, then interrupts the program. More...

Functions

void LogLines (bool nested, uint32_t count)
 Log three lines, the second one interrupted according to the mode.
std::ostream & operator<< (std::ostream &os, const Parameter &param)
 Stream a Parameter, logging and dying in the middle of the enclosing log line.

Detailed Description

Example program exercising how a log line is assembled and emitted: the time, node, function and context prefixes, the parameters logged by NS_LOG_FUNCTION, a user-defined operator<< which itself logs (nested logging), and what is emitted when the program dies while a log line is still being assembled.

Log lines are assembled in a memory buffer and emitted with a single write (see ns3::LogLineBegin), so a line interrupted before it is committed would be lost; ns3::FatalImpl::FlushStreams() emits the truncated line instead.

Run without arguments to see the uninterrupted output:

$ ./ns3 run log-line-test-example

The lines are logged from an event scheduled at --Virtual Time seconds, with the Time resolution set to seconds, so the time prefix can be exercised up to the largest time which converts to seconds (about 9.2e18); --context sets the node context of that event, shown by the node prefix, and --nested logs from within the parameter's operator<<:

$ ./ns3 run "log-line-test-example --time=9e18 --context=7 --nested"

Run with --mode to interrupt the second log line:

$ ./ns3 run "log-line-test-example --mode=assert"
$ ./ns3 run "log-line-test-example --mode=fatal --nested"

--mode=throw reaches std::terminate() without going through the fatal error macros, and --mode=abort calls std::abort() directly; the truncated line is recovered by the terminate and crash signal handlers installed by ns3::FatalImpl, as it is on a segmentation fault.

Function Documentation

◆ LogLines()

void LogLines ( bool nested,
uint32_t count )

Log three lines, the second one interrupted according to the mode.

Parameters
[in]nestedWhether the parameter logs from within its operator<<.
[in]countA parameter logged by NS_LOG_FUNCTION.

Definition at line 140 of file log-line-test-example.cc.

References NS_LOG_FUNCTION, and NS_LOG_INFO.

◆ operator<<()

std::ostream & operator<< ( std::ostream & os,
const Parameter & param )

Stream a Parameter, logging and dying in the middle of the enclosing log line.

Parameters
[in,out]osThe output stream.
[in]paramThe parameter to stream.
Returns
The stream.

Definition at line 95 of file log-line-test-example.cc.

References g_mode, Parameter::interrupt, Parameter::nested, NS_ASSERT_MSG, NS_FATAL_ERROR, and NS_LOG_INFO.