OMNeT++ Simulation Library  5.6.1
clog.h
1 //==========================================================================
2 // CLOG.H - header for
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CLOG_H
17 #define __OMNETPP_CLOG_H
18 
19 #include <ctime>
20 #include <sstream>
21 #include "simkerneldefs.h"
22 
23 namespace omnetpp {
24 
25 class cObject;
26 class cComponent;
27 
34 {
43 
52 
62 
70 
77 
85 
95 
100 };
101 
112 #ifndef COMPILETIME_LOGLEVEL
113 #ifdef NDEBUG
114 #define COMPILETIME_LOGLEVEL omnetpp::LOGLEVEL_DETAIL
115 #else
116 #define COMPILETIME_LOGLEVEL omnetpp::LOGLEVEL_TRACE
117 #endif
118 #endif
119 
120 constexpr bool defaultCompiletimeLogPredicate(LogLevel logLevel, LogLevel compiletimeLogLevel)
121 {
122  // The COMPILETIME_LOGLEVEL macro is not used directly here,
123  // as that would prevent its redefinitions from taking effect.
124  // Rather, it is taken as a parameter, so COMPILETIME_LOGLEVEL
125  // is substituted at each call site with its current value.
126  return logLevel >= compiletimeLogLevel;
127 }
128 
138 #ifndef COMPILETIME_LOG_PREDICATE
139 #define COMPILETIME_LOG_PREDICATE(object, logLevel, category) \
140  (::omnetpp::defaultCompiletimeLogPredicate(logLevel, COMPILETIME_LOGLEVEL))
141 #endif
142 
149 class SIM_API cLog
150 {
151  public:
152  typedef bool (*NoncomponentLogPredicate)(const void *object, LogLevel logLevel, const char *category);
153  typedef bool (*ComponentLogPredicate)(const cComponent *object, LogLevel logLevel, const char *category);
154 
155  public:
162 
168  static NoncomponentLogPredicate noncomponentLogPredicate;
169 
175  static ComponentLogPredicate componentLogPredicate;
176 
177  public:
181  static const char *getLogLevelName(LogLevel logLevel);
182 
186  static LogLevel resolveLogLevel(const char *name);
187 
188  static inline bool runtimeLogPredicate(const void *object, LogLevel logLevel, const char *category)
189  { return noncomponentLogPredicate(object, logLevel, category); }
190 
191  static inline bool runtimeLogPredicate(const cComponent *object, LogLevel logLevel, const char *category)
192  { return componentLogPredicate(object, logLevel, category); }
193 
194  static bool defaultNoncomponentLogPredicate(const void *object, LogLevel logLevel, const char *category);
195  static bool defaultComponentLogPredicate(const cComponent *object, LogLevel logLevel, const char *category);
196 };
197 
198 // Creates a log proxy object that captures the provided context.
199 // This macro is internal to the logging infrastructure.
200 //
201 // NOTE: the (void)0 trick prevents GCC producing statement has no effect warnings
202 // for compile time disabled log statements.
203 //
204 #define OPP_LOGPROXY(object, logLevel, category) \
205  ((void)0, !(COMPILETIME_LOG_PREDICATE(object, logLevel, category) && \
206  omnetpp::cLog::runtimeLogPredicate(object, logLevel, category))) ? \
207  omnetpp::cLogProxy::dummyStream : omnetpp::cLogProxy(object, logLevel, category, __FILE__, __LINE__, __FUNCTION__)
208 
209 // Returns nullptr. Helper function for the logging macros.
210 inline void *getThisPtr() {return nullptr;}
211 
225 #define EV_STATICCONTEXT void *(*getThisPtr)() = omnetpp::getThisPtr;
226 
264 #define EV_LOG(logLevel, category) OPP_LOGPROXY(getThisPtr(), logLevel, category).getStream()
265 
270 #define EV EV_INFO
271 
276 #define EV_FATAL EV_LOG(omnetpp::LOGLEVEL_FATAL, nullptr)
277 
282 #define EV_ERROR EV_LOG(omnetpp::LOGLEVEL_ERROR, nullptr)
283 
288 #define EV_WARN EV_LOG(omnetpp::LOGLEVEL_WARN, nullptr)
289 
294 #define EV_INFO EV_LOG(omnetpp::LOGLEVEL_INFO, nullptr)
295 
300 #define EV_DETAIL EV_LOG(omnetpp::LOGLEVEL_DETAIL, nullptr)
301 
306 #define EV_DEBUG EV_LOG(omnetpp::LOGLEVEL_DEBUG, nullptr)
307 
312 #define EV_TRACE EV_LOG(omnetpp::LOGLEVEL_TRACE, nullptr)
313 
318 #define EV_C(category) EV_INFO_C(category)
319 
324 #define EV_FATAL_C(category) EV_LOG(omnetpp::LOGLEVEL_FATAL, category)
325 
330 #define EV_ERROR_C(category) EV_LOG(omnetpp::LOGLEVEL_ERROR, category)
331 
336 #define EV_WARN_C(category) EV_LOG(omnetpp::LOGLEVEL_WARN, category)
337 
342 #define EV_INFO_C(category) EV_LOG(omnetpp::LOGLEVEL_INFO, category)
343 
348 #define EV_DETAIL_C(category) EV_LOG(omnetpp::LOGLEVEL_DETAIL, category)
349 
354 #define EV_DEBUG_C(category) EV_LOG(omnetpp::LOGLEVEL_DEBUG, category)
355 
360 #define EV_TRACE_C(category) EV_LOG(omnetpp::LOGLEVEL_TRACE, category)
361 
369 class SIM_API cLogEntry
370 {
371  public:
372  // log statement related
373  LogLevel logLevel;
374  const char *category;
375 
376  // C++ source related (where the log statement appears)
377  const void *sourcePointer;
378  const cObject *sourceObject;
379  const cComponent *sourceComponent;
380  const char *sourceFile;
381  int sourceLine;
382  const char *sourceFunction;
383 
384  // operating system related
385  clock_t userTime;
386 
387  // the actual text of the log statement
388  const char *text;
389  int textLength;
390 };
391 
392 
393 //
394 // This class captures the context where the log statement appears.
395 // NOTE: This class is internal to the logging infrastructure.
396 //
397 class SIM_API cLogProxy
398 {
399  private:
400  // This class is used for buffering the text content to be able to send whole
401  // lines one by one to the active environment.
402  class LogBuffer : public std::basic_stringbuf<char> {
403  public:
404  LogBuffer() { }
405  bool isEmpty() { return pptr() == pbase(); }
406  protected:
407  virtual int sync() override; // invokes getEnvir()->log() for each log line
408  };
409 
410  // act likes /dev/null
411  class nullstream : public std::ostream {
412  public:
413  nullstream() : std::ostream(nullptr) {} // results in rdbuf==0 and badbit==true
414  };
415 
416  public:
417  static nullstream dummyStream; // EV evaluates to this when in express mode (getEnvir()->disabled())
418 
419  private:
420  static LogBuffer buffer; // underlying buffer that contains the text that has been written so far
421  static std::ostream stream; // this singleton is used to avoid allocating a new stream each time a log statement executes
422  static cLogEntry currentEntry; // context of the current (last) log statement that has been executed.
423  static LogLevel previousLogLevel; // log level of the previous log statement
424  static const char *previousCategory; // category of the previous log statement
425 
426  private:
427  void fillEntry(LogLevel logLevel, const char *category, const char *sourceFile, int sourceLine, const char *sourceFunction);
428 
429  public:
430  cLogProxy(const void *sourcePointer, LogLevel logLevel, const char *category, const char *sourceFile, int sourceLine, const char *sourceFunction);
431  cLogProxy(const cObject *sourceObject, LogLevel logLevel, const char *category, const char *sourceFile, int sourceLine, const char *sourceFunction);
432  cLogProxy(const cComponent *sourceComponent, LogLevel logLevel, const char *category, const char *sourceFile, int sourceLine, const char *sourceFunction);
433  ~cLogProxy();
434 
435  std::ostream& getStream() { return stream; }
436  static void flushLastLine();
437 };
438 
439 } // namespace omnetpp
440 
441 #endif
Definition: clog.h:51
Common base for module and channel classes.
Definition: ccomponent.h:48
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
Definition: clog.h:69
static NoncomponentLogPredicate noncomponentLogPredicate
Definition: clog.h:168
static LogLevel logLevel
Definition: clog.h:161
LogLevel
Classifies log messages based on detail and importance.
Definition: clog.h:33
Definition: clog.h:42
This class holds various data that is captured when a particular log statement executes. It also contains the text written to the log stream.
Definition: clog.h:369
Definition: clog.h:94
This class groups logging related functionality.
Definition: clog.h:149
Definition: clog.h:61
static ComponentLogPredicate componentLogPredicate
Definition: clog.h:175
Definition: cabstracthistogram.h:21
Definition: clog.h:76
Definition: clog.h:84
Definition: clog.h:99