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
craigdo@1498
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
craigdo@1498
     2
/*
craigdo@1498
     3
 * Copyright (c) 2006,2007 INRIA
craigdo@1498
     4
 *
craigdo@1498
     5
 * This program is free software; you can redistribute it and/or modify
craigdo@1498
     6
 * it under the terms of the GNU General Public License version 2 as
craigdo@1498
     7
 * published by the Free Software Foundation;
craigdo@1498
     8
 *
craigdo@1498
     9
 * This program is distributed in the hope that it will be useful,
craigdo@1498
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
craigdo@1498
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
craigdo@1498
    12
 * GNU General Public License for more details.
craigdo@1498
    13
 *
craigdo@1498
    14
 * You should have received a copy of the GNU General Public License
craigdo@1498
    15
 * along with this program; if not, write to the Free Software
craigdo@1498
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
craigdo@1498
    17
 *
craigdo@1498
    18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
craigdo@1498
    19
 */
gjc@3483
    20
#include "log.h"
craigdo@1506
    21
craigdo@1506
    22
#ifdef NS3_LOG_ENABLE
craigdo@1506
    23
craigdo@1498
    24
#include <list>
craigdo@1498
    25
#include <utility>
craigdo@1498
    26
#include <iostream>
craigdo@1498
    27
#include "assert.h"
craigdo@1498
    28
#include "ns3/core-config.h"
craigdo@1498
    29
#include "fatal-error.h"
craigdo@1498
    30
craigdo@1498
    31
#ifdef HAVE_STDLIB_H
craigdo@1498
    32
#include <stdlib.h>
craigdo@1498
    33
#endif
craigdo@1498
    34
craigdo@1498
    35
