src/core/log.h
changeset 2978 c93e1d0d6916
parent 2976 ee338f8cef49
child 2979 3d39dd617b8d
equal deleted inserted replaced
2977:35618565c0e5 2978:c93e1d0d6916
    21 #ifndef __LOG_H__
    21 #ifndef __LOG_H__
    22 #define __LOG_H__
    22 #define __LOG_H__
    23 
    23 
    24 #include <string>
    24 #include <string>
    25 #include <iostream>
    25 #include <iostream>
       
    26 
       
    27 #ifdef NS3_LOG_ENABLE
       
    28 
    26 
    29 
    27 /**
    30 /**
    28  * \ingroup core
    31  * \ingroup core
    29  * \defgroup logging Logging
    32  * \defgroup logging Logging
    30  * \brief Logging functions and macros
    33  * \brief Logging functions and macros
    63  * "log component" which can be later selectively enabled
    66  * "log component" which can be later selectively enabled
    64  * or disabled with the ns3::LogComponentEnable and 
    67  * or disabled with the ns3::LogComponentEnable and 
    65  * ns3::LogComponentDisable functions or with the NS_LOG
    68  * ns3::LogComponentDisable functions or with the NS_LOG
    66  * environment variable.
    69  * environment variable.
    67  */
    70  */
    68 
       
    69 #ifdef NS3_LOG_ENABLE
       
    70 
       
    71 #define NS_LOG_COMPONENT_DEFINE(name)                                \
    71 #define NS_LOG_COMPONENT_DEFINE(name)                                \
    72   static ns3::LogComponent g_log = ns3::LogComponent (name)
    72   static ns3::LogComponent g_log = ns3::LogComponent (name)
    73 
    73 
    74 #else
    74 /**
    75 
    75  * \ingroup logging
    76 #define NS_LOG_COMPONENT_DEFINE(name)
    76  * \param level the log level
    77 
    77  * \param msg the message to log
    78 #endif
    78  *
    79 
    79  * This macro allows you to log an arbitrary message at a specific
    80 
    80  * log level. The log message is expected to be a C++ ostream
    81 /**
    81  * message such as "my string" << aNumber << "my oth stream".
    82  * \ingroup logging
    82  *
    83  * \param msg message to output
    83  * Typical usage looks like:
    84  *
    84  * \code
    85  * Generate logging output in the "log component" of the 
    85  * NS_LOG (LOG_DEBUG, "a number="<<aNumber<<", anotherNumber="<<anotherNumber);
    86  * current file. i.e., every call to NS_LOG from within
    86  * \endcode
    87  * a file implicitely generates out within the component
    87  */
    88  * defined with the NS_LOG_COMPONENT_DEFINE macro in the
    88 #define NS_LOG(level, msg)                                      \
    89  * same file.
    89   do                                                            \
    90  */
    90     {                                                           \
    91 
    91       if (g_log.IsEnabled (level))                              \
    92 #ifdef NS3_LOG_ENABLE
    92         {                                                       \
    93 
    93           if (g_log.IsEnabled (ns3::LOG_PREFIX_ALL))            \
       
    94             {                                                   \
       
    95               std::clog << g_log.Name () << ":" <<              \
       
    96                 __FUNCTION__ << "(): ";                         \
       
    97             }                                                   \
       
    98           std::clog << msg << std::endl;                        \
       
    99         }                                                       \
       
   100     }                                                           \
       
   101   while (false)
       
   102 
       
   103 #define NS_LOG_F(level)                                         \
       
   104   do                                                            \
       
   105     {                                                           \
       
   106       if (g_log.IsEnabled (level))                              \
       
   107         {                                                       \
       
   108           std::clog << g_log.Name () << ":" << __FUNCTION__ <<  \
       
   109             "()" << std::endl;                                \
       
   110         }                                                       \
       
   111     }                                                           \
       
   112   while (false)
       
   113 
       
   114 #define NS_LOG_ERROR(msg) \
       
   115   NS_LOG(ns3::LOG_ERROR, msg)
       
   116 
       
   117 #define NS_LOG_WARN(msg) \
       
   118   NS_LOG(ns3::LOG_WARN, msg)
       
   119 
       
   120 #define NS_LOG_DEBUG(msg) \
       
   121   NS_LOG(ns3::LOG_DEBUG, msg)
       
   122 
       
   123 #define NS_LOG_INFO(msg) \
       
   124   NS_LOG(ns3::LOG_INFO, msg)
       
   125 
       
   126 #define NS_LOG_FUNCTION \
       
   127   NS_LOG_F(ns3::LOG_FUNCTION)
       
   128 
       
   129 
       
   130 
       
   131 #define NS_LOG_PARAMS(parameters)                       \
       
   132   do                                                    \
       
   133     {                                                   \
       
   134       if (g_log.IsEnabled (ns3::LOG_PARAM))             \
       
   135         {                                               \
       
   136           std::clog << g_log.Name () << ":"             \
       
   137                     << __FUNCTION__ << "(";             \
       
   138           g_parameterLogger << parameters;              \
       
   139           std::clog << ")" << std::endl;                \
       
   140         }                                               \
       
   141     }                                                   \
       
   142   while (false)
       
   143 
       
   144 
       
   145 #define NS_LOG_LOGIC(msg) \
       
   146   NS_LOG(ns3::LOG_LOGIC, msg)
       
   147 
       
   148 #define NS_LOG_UNCOND(msg)              \
       
   149   do                                    \
       
   150     {                                   \
       
   151       std::clog << msg << std::endl;    \
       
   152     }                                   \
       
   153   while (false)
    94 
   154 
    95 namespace ns3 {
   155 namespace ns3 {
       
   156 
       
   157 enum LogLevel {
       
   158   LOG_NONE           = 0x00000000, // no logging
       
   159 
       
   160   LOG_ERROR          = 0x00000001, // serious error messages only
       
   161   LOG_LEVEL_ERROR    = 0x00000001,
       
   162 
       
   163   LOG_WARN           = 0x00000002, // warning messages
       
   164   LOG_LEVEL_WARN     = 0x00000003,
       
   165 
       
   166   LOG_DEBUG          = 0x00000004, // rare ad-hoc debug messages
       
   167   LOG_LEVEL_DEBUG    = 0x00000007,
       
   168 
       
   169   LOG_INFO           = 0x00000008, // informational messages (e.g., banners)
       
   170   LOG_LEVEL_INFO     = 0x0000000f,
       
   171 
       
   172   LOG_FUNCTION       = 0x00000010, // function tracing
       
   173   LOG_LEVEL_FUNCTION = 0x0000001f, 
       
   174 
       
   175   LOG_PARAM          = 0x00000020, // parameters to functions
       
   176   LOG_LEVEL_PARAM    = 0x0000003f,
       
   177 
       
   178   LOG_LOGIC          = 0x00000040, // control flow tracing within functions
       
   179   LOG_LEVEL_LOGIC    = 0x0000007f,
       
   180 
       
   181   LOG_ALL            = 0x7fffffff, // print everything
       
   182   LOG_LEVEL_ALL      = LOG_ALL,
       
   183 
       
   184   LOG_PREFIX_ALL     = 0x80000000  // prefix all trace prints with function
       
   185 };
       
   186 
       
   187 /**
       
   188  * \param name a log component name
       
   189  * \param level a logging level
       
   190  * \ingroup logging
       
   191  *
       
   192  * Enable the logging output associated with that log component.
       
   193  * The logging output can be later disabled with a call
       
   194  * to ns3::LogComponentDisable.
       
   195  */
       
   196 void LogComponentEnable (char const *name, enum LogLevel level);
       
   197 
       
   198 /**
       
   199  * \param level a logging level
       
   200  * \ingroup logging
       
   201  *
       
   202  * Enable the logging output for all registered log components.
       
   203  */
       
   204 void LogComponentEnableAll (enum LogLevel level);
       
   205 
       
   206 
       
   207 /**
       
   208  * \param name a log component name
       
   209  * \param level a logging level
       
   210  * \ingroup logging
       
   211  *
       
   212  * Disable the logging output associated with that log component.
       
   213  * The logging output can be later re-enabled with a call
       
   214  * to ns3::LogComponentEnable.
       
   215  */
       
   216 void LogComponentDisable (char const *name, enum LogLevel level);
       
   217 
       
   218 /**
       
   219  * \param level a logging level
       
   220  * \ingroup logging
       
   221  *
       
   222  * Disable all logging for all components.
       
   223  */
       
   224 void LogComponentDisableAll (enum LogLevel level);
       
   225 
       
   226 
       
   227 /**
       
   228  * \ingroup logging
       
   229  *
       
   230  * Print the list of logging messages available.
       
   231  */
       
   232 void LogComponentPrintList (void);
       
   233 
       
   234 class LogComponent {
       
   235 public:
       
   236   LogComponent (char const *name);
       
   237   void EnvVarCheck (char const *name);
       
   238   bool IsEnabled (enum LogLevel level) const;
       
   239   bool IsNoneEnabled (void) const;
       
   240   void Enable (enum LogLevel level);
       
   241   void Disable (enum LogLevel level);
       
   242   bool Decorate (void) const;
       
   243   char const *Name (void) const;
       
   244 private:
       
   245   int32_t     m_levels;
       
   246   char const *m_name;
       
   247   bool        m_decorate;
       
   248 };
    96 
   249 
    97 class ParameterLogger : public std::ostream
   250 class ParameterLogger : public std::ostream
    98 {
   251 {
    99   int m_itemNumber;
   252   int m_itemNumber;
   100 public:
   253 public:
   117   }
   270   }
   118 };
   271 };
   119 
   272 
   120 extern ParameterLogger g_parameterLogger;
   273 extern ParameterLogger g_parameterLogger;
   121 
   274 
   122 }
   275 
   123 
   276 } // namespace ns3
   124 
   277 
   125 
   278 #else /* LOG_ENABLE */
   126 /**
   279 
   127  * \param level the log level
   280 #define NS_LOG_COMPONENT_DEFINE(component)
   128  * \param msg the message to log
       
   129  *
       
   130  * This macro allows you to log an arbitrary message at a specific
       
   131  * log level. The log message is expected to be a C++ ostream
       
   132  * message such as "my string" << aNumber << "my oth stream".
       
   133  *
       
   134  * Typical usage looks like:
       
   135  * \code
       
   136  * NS_LOG (LOG_DEBUG, "a number="<<aNumber<<", anotherNumber="<<anotherNumber);
       
   137  * \endcode
       
   138  */
       
   139 #define NS_LOG(level, msg)                                      \
       
   140   do                                                            \
       
   141     {                                                           \
       
   142       if (g_log.IsEnabled (level))                              \
       
   143         {                                                       \
       
   144           if (g_log.IsEnabled (ns3::LOG_PREFIX_ALL))            \
       
   145             {                                                   \
       
   146               std::clog << g_log.Name () << ":" <<              \
       
   147                 __FUNCTION__ << "(): ";                         \
       
   148             }                                                   \
       
   149           std::clog << msg << std::endl;                        \
       
   150         }                                                       \
       
   151     }                                                           \
       
   152   while (false)
       
   153 
       
   154 #define NS_LOG_F(level)                                         \
       
   155   do                                                            \
       
   156     {                                                           \
       
   157       if (g_log.IsEnabled (level))                              \
       
   158         {                                                       \
       
   159           std::clog << g_log.Name () << ":" << __FUNCTION__ <<  \
       
   160             "()" << std::endl;                                \
       
   161         }                                                       \
       
   162     }                                                           \
       
   163   while (false)
       
   164 
       
   165 #define NS_LOG_ERROR(msg) \
       
   166   NS_LOG(ns3::LOG_ERROR, msg)
       
   167 
       
   168 #define NS_LOG_WARN(msg) \
       
   169   NS_LOG(ns3::LOG_WARN, msg)
       
   170 
       
   171 #define NS_LOG_DEBUG(msg) \
       
   172   NS_LOG(ns3::LOG_DEBUG, msg)
       
   173 
       
   174 #define NS_LOG_INFO(msg) \
       
   175   NS_LOG(ns3::LOG_INFO, msg)
       
   176 
       
   177 #define NS_LOG_FUNCTION \
       
   178   NS_LOG_F(ns3::LOG_FUNCTION)
       
   179 
       
   180 
       
   181 
       
   182 #define NS_LOG_PARAMS(parameters)                       \
       
   183   do                                                    \
       
   184     {                                                   \
       
   185       if (g_log.IsEnabled (ns3::LOG_PARAM))             \
       
   186         {                                               \
       
   187           std::clog << g_log.Name () << ":"             \
       
   188                     << __FUNCTION__ << "(";             \
       
   189           g_parameterLogger << parameters;              \
       
   190           std::clog << ")" << std::endl;                \
       
   191         }                                               \
       
   192     }                                                   \
       
   193   while (false)
       
   194 
       
   195 
       
   196 #define NS_LOG_LOGIC(msg) \
       
   197   NS_LOG(ns3::LOG_LOGIC, msg)
       
   198 
       
   199 #define NS_LOG_UNCOND(msg)              \
       
   200   do                                    \
       
   201     {                                   \
       
   202       std::clog << msg << std::endl;    \
       
   203     }                                   \
       
   204   while (false)
       
   205 
       
   206 #else
       
   207 
       
   208 #define NS_LOG(level, msg)
   281 #define NS_LOG(level, msg)
   209 #define NS_LOG_F(level)
       
   210 #define NS_LOG_ERROR(msg)
   282 #define NS_LOG_ERROR(msg)
   211 #define NS_LOG_WARN(msg)
   283 #define NS_LOG_WARN(msg)
   212 #define NS_LOG_DEBUG(msg)
   284 #define NS_LOG_DEBUG(msg)
   213 #define NS_LOG_INFO(msg)
   285 #define NS_LOG_INFO(msg)
   214 #define NS_LOG_FUNCTION
   286 #define NS_LOG_FUNCTION
   215 #define NS_LOG_PARAMS(parameters)
   287 #define NS_LOG_PARAMS(parameters)
   216 #define NS_LOG_LOGIC(msg)
   288 #define NS_LOG_LOGIC(msg)
   217 #define NS_LOG_UNCOND(msg)
   289 #define NS_LOG_UNCOND(msg)
   218 
   290 
   219 #endif
   291 #define LogComponentPrintList
   220 
   292 #define LogComponentEnable(name,level)
   221 namespace ns3 {
   293 #define LogComponentDisable(name,level)
   222 
   294 #define LogComponentEnableAll(level)
   223 #ifdef NS3_LOG_ENABLE
   295 #define LogComponentDisableAll(level)
   224 
   296 
   225 enum LogLevel {
   297 #endif /* LOG_ENABLE */
   226   LOG_NONE           = 0x00000000, // no logging
       
   227 
       
   228   LOG_ERROR          = 0x00000001, // serious error messages only
       
   229   LOG_LEVEL_ERROR    = 0x00000001,
       
   230 
       
   231   LOG_WARN           = 0x00000002, // warning messages
       
   232   LOG_LEVEL_WARN     = 0x00000003,
       
   233 
       
   234   LOG_DEBUG          = 0x00000004, // rare ad-hoc debug messages
       
   235   LOG_LEVEL_DEBUG    = 0x00000007,
       
   236 
       
   237   LOG_INFO           = 0x00000008, // informational messages (e.g., banners)
       
   238   LOG_LEVEL_INFO     = 0x0000000f,
       
   239 
       
   240   LOG_FUNCTION       = 0x00000010, // function tracing
       
   241   LOG_LEVEL_FUNCTION = 0x0000001f, 
       
   242 
       
   243   LOG_PARAM          = 0x00000020, // parameters to functions
       
   244   LOG_LEVEL_PARAM    = 0x0000003f,
       
   245 
       
   246   LOG_LOGIC          = 0x00000040, // control flow tracing within functions
       
   247   LOG_LEVEL_LOGIC    = 0x0000007f,
       
   248 
       
   249   LOG_ALL            = 0x7fffffff, // print everything
       
   250   LOG_LEVEL_ALL      = LOG_ALL,
       
   251 
       
   252   LOG_PREFIX_ALL     = 0x80000000  // prefix all trace prints with function
       
   253 };
       
   254 
       
   255 #endif
       
   256 
       
   257 #ifdef NS3_LOG_ENABLE
       
   258 /**
       
   259  * \param name a log component name
       
   260  * \param level a logging level
       
   261  * \ingroup logging
       
   262  *
       
   263  * Enable the logging output associated with that log component.
       
   264  * The logging output can be later disabled with a call
       
   265  * to ns3::LogComponentDisable.
       
   266  */
       
   267   void LogComponentEnable (char const *name, enum LogLevel level);
       
   268 
       
   269 /**
       
   270  * \param level a logging level
       
   271  * \ingroup logging
       
   272  *
       
   273  * Enable the logging output for all registered log components.
       
   274  */
       
   275   void LogComponentEnableAll (enum LogLevel level);
       
   276 #else
       
   277 #define LogComponentEnable(a,b)
       
   278 #define LogComponentEnableAll(a)
       
   279 #endif
       
   280 
       
   281 #ifdef NS3_LOG_ENABLE
       
   282 /**
       
   283  * \param name a log component name
       
   284  * \param level a logging level
       
   285  * \ingroup logging
       
   286  *
       
   287  * Disable the logging output associated with that log component.
       
   288  * The logging output can be later re-enabled with a call
       
   289  * to ns3::LogComponentEnable.
       
   290  */
       
   291 void LogComponentDisable (char const *name, enum LogLevel level);
       
   292 
       
   293 /**
       
   294  * \param level a logging level
       
   295  * \ingroup logging
       
   296  *
       
   297  * Disable all logging for all components.
       
   298  */
       
   299 void LogComponentDisableAll (enum LogLevel level);
       
   300 
       
   301 #else
       
   302 #define LogComponentDisable(a,b)
       
   303 #define LogComponentDisableAll(a)
       
   304 #endif
       
   305 
       
   306 /**
       
   307  * \ingroup logging
       
   308  *
       
   309  * Print the list of logging messages available.
       
   310  */
       
   311 #ifdef NS3_LOG_ENABLE
       
   312 void LogComponentPrintList (void);
       
   313 #else
       
   314 #define LogComponentPrintList()
       
   315 #endif
       
   316 
       
   317 #ifdef NS3_LOG_ENABLE
       
   318 
       
   319 class LogComponent {
       
   320 public:
       
   321   LogComponent (char const *name);
       
   322   void EnvVarCheck (char const *name);
       
   323   bool IsEnabled (enum LogLevel level) const;
       
   324   bool IsNoneEnabled (void) const;
       
   325   void Enable (enum LogLevel level);
       
   326   void Disable (enum LogLevel level);
       
   327   bool Decorate (void) const;
       
   328   char const *Name (void) const;
       
   329 private:
       
   330   int32_t     m_levels;
       
   331   char const *m_name;
       
   332   bool        m_decorate;
       
   333 };
       
   334 
       
   335 #endif
       
   336 
       
   337 } // namespace ns3
       
   338 
   298 
   339 #endif // __LOG_H__
   299 #endif // __LOG_H__