src/core/log.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3005 cc521f35f033
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
     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 #include "log.h"
    21 
    22 #ifdef NS3_LOG_ENABLE
    23 
    24 #include <list>
    25 #include <utility>
    26 #include <iostream>
    27 #include "assert.h"
    28 #include "ns3/core-config.h"
    29 #include "fatal-error.h"
    30 
    31 #ifdef HAVE_STDLIB_H
    32 #include <stdlib.h>
    33 #endif
    34 
    35 namespace ns3 {
    36 
    37 LogTimePrinter g_logTimePrinter = 0;
    38 
    39 typedef std::list<std::pair <std::string, LogComponent *> > ComponentList;
    40 typedef std::list<std::pair <std::string, LogComponent *> >::iterator ComponentListI;
    41 
    42 static class PrintList
    43 {
    44 public:
    45   PrintList ();
    46 } g_printList;
    47 
    48 static 
    49 ComponentList *GetComponentList (void)
    50 {
    51   static ComponentList components;
    52   return &components;
    53 }
    54 
    55 
    56 
    57 PrintList::PrintList ()
    58 {
    59 #ifdef HAVE_GETENV
    60   char *envVar = getenv("NS_LOG");
    61   if (envVar == 0)
    62     {
    63       return;
    64     }
    65   std::string env = envVar;
    66   std::string::size_type cur = 0;
    67   std::string::size_type next = 0;
    68   while (next != std::string::npos)
    69     {
    70       next = env.find_first_of (":", cur);
    71       std::string tmp = std::string (env, cur, next-cur);
    72       if (tmp == "print-list")
    73         {
    74           LogComponentPrintList ();
    75           exit (0);
    76           break;
    77         }
    78       cur = next + 1;
    79     }
    80 #endif  
    81 }
    82 
    83 
    84 LogComponent::LogComponent (char const * name)
    85   : m_levels (0), m_name (name)
    86 {
    87   EnvVarCheck (name);
    88 
    89   ComponentList *components = GetComponentList ();
    90   for (ComponentListI i = components->begin ();
    91        i != components->end ();
    92        i++)
    93     {
    94       if (i->first == name)
    95         {
    96           NS_FATAL_ERROR ("Log component \""<<name<<"\" has already been registered once.");
    97         }
    98     }
    99   components->push_back (std::make_pair (name, this));
   100 }
   101 
   102 void
   103 LogComponent::EnvVarCheck (char const * name)
   104 {
   105 #ifdef HAVE_GETENV
   106   char *envVar = getenv("NS_LOG");
   107   if (envVar == 0)
   108     {
   109       return;
   110     }
   111   std::string env = envVar;
   112   std::string myName = name;
   113 
   114   std::string::size_type cur = 0;
   115   std::string::size_type next = 0;
   116   while (next != std::string::npos)
   117     {
   118       next = env.find_first_of (":", cur);
   119       std::string tmp = std::string (env, cur, next-cur);
   120       std::string::size_type equal = tmp.find ("=");
   121       std::string component;
   122       if (equal == std::string::npos)
   123         {
   124           component = tmp;
   125           if (component == myName || component == "*")
   126             {
   127               int level = LOG_ALL | LOG_PREFIX_TIME | LOG_PREFIX_FUNC;
   128               Enable ((enum LogLevel)level);
   129               return;
   130             }
   131         }
   132       else
   133         {
   134           component = tmp.substr (0, equal);
   135           if (component == myName || component == "*")
   136             {
   137               int level = 0;
   138               std::string::size_type cur_lev;
   139               std::string::size_type next_lev = equal;
   140               do
   141                 {
   142                   cur_lev = next_lev + 1;
   143                   next_lev = tmp.find ("|", cur_lev);
   144                   std::string lev = tmp.substr (cur_lev, next_lev - cur_lev);
   145                   if (lev == "error")
   146                     {
   147                       level |= LOG_ERROR;
   148                     }
   149                   else if (lev == "warn")
   150                     {
   151                       level |= LOG_WARN;
   152                     }
   153                   else if (lev == "debug")
   154                     {
   155                       level |= LOG_DEBUG;
   156                     }
   157                   else if (lev == "info")
   158                     {
   159                       level |= LOG_INFO;
   160                     }
   161                   else if (lev == "function")
   162                     {
   163                       level |= LOG_FUNCTION;
   164                     }
   165                   else if (lev == "logic")
   166                     {
   167                       level |= LOG_LOGIC;
   168                     }
   169                   else if (lev == "all")
   170                     {
   171                       level |= LOG_ALL;
   172                     }
   173                   else if (lev == "prefix_func")
   174                     {
   175                       level |= LOG_PREFIX_FUNC;
   176                     }
   177                   else if (lev == "prefix_time")
   178                     {
   179                       level |= LOG_PREFIX_TIME;
   180                     }
   181                   else if (lev == "level_error")
   182                     {
   183                       level |= LOG_LEVEL_ERROR;
   184                     }
   185                   else if (lev == "level_warn")
   186                     {
   187                       level |= LOG_LEVEL_WARN;
   188                     }
   189                   else if (lev == "level_debug")
   190                     {
   191                       level |= LOG_LEVEL_DEBUG;
   192                     }
   193                   else if (lev == "level_info")
   194                     {
   195                       level |= LOG_LEVEL_INFO;
   196                     }
   197                   else if (lev == "level_function")
   198                     {
   199                       level |= LOG_LEVEL_FUNCTION;
   200                     }
   201                   else if (lev == "level_logic")
   202                     {
   203                       level |= LOG_LEVEL_LOGIC;
   204                     }
   205                   else if (lev == "level_all")
   206                     {
   207                       level |= LOG_LEVEL_ALL;
   208                     }
   209                 } while (next_lev != std::string::npos);
   210 
   211               Enable ((enum LogLevel)level);
   212             }
   213         }
   214       cur = next + 1;
   215     }
   216 #endif
   217 }
   218 
   219 
   220 bool 
   221 LogComponent::IsEnabled (enum LogLevel level) const
   222 {
   223   //  LogComponentEnableEnvVar ();
   224   return (level & m_levels) ? 1 : 0;
   225 }
   226 
   227 bool
   228 LogComponent::IsNoneEnabled (void) const
   229 {
   230   return m_levels == 0;
   231 }
   232 
   233 void 
   234 LogComponent::Enable (enum LogLevel level)
   235 {
   236   m_levels |= level;
   237 }
   238 
   239 void 
   240 LogComponent::Disable (enum LogLevel level)
   241 {
   242   m_levels &= ~level;
   243 }
   244 
   245 char const *
   246 LogComponent::Name (void) const
   247 {
   248   return m_name;
   249 }
   250 
   251 
   252 void 
   253 LogComponentEnable (char const *name, enum LogLevel level)
   254 {
   255   ComponentList *components = GetComponentList ();
   256   for (ComponentListI i = components->begin ();
   257        i != components->end ();
   258        i++)
   259     {
   260       if (i->first.compare (name) == 0) 
   261 	{
   262 	  i->second->Enable (level);
   263 	  break;
   264 	}
   265     }  
   266 }
   267 
   268 void 
   269 LogComponentEnableAll (enum LogLevel level)
   270 {
   271   ComponentList *components = GetComponentList ();
   272   for (ComponentListI i = components->begin ();
   273        i != components->end ();
   274        i++)
   275     {
   276       i->second->Enable (level);
   277     }  
   278 }
   279 
   280 void 
   281 LogComponentDisable (char const *name, enum LogLevel level)
   282 {
   283   ComponentList *components = GetComponentList ();
   284   for (ComponentListI i = components->begin ();
   285        i != components->end ();
   286        i++)
   287     {
   288       if (i->first.compare (name) == 0) 
   289 	{
   290 	  i->second->Disable (level);
   291 	  break;
   292 	}
   293     }  
   294 }
   295 
   296 void 
   297 LogComponentDisableAll (enum LogLevel level)
   298 {
   299   ComponentList *components = GetComponentList ();
   300   for (ComponentListI i = components->begin ();
   301        i != components->end ();
   302        i++)
   303     {
   304       i->second->Disable (level);
   305     }  
   306 }
   307 
   308 void 
   309 LogComponentPrintList (void)
   310 {
   311   ComponentList *components = GetComponentList ();
   312   for (ComponentListI i = components->begin ();
   313        i != components->end ();
   314        i++)
   315     {
   316       std::cout << i->first << "=";
   317       if (i->second->IsNoneEnabled ())
   318         {
   319           std::cout << "0" << std::endl;
   320           continue;
   321         }
   322       if (i->second->IsEnabled (LOG_ERROR))
   323         {
   324           std::cout << "error";
   325         }
   326       if (i->second->IsEnabled (LOG_WARN))
   327         {
   328           std::cout << "|warn";
   329         }
   330       if (i->second->IsEnabled (LOG_DEBUG))
   331         {
   332           std::cout << "|debug";
   333         }
   334       if (i->second->IsEnabled (LOG_INFO))
   335         {
   336           std::cout << "|info";
   337         }
   338       if (i->second->IsEnabled (LOG_FUNCTION))
   339         {
   340           std::cout << "|function";
   341         }
   342       if (i->second->IsEnabled (LOG_LOGIC))
   343         {
   344           std::cout << "|logic";
   345         }
   346       if (i->second->IsEnabled (LOG_ALL))
   347         {
   348           std::cout << "|all";
   349         }
   350       std::cout << std::endl;
   351     }
   352 }
   353 
   354 void LogSetTimePrinter (LogTimePrinter printer)
   355 {
   356   g_logTimePrinter = printer;
   357 }
   358 LogTimePrinter LogGetTimePrinter(void)
   359 {
   360   return g_logTimePrinter;
   361 }
   362 
   363 
   364 ParameterLogger::ParameterLogger (std::ostream &os)
   365   : m_itemNumber (0),
   366     m_os (os)
   367 {}
   368 
   369 } // namespace ns3
   370 
   371 #else // NS3_LOG_ENABLE
   372 
   373 namespace ns3 {
   374 
   375 void 
   376 LogComponentEnable (char const *name, enum LogLevel level)
   377 {
   378 
   379 }
   380 
   381 void 
   382 LogComponentEnableAll (enum LogLevel level)
   383 {
   384 
   385 }
   386 
   387 void 
   388 LogComponentDisable (char const *name, enum LogLevel level)
   389 {
   390 
   391 }
   392 
   393 void 
   394 LogComponentDisableAll (enum LogLevel level)
   395 {
   396 
   397 }
   398 
   399 } // namespace ns3
   400 
   401 #endif