namespace ns3 {
craigdo@1498
    36
mathieu@2979
    37
LogTimePrinter g_logTimePrinter = 0;
mathieu@2979
    38
craigdo@1498
    39
typedef std::list<std::pair <std::string, LogComponent *> > ComponentList;
craigdo@1498
    40
typedef std::list<std::pair <std::string, LogComponent *> >::iterator ComponentListI;
craigdo@1498
    41
mathieu@1861
    42
static class PrintList
mathieu@1861
    43
{
mathieu@1861
    44
public:
mathieu@1861
    45
  PrintList ();
mathieu@1861
    46
} g_printList;
mathieu@1861
    47
craigdo@1498
    48
static 
craigdo@1498
    49
ComponentList *GetComponentList (void)
craigdo@1498
    50
{
craigdo@1498
    51
  static ComponentList components;
craigdo@1498
    52
  return &components;
craigdo@1498
    53
}
craigdo@1498
    54
mathieu@1861
    55
mathieu@1861
    56
mathieu@1861
    57
PrintList::PrintList ()
mathieu@1861
    58
{
mathieu@1861
    59
#ifdef HAVE_GETENV
mathieu@1861
    60
  char *envVar = getenv("NS_LOG");
mathieu@1861
    61
  if (envVar == 0)
mathieu@1861
    62
    {
mathieu@1861
    63
      return;
mathieu@1861
    64
    }
mathieu@1861
    65
  std::string env = envVar;
mathieu@1861
    66
  std::string::size_type cur = 0;
mathieu@1861
    67
  std::string::size_type next = 0;
mathieu@1861
    68
  while (next != std::string::npos)
mathieu@1861
    69
    {
mathieu@2982
    70
      next = env.find_first_of (":", cur);
mathieu@1861
    71
      std::string tmp = std::string (env, cur, next-cur);
mathieu@1861
    72
      if (tmp == "print-list")
mathieu@1861
    73
        {
mathieu@1861
    74
          LogComponentPrintList ();
mathieu@1861
    75
          exit (0);
mathieu@1861
    76
          break;
mathieu@1861
    77
        }
mathieu@1861
    78
      cur = next + 1;
mathieu@1861
    79
    }
mathieu@1861
    80
#endif  
mathieu@1861
    81
}
mathieu@1861
    82
mathieu@1861
    83
craigdo@1783
    84
LogComponent::LogComponent (char const * name)
craigdo@1783
    85
  : m_levels (0), m_name (name)
craigdo@1783
    86
{
craigdo@1783
    87
  EnvVarCheck (name);
craigdo@1783
    88
craigdo@1783
    89
  ComponentList *components = GetComponentList ();
craigdo@1783
    90
  for (ComponentListI i = components->begin ();
craigdo@1783
    91
       i != components->end ();
craigdo@1783
    92
       i++)
craigdo@1783
    93
    {
mathieu@2809
    94
      if (i->first == name)
mathieu@2809
    95
        {
mathieu@2809
    96
          NS_FATAL_ERROR ("Log component \""<<name<<"\" has already been registered once.");
mathieu@2809
    97
        }
craigdo@1783
    98
    }
craigdo@1783
    99
  components->push_back (std::make_pair (name, this));
craigdo@1783
   100
}
craigdo@1783
   101
craigdo@1498
   102
void
craigdo@1783
   103
LogComponent::EnvVarCheck (char const * name)
craigdo@1498
   104
{
craigdo@1498
   105
#ifdef HAVE_GETENV
craigdo@1498
   106
  char *envVar = getenv("NS_LOG");
craigdo@1498
   107
  if (envVar == 0)
craigdo@1498
   108
    {
craigdo@1498
   109
      return;
craigdo@1498
   110
    }
craigdo@1498
   111
  std::string env = envVar;
craigdo@1783
   112
  std::string myName = name;
craigdo@1783
   113
craigdo@1498
   114
  std::string::size_type cur = 0;
craigdo@1498
   115
  std::string::size_type next = 0;
mathieu@1862
   116
  while (next != std::string::npos)
craigdo@1498
   117
    {
mathieu@2982
   118
      next = env.find_first_of (":", cur);
mathieu@1862
   119
      std::string tmp = std::string (env, cur, next-cur);
craigdo@1498
   120
      std::string::size_type equal = tmp.find ("=");
craigdo@1498
   121
      std::string component;
craigdo@1498
   122
      if (equal == std::string::npos)
craigdo@1498
   123
        {
craigdo@1498
   124
          component = tmp;
craigdo@1783
   125
          if (component == myName || component == "*")
craigdo@1783
   126
            {
mathieu@2982
   127
              int level = LOG_ALL | LOG_PREFIX_TIME | LOG_PREFIX_FUNC;
mathieu@2982
   128
              Enable ((enum LogLevel)level);
craigdo@1783
   129
              return;
craigdo@1783
   130
            }
craigdo@1498
   131
        }
craigdo@1498
   132
      else
craigdo@1498
   133
        {
craigdo@1498
   134
          component = tmp.substr (0, equal);
craigdo@1783
   135
          if (component == myName || component == "*")
craigdo@1498
   136
            {
craigdo@1783
   137
              int level = 0;
craigdo@1783
   138
              std::string::size_type cur_lev;
craigdo@1783
   139
              std::string::size_type next_lev = equal;
craigdo@1783
   140
              do
craigdo@1503
   141
                {
craigdo@1783
   142
                  cur_lev = next_lev + 1;
craigdo@1783
   143
                  next_lev = tmp.find ("|", cur_lev);
craigdo@1783
   144
                  std::string lev = tmp.substr (cur_lev, next_lev - cur_lev);
craigdo@1783
   145
                  if (lev == "error")
craigdo@1783
   146
                    {
craigdo@1783
   147
                      level |= LOG_ERROR;
craigdo@1783
   148
                    }
craigdo@1783
   149
                  else if (lev == "warn")
craigdo@1783
   150
                    {
craigdo@1783
   151
                      level |= LOG_WARN;
craigdo@1783
   152
                    }
craigdo@1783
   153
                  else if (lev == "debug")
craigdo@1783
   154
                    {
craigdo@1783
   155
                      level |= LOG_DEBUG;
craigdo@1783
   156
                    }
craigdo@1783
   157
                  else if (lev == "info")
craigdo@1783
   158
                    {
craigdo@1783
   159
                      level |= LOG_INFO;
craigdo@1783
   160
                    }
craigdo@1783
   161
                  else if (lev == "function")
craigdo@1783
   162
                    {
craigdo@1783
   163
                      level |= LOG_FUNCTION;
craigdo@1783
   164
                    }
craigdo@1783
   165
                  else if (lev == "logic")
craigdo@1783
   166
                    {
craigdo@1783
   167
                      level |= LOG_LOGIC;
craigdo@1783
   168
                    }
craigdo@1783
   169
                  else if (lev == "all")
craigdo@1783
   170
                    {
craigdo@1783
   171
                      level |= LOG_ALL;
craigdo@1783
   172
                    }
mathieu@2979
   173
                  else if (lev == "prefix_func")
craigdo@1783
   174
                    {
mathieu@2979
   175
                      level |= LOG_PREFIX_FUNC;
mathieu@2979
   176
                    }
mathieu@2979
   177
                  else if (lev == "prefix_time")
mathieu@2979
   178
                    {
mathieu@2979
   179
                      level |= LOG_PREFIX_TIME;
craigdo@1783
   180
                    }
craigdo@1783
   181
                  else if (lev == "level_error")
craigdo@1783
   182
                    {
craigdo@1783
   183
                      level |= LOG_LEVEL_ERROR;
craigdo@1783
   184
                    }
craigdo@1783
   185
                  else if (lev == "level_warn")
craigdo@1783
   186
                    {
craigdo@1783
   187
                      level |= LOG_LEVEL_WARN;
craigdo@1783
   188
                    }
craigdo@1783
   189
                  else if (lev == "level_debug")
craigdo@1783
   190
                    {
craigdo@1783
   191
                      level |= LOG_LEVEL_DEBUG;
craigdo@1783
   192
                    }
craigdo@1783
   193
                  else if (lev == "level_info")
craigdo@1783
   194
                    {
craigdo@1783
   195
                      level |= LOG_LEVEL_INFO;
craigdo@1783
   196
                    }
craigdo@1783
   197
                  else if (lev == "level_function")
craigdo@1783
   198
                    {
craigdo@1783
   199
                      level |= LOG_LEVEL_FUNCTION;
craigdo@1783
   200
                    }
craigdo@1783
   201
                  else if (lev == "level_logic")
craigdo@1783
   202
                    {
craigdo@1783
   203
                      level |= LOG_LEVEL_LOGIC;
craigdo@1783
   204
                    }
craigdo@1783
   205
                  else if (lev == "level_all")
craigdo@1783
   206
                    {
craigdo@1783
   207
                      level |= LOG_LEVEL_ALL;
craigdo@1783
   208
                    }
craigdo@1783
   209
                } while (next_lev != std::string::npos);
craigdo@1783
   210
craigdo@1783
   211
              Enable ((enum LogLevel)level);
craigdo@1498
   212
            }
craigdo@1498
   213
        }
craigdo@1498
   214
      cur = next + 1;
craigdo@1498
   215
    }
craigdo@1498
   216
#endif
craigdo@1498
   217
}
craigdo@1498
   218
craigdo@1503
   219
craigdo@1498
   220
bool 
craigdo@1498
   221
LogComponent::IsEnabled (enum LogLevel level) const
craigdo@1498
   222
{
craigdo@1775
   223
  //  LogComponentEnableEnvVar ();
craigdo@1507
   224
  return (level & m_levels) ? 1 : 0;
craigdo@1498
   225
}
craigdo@1503
   226
craigdo@1498
   227
bool
craigdo@1498
   228
LogComponent::IsNoneEnabled (void) const
craigdo@1498
   229
{
craigdo@1498
   230
  return m_levels == 0;
craigdo@1498
   231
}
craigdo@1503
   232
craigdo@1498
   233
void 
craigdo@1498
   234
LogComponent::Enable (enum LogLevel level)
craigdo@1498
   235
{
craigdo@1498
   236
  m_levels |= level;
craigdo@1498
   237
}
craigdo@1503
   238
craigdo@1498
   239
void 
craigdo@1498
   240
LogComponent::Disable (enum LogLevel level)
craigdo@1498
   241
{
craigdo@1498
   242
  m_levels &= ~level;
craigdo@1498
   243
}
craigdo@1498
   244
craigdo@1772
   245
char const *
craigdo@1772
   246
LogComponent::Name (void) const
craigdo@1772
   247
{
craigdo@1772
   248
  return m_name;
craigdo@1772
   249
}
craigdo@1772
   250
craigdo@1772
   251
craigdo@1498
   252
void 
craigdo@1498
   253
LogComponentEnable (char const *name, enum LogLevel level)
craigdo@1498
   254
{
craigdo@1498
   255
  ComponentList *components = GetComponentList ();
craigdo@1498
   256
  for (ComponentListI i = components->begin ();
craigdo@1498
   257
       i != components->end ();
craigdo@1498
   258
       i++)
craigdo@1498
   259
    {
craigdo@1498
   260
      if (i->first.compare (name) == 0) 
craigdo@1498
   261
	{
craigdo@1498
   262
	  i->second->Enable (level);
craigdo@1498
   263
	  break;
craigdo@1498
   264
	}
craigdo@1498
   265
    }  
craigdo@1498
   266
}
craigdo@1503
   267
craigdo@1498
   268
void 
craigdo@1772
   269
LogComponentEnableAll (enum LogLevel level)
craigdo@1772
   270
{
craigdo@1772
   271
  ComponentList *components = GetComponentList ();
craigdo@1772
   272
  for (ComponentListI i = components->begin ();
craigdo@1772
   273
       i != components->end ();
craigdo@1772
   274
       i++)
craigdo@1772
   275
    {
craigdo@1772
   276
      i->second->Enable (level);
craigdo@1772
   277
    }  
craigdo@1772
   278
}
craigdo@1772
   279
craigdo@1772
   280
void 
craigdo@1498
   281
LogComponentDisable (char const *name, enum LogLevel level)
craigdo@1498
   282
{
craigdo@1498
   283
  ComponentList *components = GetComponentList ();
craigdo@1498
   284
  for (ComponentListI i = components->begin ();
craigdo@1498
   285
       i != components->end ();
craigdo@1498
   286
       i++)
craigdo@1498
   287
    {
craigdo@1498
   288
      if (i->first.compare (name) == 0) 
craigdo@1498
   289
	{
craigdo@1498
   290
	  i->second->Disable (level);
craigdo@1498
   291
	  break;
craigdo@1498
   292
	}
craigdo@1498
   293
    }  
craigdo@1498
   294
}
craigdo@1498
   295
craigdo@1498
   296
void 
craigdo@1772
   297
LogComponentDisableAll (enum LogLevel level)
craigdo@1772
   298
{
craigdo@1772
   299
  ComponentList *components = GetComponentList ();
craigdo@1772
   300
  for (ComponentListI i = components->begin ();
craigdo@1772
   301
       i != components->end ();
craigdo@1772
   302
       i++)
craigdo@1772
   303
    {
craigdo@1772
   304
      i->second->Disable (level);
craigdo@1772
   305
    }  
craigdo@1772
   306
}
craigdo@1772
   307
craigdo@1772
   308
void 
craigdo@1498
   309
LogComponentPrintList (void)
craigdo@1498
   310
{
craigdo@1498
   311
  ComponentList *components = GetComponentList ();
craigdo@1498
   312
  for (ComponentListI i = components->begin ();
craigdo@1498
   313
       i != components->end ();
craigdo@1498
   314
       i++)
craigdo@1498
   315
    {
craigdo@1498
   316
      std::cout << i->first << "=";
craigdo@1498
   317
      if (i->second->IsNoneEnabled ())
craigdo@1498
   318
        {
craigdo@1498
   319
          std::cout << "0" << std::endl;
craigdo@1498
   320
          continue;
craigdo@1498
   321
        }
craigdo@1507
   322
      if (i->second->IsEnabled (LOG_ERROR))
craigdo@1503
   323
        {
craigdo@1503
   324
          std::cout << "error";
craigdo@1503
   325
        }
craigdo@1507
   326
      if (i->second->IsEnabled (LOG_WARN))
craigdo@1503
   327
        {
craigdo@1503
   328
          std::cout << "|warn";
craigdo@1503
   329
        }
craigdo@1507
   330
      if (i->second->IsEnabled (LOG_DEBUG))
craigdo@1498
   331
        {
craigdo@1503
   332
          std::cout << "|debug";
craigdo@1503
   333
        }
craigdo@1507
   334
      if (i->second->IsEnabled (LOG_INFO))
craigdo@1503
   335
        {
craigdo@1503
   336
          std::cout << "|info";
craigdo@1498
   337
        }
craigdo@1507
   338
      if (i->second->IsEnabled (LOG_FUNCTION))
craigdo@1498
   339
        {
craigdo@1503
   340
          std::cout << "|function";
craigdo@1498
   341
        }
craigdo@1507
   342
      if (i->second->IsEnabled (LOG_LOGIC))
craigdo@1498
   343
        {
craigdo@1503
   344
          std::cout << "|logic";
craigdo@1498
   345
        }
craigdo@1507
   346
      if (i->second->IsEnabled (LOG_ALL))
craigdo@1498
   347
        {
craigdo@1503
   348
          std::cout << "|all";
craigdo@1498
   349
        }
craigdo@1498
   350
      std::cout << std::endl;
craigdo@1498
   351
    }
craigdo@1498
   352
}
craigdo@1498
   353
mathieu@3005
   354
void LogSetTimePrinter (LogTimePrinter printer)
mathieu@2979
   355
{
mathieu@2979
   356
  g_logTimePrinter = printer;
mathieu@2979
   357
}
mathieu@2979
   358
LogTimePrinter LogGetTimePrinter(void)
mathieu@2979
   359
{
mathieu@2979
   360
  return g_logTimePrinter;
mathieu@2979
   361
}
mathieu@2979
   362
mathieu@2979
   363
mathieu@2979
   364
ParameterLogger::ParameterLogger (std::ostream &os)
mathieu@2979
   365
  : m_itemNumber (0),
mathieu@2979
   366
    m_os (os)
mathieu@2976
   367
{}
tomh@1828
   368
craigdo@1498
   369
} // namespace ns3
craigdo@1498
   370
gjc@3483
   371
#else // NS3_LOG_ENABLE
gjc@3483
   372
gjc@3483
   373
namespace ns3 {
gjc@3483
   374
gjc@3483
   375
void 
gjc@3483
   376
LogComponentEnable (char const *name, enum LogLevel level)
gjc@3483
   377
{
gjc@3483
   378
gjc@3483
   379
}
gjc@3483
   380
gjc@3483
   381
void 
gjc@3483
   382
LogComponentEnableAll (enum LogLevel level)
gjc@3483
   383
{
gjc@3483
   384
gjc@3483
   385
}
gjc@3483
   386
gjc@3483
   387
void 
gjc@3483
   388
LogComponentDisable (char const *name, enum LogLevel level)
gjc@3483
   389
{
gjc@3483
   390
gjc@3483
   391
}
gjc@3483
   392
gjc@3483
   393
void 
gjc@3483
   394
LogComponentDisableAll (enum LogLevel level)
gjc@3483
   395
{
gjc@3483
   396
gjc@3483
   397
}
gjc@3483
   398
gjc@3483
   399
} // namespace ns3
gjc@3483
   400
gjc@3483
   401
#endif