00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __LOG_H__
00022 #define __LOG_H__
00023
00024 #include <string>
00025 #include <iostream>
00026
00027 namespace ns3 {
00028
00029 enum LogLevel {
00030 LOG_NONE = 0x00000000,
00031
00032 LOG_ERROR = 0x00000001,
00033 LOG_LEVEL_ERROR = 0x00000001,
00034
00035 LOG_WARN = 0x00000002,
00036 LOG_LEVEL_WARN = 0x00000003,
00037
00038 LOG_DEBUG = 0x00000004,
00039 LOG_LEVEL_DEBUG = 0x00000007,
00040
00041 LOG_INFO = 0x00000008,
00042 LOG_LEVEL_INFO = 0x0000000f,
00043
00044 LOG_FUNCTION = 0x00000010,
00045 LOG_LEVEL_FUNCTION = 0x0000001f,
00046
00047 LOG_LOGIC = 0x00000020,
00048 LOG_LEVEL_LOGIC = 0x0000003f,
00049
00050 LOG_ALL = 0x3fffffff,
00051 LOG_LEVEL_ALL = LOG_ALL,
00052
00053 LOG_PREFIX_FUNC = 0x80000000,
00054 LOG_PREFIX_TIME = 0x40000000
00055 };
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 void LogComponentEnable (char const *name, enum LogLevel level);
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 void LogComponentEnableAll (enum LogLevel level);
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 void LogComponentDisable (char const *name, enum LogLevel level);
00093
00094
00095
00096
00097
00098
00099
00100 void LogComponentDisableAll (enum LogLevel level);
00101
00102
00103 }
00104
00105
00106 #ifdef NS3_LOG_ENABLE
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 #define NS_LOG_COMPONENT_DEFINE(name) \
00149 static ns3::LogComponent g_log = ns3::LogComponent (name)
00150
00151 #define NS_LOG_APPEND_TIME_PREFIX \
00152 if (g_log.IsEnabled (ns3::LOG_PREFIX_TIME)) \
00153 { \
00154 ns3::LogTimePrinter printer = ns3::LogGetTimePrinter (); \
00155 if (printer != 0) \
00156 { \
00157 (*printer) (std::clog); \
00158 std::clog << " "; \
00159 } \
00160 }
00161
00162 #define NS_LOG_APPEND_FUNC_PREFIX \
00163 if (g_log.IsEnabled (ns3::LOG_PREFIX_FUNC)) \
00164 { \
00165 std::clog << g_log.Name () << ":" << \
00166 __FUNCTION__ << "(): "; \
00167 } \
00168
00169 #ifndef NS_LOG_APPEND_CONTEXT
00170 #define NS_LOG_APPEND_CONTEXT
00171 #endif
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 #define NS_LOG(level, msg) \
00189 do \
00190 { \
00191 if (g_log.IsEnabled (level)) \
00192 { \
00193 NS_LOG_APPEND_TIME_PREFIX; \
00194 NS_LOG_APPEND_CONTEXT; \
00195 NS_LOG_APPEND_FUNC_PREFIX; \
00196 std::clog << msg << std::endl; \
00197 } \
00198 } \
00199 while (false)
00200
00201
00202
00203
00204
00205
00206
00207 #define NS_LOG_ERROR(msg) \
00208 NS_LOG(ns3::LOG_ERROR, msg)
00209
00210
00211
00212
00213
00214
00215
00216 #define NS_LOG_WARN(msg) \
00217 NS_LOG(ns3::LOG_WARN, msg)
00218
00219
00220
00221
00222
00223
00224
00225 #define NS_LOG_DEBUG(msg) \
00226 NS_LOG(ns3::LOG_DEBUG, msg)
00227
00228
00229
00230
00231
00232
00233
00234 #define NS_LOG_INFO(msg) \
00235 NS_LOG(ns3::LOG_INFO, msg)
00236
00237
00238
00239
00240
00241
00242 #define NS_LOG_FUNCTION_NOARGS() \
00243 do \
00244 { \
00245 if (g_log.IsEnabled (ns3::LOG_FUNCTION)) \
00246 { \
00247 NS_LOG_APPEND_TIME_PREFIX; \
00248 NS_LOG_APPEND_CONTEXT; \
00249 std::clog << g_log.Name () << ":" \
00250 << __FUNCTION__ << "()" << std::endl; \
00251 } \
00252 } \
00253 while (false)
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 #define NS_LOG_FUNCTION(parameters) \
00273 do \
00274 { \
00275 if (g_log.IsEnabled (ns3::LOG_FUNCTION)) \
00276 { \
00277 NS_LOG_APPEND_TIME_PREFIX; \
00278 NS_LOG_APPEND_CONTEXT; \
00279 std::clog << g_log.Name () << ":" \
00280 << __FUNCTION__ << "("; \
00281 ns3::ParameterLogger (std::clog) << parameters; \
00282 std::clog << ")" << std::endl; \
00283 } \
00284 } \
00285 while (false)
00286
00287
00288
00289
00290
00291
00292
00293
00294 #define NS_LOG_LOGIC(msg) \
00295 NS_LOG(ns3::LOG_LOGIC, msg)
00296
00297
00298
00299
00300
00301
00302
00303 #define NS_LOG_UNCOND(msg) \
00304 do \
00305 { \
00306 std::clog << msg << std::endl; \
00307 } \
00308 while (false)
00309
00310 namespace ns3 {
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 void LogComponentPrintList (void);
00321
00322 typedef void (*LogTimePrinter) (std::ostream &os);
00323
00324 void LogSetTimePrinter (LogTimePrinter);
00325 LogTimePrinter LogGetTimePrinter(void);
00326
00327
00328 class LogComponent {
00329 public:
00330 LogComponent (char const *name);
00331 void EnvVarCheck (char const *name);
00332 bool IsEnabled (enum LogLevel level) const;
00333 bool IsNoneEnabled (void) const;
00334 void Enable (enum LogLevel level);
00335 void Disable (enum LogLevel level);
00336 char const *Name (void) const;
00337 private:
00338 int32_t m_levels;
00339 char const *m_name;
00340 };
00341
00342 class ParameterLogger : public std::ostream
00343 {
00344 int m_itemNumber;
00345 std::ostream &m_os;
00346 public:
00347 ParameterLogger (std::ostream &os);
00348
00349 template<typename T>
00350 ParameterLogger& operator<< (T param)
00351 {
00352 switch (m_itemNumber)
00353 {
00354 case 0:
00355 m_os << param;
00356 break;
00357 default:
00358 m_os << ", " << param;
00359 break;
00360 }
00361 m_itemNumber++;
00362 return *this;
00363 }
00364 };
00365
00366 }
00367
00368 #else
00369
00370 #define NS_LOG_COMPONENT_DEFINE(component)
00371 #define NS_LOG(level, msg)
00372 #define NS_LOG_ERROR(msg)
00373 #define NS_LOG_WARN(msg)
00374 #define NS_LOG_DEBUG(msg)
00375 #define NS_LOG_INFO(msg)
00376 #define NS_LOG_FUNCTION_NOARGS()
00377 #define NS_LOG_FUNCTION(msg)
00378 #define NS_LOG_LOGIC(msg)
00379 #define NS_LOG_UNCOND(msg)
00380
00381 #define LogComponentPrintList
00382 #define LogRegisterTimePrinter(printer)
00383
00384 #define LogSetTimePrinter(printer)
00385 #define LogGetTimePrinter
00386
00387 #endif
00388
00389 #endif // __LOG_H__