A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
log-macros-enabled.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006,2007 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
9#ifndef NS3_LOG_MACROS_ENABLED_H
10#define NS3_LOG_MACROS_ENABLED_H
11
12/**
13 * @file
14 * @ingroup logging
15 * NS_LOG and related logging macro definitions.
16 */
17
18#ifdef NS3_LOG_ENABLE
19
20/**
21 * @ingroup logging
22 * Append the simulation time to a log message.
23 * @internal
24 * Logging implementation macro; should not be called directly.
25 * NS_FATAL_ERROR_IMPL_NO_MSG (fatal-error.h) emits the same prefix
26 * directly to std::clog; keep the two formats in sync.
27 */
28#define NS_LOG_APPEND_TIME_PREFIX \
29 if (g_log.IsEnabled(ns3::LOG_PREFIX_TIME)) \
30 { \
31 ns3::TimePrinter printer = ns3::LogGetTimePrinter(); \
32 if (printer != 0) \
33 { \
34 (*printer)(ns3LogStream); \
35 ns3LogStream << " "; \
36 } \
37 }
38
39/**
40 * @ingroup logging
41 * Append the simulation node id to a log message.
42 * @internal
43 * Logging implementation macro; should not be called directly.
44 * NS_FATAL_ERROR_IMPL_NO_MSG (fatal-error.h) emits the same prefix
45 * directly to std::clog; keep the two formats in sync.
46 */
47#define NS_LOG_APPEND_NODE_PREFIX \
48 if (g_log.IsEnabled(ns3::LOG_PREFIX_NODE)) \
49 { \
50 ns3::NodePrinter printer = ns3::LogGetNodePrinter(); \
51 if (printer != 0) \
52 { \
53 (*printer)(ns3LogStream); \
54 ns3LogStream << " "; \
55 } \
56 }
57
58/**
59 * @ingroup logging
60 * Append the function name to a log message.
61 * @internal
62 * Logging implementation macro; should not be called directly.
63 */
64#define NS_LOG_APPEND_FUNC_PREFIX \
65 if (g_log.IsEnabled(ns3::LOG_PREFIX_FUNC)) \
66 { \
67 ns3LogStream << g_log.Name() << ":" << __FUNCTION__ << "(): "; \
68 }
69
70/**
71 * @ingroup logging
72 * Append the log severity level to a log message.
73 * @internal
74 * Logging implementation macro; should not be called directly.
75 */
76#define NS_LOG_APPEND_LEVEL_PREFIX(level) \
77 if (g_log.IsEnabled(ns3::LOG_PREFIX_LEVEL)) \
78 { \
79 ns3LogStream << "[" << g_log.GetLevelLabel(level) << "] "; \
80 }
81
82#ifndef NS_LOG_APPEND_CONTEXT
83/**
84 * @ingroup logging
85 * Append the node id (or other file-local programmatic context, such as
86 * MPI rank) to a log message.
87 *
88 * This is implemented locally in `.cc` files because
89 * the relevant variable is only known there.
90 *
91 * The macro is expanded inside the NS_LOG_* macros where a local
92 * `std::ostream& ns3LogStream` is in scope. Redefinitions must stream the
93 * context into `ns3LogStream` (not directly to `std::clog`) so the whole
94 * log line can be emitted with a single write operation.
95 *
96 * Preferred format is something like (assuming the node id is
97 * accessible from `var`:
98 * @code
99 * if (var)
100 * {
101 * ns3LogStream << "[node " << var->GetObject<Node> ()->GetId () << "] ";
102 * }
103 * @endcode
104 */
105#define NS_LOG_APPEND_CONTEXT
106#endif /* NS_LOG_APPEND_CONTEXT */
107
108#ifndef NS_LOG_CONDITION
109/**
110 * @ingroup logging
111 * Limit logging output based on some file-local condition,
112 * such as MPI rank.
113 *
114 * This is implemented locally in `.cc` files because
115 * the relevant condition variable is only known there.
116 *
117 * Since this appears immediately before the `do { ... } while false`
118 * construct of \c NS_LOG(level, msg), it must have the form
119 * @code
120 * #define NS_LOG_CONDITION if (condition)
121 * @endcode
122 */
123#define NS_LOG_CONDITION
124#endif
125
126/**
127 * @ingroup logging
128 *
129 * This macro allows you to log an arbitrary message at a specific
130 * log level.
131 *
132 * The log message is expected to be a C++ ostream
133 * message such as "my string" << aNumber << "my oth stream".
134 * The message and all prefixes (time, node, context, function, level) are
135 * assembled in a reusable memory buffer (`ns3LogStream`) and emitted to
136 * `std::clog` with a single write operation
137 * (see ns3::LogLineBegin() / ns3::LogLineCommit()).
138 *
139 * Typical usage looks like:
140 * @code
141 * NS_LOG (LOG_DEBUG, "a number="<<aNumber<<", anotherNumber="<<anotherNumber);
142 * @endcode
143 *
144 * @param [in] level The log level
145 * @param [in] msg The message to log
146 * @internal
147 * Logging implementation macro; should not be called directly.
148 */
149#define NS_LOG(level, msg) \
150 NS_LOG_CONDITION \
151 do \
152 { \
153 if (g_log.IsEnabled(level)) \
154 { \
155 std::ostream& ns3LogStream = ns3::LogLineBegin(); \
156 NS_LOG_APPEND_TIME_PREFIX; \
157 NS_LOG_APPEND_NODE_PREFIX; \
158 NS_LOG_APPEND_CONTEXT; \
159 NS_LOG_APPEND_FUNC_PREFIX; \
160 NS_LOG_APPEND_LEVEL_PREFIX(level); \
161 auto flags = ns3LogStream.setf(std::ios_base::boolalpha); \
162 ns3LogStream << msg; \
163 ns3LogStream.flags(flags); \
164 ns3::LogLineCommit(ns3LogStream); \
165 } \
166 } while (false)
167
168/**
169 * @ingroup logging
170 *
171 * Output the name of the function.
172 *
173 * This should be used only in static functions without arguments; most member functions
174 * should instead use NS_LOG_FUNCTION().
175 */
176#define NS_LOG_FUNCTION_NOARGS() \
177 NS_LOG_CONDITION \
178 do \
179 { \
180 if (g_log.IsEnabled(ns3::LOG_FUNCTION)) \
181 { \
182 std::ostream& ns3LogStream = ns3::LogLineBegin(); \
183 NS_LOG_APPEND_TIME_PREFIX; \
184 NS_LOG_APPEND_NODE_PREFIX; \
185 NS_LOG_APPEND_CONTEXT; \
186 ns3LogStream << g_log.Name() << ":" << __FUNCTION__ << "()"; \
187 ns3::LogLineCommit(ns3LogStream); \
188 } \
189 } while (false)
190
191/**
192 * @ingroup logging
193 *
194 * If log level LOG_FUNCTION is enabled, this macro will output
195 * all input parameters separated by ", ".
196 *
197 * Typical usage looks like:
198 * @code
199 * NS_LOG_FUNCTION (aNumber<<anotherNumber);
200 * @endcode
201 * And the output will look like:
202 * @code
203 * Component:Function (aNumber, anotherNumber)
204 * @endcode
205 *
206 * To facilitate function tracing, most functions should begin with
207 * (at least) NS_LOG_FUNCTION(this).
208 *
209 * Static functions should use NS_LOG_FUNCTION(args) when they have arguments,
210 * and NS_LOG_FUNCTION_NOARGS() when they have no arguments.
211 *
212 * @param [in] parameters The parameters to output.
213 */
214#define NS_LOG_FUNCTION(parameters) \
215 NS_LOG_CONDITION \
216 do \
217 { \
218 if (g_log.IsEnabled(ns3::LOG_FUNCTION)) \
219 { \
220 std::ostream& ns3LogStream = ns3::LogLineBegin(); \
221 NS_LOG_APPEND_TIME_PREFIX; \
222 NS_LOG_APPEND_NODE_PREFIX; \
223 NS_LOG_APPEND_CONTEXT; \
224 ns3LogStream << g_log.Name() << ":" << __FUNCTION__ << "("; \
225 auto flags = ns3LogStream.setf(std::ios_base::boolalpha); \
226 ns3::ParameterLogger(ns3LogStream) << parameters; \
227 ns3LogStream.flags(flags); \
228 ns3LogStream << ")"; \
229 ns3::LogLineCommit(ns3LogStream); \
230 } \
231 } while (false)
232
233/**
234 * @ingroup logging
235 *
236 * Output the requested message unconditionally.
237 *
238 * @param [in] msg The message to log
239 */
240#define NS_LOG_UNCOND(msg) \
241 NS_LOG_CONDITION \
242 do \
243 { \
244 std::ostream& ns3LogStream = ns3::LogLineBegin(); \
245 auto flags = ns3LogStream.setf(std::ios_base::boolalpha); \
246 ns3LogStream << msg; \
247 ns3LogStream.flags(flags); \
248 ns3::LogLineCommit(ns3LogStream); \
249 } while (false)
250
251#endif /* NS3_LOG_ENABLE */
252
253#endif /* NS3_LOG_MACROS_ENABLED_H */