src/core/log.h
changeset 1498 520bc8457799
child 1503 53dd8f414ba6
equal deleted inserted replaced
1497:dff0a53e60f2 1498:520bc8457799
       
     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 #ifndef LOG_H
       
    21 #define LOG_H
       
    22 
       
    23 #include <string>
       
    24 #include <iostream>
       
    25 
       
    26 /**
       
    27  * \defgroup logging Logging
       
    28  * \brief Logging functions and macros
       
    29  *
       
    30  *   LOG functionality: macros which allow developers to
       
    31  *     send information out on screen. All logging messages 
       
    32  *     are disabled by default. To enable selected logging 
       
    33  *     messages, use the ns3::LogComponentEnable
       
    34  *     function. 
       
    35  * 
       
    36  * Alternatively, you can use the NS_LOG
       
    37  * environment variable to define a ';'-separated list of
       
    38  * logging components to enable. For example, NS_LOG=a;b;c;DAFD;GH
       
    39  * would enable the components 'a', 'b', 'c', 'DAFD', and, 'GH'.
       
    40  *
       
    41  * For each component, the "debug" log level is enabled by default
       
    42  * but more components can be enabled selectively with the following
       
    43  * syntax: NS_LOG='Component1=func|param|warn;Component2=error|debug'
       
    44  * This example would enable the 'func', 'param', and 'warn' log
       
    45  * levels for 'Component1' and the 'error' and 'debug' log levels
       
    46  * for 'Component2'.
       
    47  *
       
    48  * The list of available log components can be printed on stdout
       
    49  * with the NS_LOG=print-list syntax.
       
    50  */
       
    51 
       
    52 /**
       
    53  * \ingroup logging
       
    54  * \param name a string
       
    55  *
       
    56  * Define a Log component with a specific name. This macro
       
    57  * should be used at the top of every file in which you want 
       
    58  * to use the NS_LOG macro. This macro defines a new
       
    59  * "log component" which can be later selectively enabled
       
    60  * or disabled with the ns3::LogComponentEnable and 
       
    61  * ns3::LogComponentDisable functions or with the NS_LOG
       
    62  * environment variable.
       
    63  */
       
    64 #define NS_LOG_COMPONENT_DEFINE(name)                                \
       
    65   static ns3::LogComponent g_log = ns3::LogComponent (name)
       
    66 
       
    67 /**
       
    68  * \ingroup logging
       
    69  * \param msg message to output
       
    70  *
       
    71  * Generate logging output in the "log component" of the 
       
    72  * current file. i.e., every call to NS_LOG from within
       
    73  * a file implicitely generates out within the component
       
    74  * defined with the NS_LOG_COMPONENT_DEFINE macro in the
       
    75  * same file.
       
    76  */
       
    77 #define NS_LOG(level,msg)                       \
       
    78   do                                            \
       
    79     {                                           \
       
    80       if (g_log.IsEnabled (level))              \
       
    81         {                                       \
       
    82           std::clog << msg << std::endl;        \
       
    83         }                                       \
       
    84     }                                           \
       
    85   while (false)
       
    86 
       
    87 #define NS_LOG_DEBUG(msg) \
       
    88   NS_LOG (ns3::LOG_LEVEL_DEBUG,msg)
       
    89 
       
    90 #define NS_LOG_FUNCTION \
       
    91   NS_LOG (ns3::LOG_LEVEL_FUNCTION, __PRETTY_PRINT__)
       
    92 
       
    93 #define NS_LOG_PARAM(msg) \
       
    94   NS_LOG (ns3::LOG_LEVEL_PARAM,msg)
       
    95 
       
    96 #define NS_LOG_WARN(msg) \
       
    97   NS_LOG (ns3::LOG_LEVEL_WARN,msg)
       
    98 
       
    99 #define NS_LOG_ERROR(msg) \
       
   100   NS_LOG (ns3::LOG_LEVEL_ERROR,msg)
       
   101 
       
   102 #define NS_LOG_UNCOND(msg) \
       
   103   do                                            \
       
   104     {                                           \
       
   105       std::clog << msg << std::endl;            \
       
   106     }                                           \
       
   107   while (false)
       
   108 
       
   109 
       
   110 
       
   111 namespace ns3 {
       
   112 
       
   113 enum LogLevel {
       
   114   LOG_LEVEL_DEBUG    = 1<<0,
       
   115   LOG_LEVEL_FUNCTION = 1<<1,
       
   116   LOG_LEVEL_PARAM    = 1<<2,
       
   117   LOG_LEVEL_WARN     = 1<<3,
       
   118   LOG_LEVEL_ERROR    = 1<<4,
       
   119   LOG_LEVEL_LAST     = 1<<31
       
   120 };
       
   121 
       
   122 /**
       
   123  * \param name a log component name
       
   124  * \ingroup logging
       
   125  *
       
   126  * Enable the logging output associated with that log component.
       
   127  * The logging output can be later disabled with a call
       
   128  * to ns3::LogComponentDisable.
       
   129  */
       
   130 void LogComponentEnable (char const *name, enum LogLevel level);
       
   131 
       
   132 /**
       
   133  * \param name a log component name
       
   134  * \ingroup logging
       
   135  *
       
   136  * Disable the logging output associated with that log component.
       
   137  * The logging output can be later re-enabled with a call
       
   138  * to ns3::LogComponentEnable.
       
   139  */
       
   140 void LogComponentDisable (char const *name, enum LogLevel level);
       
   141 
       
   142 /**
       
   143  * \ingroup logging
       
   144  *
       
   145  * Print the list of logging messages available.
       
   146  * The output of this function can be obtained by setting
       
   147  * the NS_LOG environment variable to the special value 
       
   148  * 'print-list'.
       
   149  * 
       
   150  * For example: NS_LOG=print-list
       
   151  */
       
   152 void LogComponentPrintList (void);
       
   153 
       
   154 class LogComponent {
       
   155 public:
       
   156   LogComponent (char const *name);
       
   157   bool IsEnabled (enum LogLevel level) const;
       
   158   bool IsNoneEnabled (void) const;
       
   159   void Enable (enum LogLevel level);
       
   160   void Disable (enum LogLevel level);
       
   161 private:
       
   162   uint32_t m_levels;
       
   163 };
       
   164 
       
   165 } // namespace ns3
       
   166 
       
   167 #endif /* LOG_H */