A Discrete-Event Network Simulator
API
log.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006,2007 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #ifndef NS3_LOG_H
22 #define NS3_LOG_H
23 
24 #include <string>
25 #include <iostream>
26 #include <stdint.h>
27 #include <map>
28 
29 #include "log-macros-enabled.h"
30 #include "log-macros-disabled.h"
31 
85 namespace ns3 {
86 
90 enum LogLevel {
91  LOG_NONE = 0x00000000,
92 
93  LOG_ERROR = 0x00000001,
94  LOG_LEVEL_ERROR = 0x00000001,
95 
96  LOG_WARN = 0x00000002,
97  LOG_LEVEL_WARN = 0x00000003,
98 
99  LOG_DEBUG = 0x00000004,
100  LOG_LEVEL_DEBUG = 0x00000007,
101 
102  LOG_INFO = 0x00000008,
103  LOG_LEVEL_INFO = 0x0000000f,
104 
105  LOG_FUNCTION = 0x00000010,
106  LOG_LEVEL_FUNCTION = 0x0000001f,
107 
108  LOG_LOGIC = 0x00000020,
109  LOG_LEVEL_LOGIC = 0x0000003f,
110 
111  LOG_ALL = 0x0fffffff,
113 
114  LOG_PREFIX_FUNC = 0x80000000,
115  LOG_PREFIX_TIME = 0x40000000,
116  LOG_PREFIX_NODE = 0x20000000,
117  LOG_PREFIX_LEVEL = 0x10000000,
118  LOG_PREFIX_ALL = 0xf0000000
119 };
120 
133 void LogComponentEnable (char const *name, enum LogLevel level);
134 
143 void LogComponentEnableAll (enum LogLevel level);
144 
145 
155 void LogComponentDisable (char const *name, enum LogLevel level);
156 
162 void LogComponentDisableAll (enum LogLevel level);
163 
164 
165 } // namespace ns3
166 
167 
201 #define NS_LOG_COMPONENT_DEFINE(name) \
202  static ns3::LogComponent g_log = ns3::LogComponent (name, __FILE__)
203 
212 #define NS_LOG_COMPONENT_DEFINE_MASK(name, mask) \
213  static ns3::LogComponent g_log = ns3::LogComponent (name, __FILE__, mask)
214 
224 #define NS_LOG_TEMPLATE_DECLARE LogComponent & g_log
225 
235 #define NS_LOG_TEMPLATE_DEFINE(name) g_log (GetLogComponent (name))
236 
245 #define NS_LOG_STATIC_TEMPLATE_DEFINE(name) \
246  static LogComponent & NS_UNUSED_GLOBAL (g_log) = GetLogComponent (name)
247 
253 #define NS_LOG_ERROR(msg) \
254  NS_LOG (ns3::LOG_ERROR, msg)
255 
261 #define NS_LOG_WARN(msg) \
262  NS_LOG (ns3::LOG_WARN, msg)
263 
269 #define NS_LOG_DEBUG(msg) \
270  NS_LOG (ns3::LOG_DEBUG, msg)
271 
277 #define NS_LOG_INFO(msg) \
278  NS_LOG (ns3::LOG_INFO, msg)
279 
285 #define NS_LOG_LOGIC(msg) \
286  NS_LOG (ns3::LOG_LOGIC, msg)
287 
288 
289 namespace ns3 {
290 
296 void LogComponentPrintList (void);
297 
304 typedef void (*LogTimePrinter)(std::ostream &os);
311 typedef void (*LogNodePrinter)(std::ostream &os);
312 
325 
338 
339 
344 {
345 public:
355  LogComponent (const std::string & name,
356  const std::string & file,
357  const enum LogLevel mask = LOG_NONE);
364  bool IsEnabled (const enum LogLevel level) const;
370  bool IsNoneEnabled (void) const;
376  void Enable (const enum LogLevel level);
382  void Disable (const enum LogLevel level);
388  char const *Name (void) const;
393  std::string File (void) const;
400  static std::string GetLevelLabel(const enum LogLevel level);
406  void SetMask (const enum LogLevel level);
407 
416  typedef std::map<std::string, LogComponent *> ComponentList;
417 
428  static ComponentList *GetComponentList (void);
429 
430 
431 private:
436  void EnvVarCheck (void);
437 
438  int32_t m_levels;
439  int32_t m_mask;
440  std::string m_name;
441  std::string m_file;
442 
443 }; // class LogComponent
444 
451 LogComponent & GetLogComponent (const std::string name);
452 
457 {
458  bool m_first;
459  std::ostream &m_os;
460 public:
466  ParameterLogger (std::ostream &os);
467 
475  template<typename T>
476  ParameterLogger& operator<< (T param);
477 
478 };
479 
480 template<typename T>
483 {
484  if (m_first)
485  {
486  m_os << param;
487  m_first = false;
488  }
489  else
490  {
491  m_os << ", " << param;
492  }
493  return *this;
494 }
495 
501 template<>
503 ParameterLogger::operator<< <std::string>(const std::string param);
504 
510 template<>
512 ParameterLogger::operator<< <const char *>(const char * param);
513 
514 } // namespace ns3
515  // \ingroup logging
517 
518 #endif /* NS3_LOG_H */
LOG_LOGIC and above.
Definition: log.h:109
LogComponent(const std::string &name, const std::string &file, const enum LogLevel mask=LOG_NONE)
Constructor.
Definition: log.cc:114
ParameterLogger & operator<<(T param)
Write a function parameter on the output stream, separating parameters after the first by ...
Definition: log.h:482
void LogComponentDisable(char const *name, enum LogLevel level)
Disable the logging output associated with that log component.
Definition: log.cc:405
std::ostream & m_os
Underlying output stream.
Definition: log.h:459
No logging.
Definition: log.h:91
bool m_first
First argument flag, doesn't get ,.
Definition: log.h:458
A single log component configuration.
Definition: log.h:343
ParameterLogger(std::ostream &os)
Constructor.
Definition: log.cc:653
LOG_ERROR and above.
Definition: log.h:94
void Disable(const enum LogLevel level)
Disable logging at level for this LogComponent.
Definition: log.cc:315
void(* LogTimePrinter)(std::ostream &os)
Function signature for prepending the simulation time to a log message.
Definition: log.h:304
LOG_INFO and above.
Definition: log.h:103
void EnvVarCheck(void)
Parse the NS_LOG environment variable for options relating to this LogComponent.
Definition: log.cc:152
void(* LogNodePrinter)(std::ostream &os)
Function signature for prepending the node id to a log message.
Definition: log.h:311
LOG_FUNCTION and above.
Definition: log.h:106
std::string m_file
File defining this LogComponent.
Definition: log.h:441
Rare ad-hoc debug messages.
Definition: log.h:99
Control flow tracing within functions.
Definition: log.h:108
Insert , when streaming function arguments.
Definition: log.h:456
Serious error messages only.
Definition: log.h:93
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
void Enable(const enum LogLevel level)
Enable this LogComponent at level.
Definition: log.cc:309
bool IsEnabled(const enum LogLevel level) const
Check if this LogComponent is enabled for level.
Definition: log.cc:290
void LogSetNodePrinter(LogNodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
Definition: log.cc:643
void SetMask(const enum LogLevel level)
Prevent the enabling of a specific LogLevel.
Definition: log.cc:303
void LogComponentPrintList(void)
Print the list of logging messages available.
Definition: log.cc:433
LogNodePrinter LogGetNodePrinter(void)
Get the LogNodePrinter function currently in use.
Definition: log.cc:647
Warning messages.
Definition: log.h:96
LOG_WARN and above.
Definition: log.h:97
Print everything.
Definition: log.h:111
Definition of empty logging macros and the NS_LOG_NOOP_INTERNAL macro.
std::map< std::string, LogComponent * > ComponentList
LogComponent name map.
Definition: log.h:416
LOG_DEBUG and above.
Definition: log.h:100
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:393
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Prefix all trace prints with simulation time.
Definition: log.h:115
void LogComponentDisableAll(enum LogLevel level)
Disable all logging for all components.
Definition: log.cc:421
Prefix all trace prints with function.
Definition: log.h:114
static ComponentList * GetComponentList(void)
Get the list of LogComponnents.
Definition: log.cc:80
Function tracing.
Definition: log.h:105
NS_LOG and related logging macro definitions.
int32_t m_mask
Blocked LogLevels.
Definition: log.h:439
int32_t m_levels
Enabled LogLevels.
Definition: log.h:438
LogComponent & GetLogComponent(const std::string name)
Get the LogComponent registered with the given name.
Definition: log.cc:135
void LogSetTimePrinter(LogTimePrinter printer)
Set the LogTimePrinter function to be used to prepend log messages with the simulation time...
Definition: log.cc:629
std::string File(void) const
Get the compilation unit defining this LogComponent.
Definition: log.cc:327
Prefix all trace prints with simulation node.
Definition: log.h:116
All prefixes.
Definition: log.h:118
bool IsNoneEnabled(void) const
Check if all levels are disabled.
Definition: log.cc:297
LogLevel
Logging severity classes and levels.
Definition: log.h:90
Print everything.
Definition: log.h:112
Informational messages (e.g., banners).
Definition: log.h:102
char const * Name(void) const
Get the name of this LogComponent.
Definition: log.cc:321
Prefix all trace prints with log level (severity).
Definition: log.h:117
static std::string GetLevelLabel(const enum LogLevel level)
Get the string label for the given LogLevel.
Definition: log.cc:334
LogTimePrinter LogGetTimePrinter(void)
Get the LogTimePrinter function currently in use.
Definition: log.cc:638
std::string m_name
LogComponent name.
Definition: log.h:440