clog.h Source File

API: clog.h Source File
API
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
23namespace omnetpp {
24
25class cObject;
26class cComponent;
27
106
117#ifndef COMPILETIME_LOGLEVEL
118#ifdef NDEBUG
119#define COMPILETIME_LOGLEVEL omnetpp::LOGLEVEL_DETAIL
120#else
121#define COMPILETIME_LOGLEVEL omnetpp::LOGLEVEL_TRACE
122#endif
123#endif
124
125constexpr bool defaultCompiletimeLogPredicate(LogLevel logLevel, LogLevel compiletimeLogLevel)
126{
127 // The COMPILETIME_LOGLEVEL macro is not used directly here,
128 // as that would prevent its redefinitions from taking effect.
129 // Rather, it is taken as a parameter, so COMPILETIME_LOGLEVEL
130 // is substituted at each call site with its current value.
131 return logLevel >= compiletimeLogLevel;
132}
133
143#ifndef COMPILETIME_LOG_PREDICATE
144#define COMPILETIME_LOG_PREDICATE(object, logLevel, category) \
145 (::omnetpp::defaultCompiletimeLogPredicate(logLevel, COMPILETIME_LOGLEVEL))
146#endif
147
154class SIM_API cLog
155{
156 public:
157 typedef bool (*NoncomponentLogPredicate)(const void *object, LogLevel logLevel, const char *category);
158 typedef bool (*ComponentLogPredicate)(const cComponent *object, LogLevel logLevel, const char *category);
159
160 public:
167
173 static NoncomponentLogPredicate noncomponentLogPredicate;
174
180 static ComponentLogPredicate componentLogPredicate;
181
182 public:
186 static const char *getLogLevelName(LogLevel logLevel);
187
191 static LogLevel resolveLogLevel(const char *name);
192
193 static inline bool runtimeLogPredicate(const void *object, LogLevel logLevel, const char *category)
194 { return noncomponentLogPredicate(object, logLevel, category); }
195
196 static inline bool runtimeLogPredicate(const cComponent *object, LogLevel logLevel, const char *category)
197 { return componentLogPredicate(object, logLevel, category); }
198
199 static bool defaultNoncomponentLogPredicate(const void *object, LogLevel logLevel, const char *category);
200 static bool defaultComponentLogPredicate(const cComponent *object, LogLevel logLevel, const char *category);
201};
202
203// Creates a log proxy object that captures the provided context.
204// This macro is internal to the logging infrastructure.
205//
206// NOTE: the (void)0 trick prevents GCC producing statement has no effect warnings
207// for compile time disabled log statements.
208//
209#define OPP_LOGPROXY(object, logLevel, category) \
210 ((void)0, !(COMPILETIME_LOG_PREDICATE(object, logLevel, category) && \
211 omnetpp::cLog::runtimeLogPredicate(object, logLevel, category))) ? \
212 omnetpp::internal::cLogProxy::dummyStream : omnetpp::internal::cLogProxy(object, logLevel, category, __FILE__, __LINE__, __FUNCTION__)
213
214// Returns nullptr. Helper function for the logging macros.
215inline void *getThisPtr() {return nullptr;}
216
230#define EV_STATICCONTEXT void *(*getThisPtr)() = omnetpp::getThisPtr;
231
271#define EV_LOG(logLevel, category) OPP_LOGPROXY(getThisPtr(), logLevel, category).getStream()
272
277#define EV EV_INFO
278
283#define EV_FATAL EV_LOG(omnetpp::LOGLEVEL_FATAL, nullptr)
284
289#define EV_ERROR EV_LOG(omnetpp::LOGLEVEL_ERROR, nullptr)
290
295#define EV_WARN EV_LOG(omnetpp::LOGLEVEL_WARN, nullptr)
296
301#define EV_INFO EV_LOG(omnetpp::LOGLEVEL_INFO, nullptr)
302
307#define EV_DETAIL EV_LOG(omnetpp::LOGLEVEL_DETAIL, nullptr)
308
313#define EV_DEBUG EV_LOG(omnetpp::LOGLEVEL_DEBUG, nullptr)
314
319#define EV_TRACE EV_LOG(omnetpp::LOGLEVEL_TRACE, nullptr)
320
325#define EV_C(category) EV_INFO_C(category)
326
331#define EV_FATAL_C(category) EV_LOG(omnetpp::LOGLEVEL_FATAL, category)
332
337#define EV_ERROR_C(category) EV_LOG(omnetpp::LOGLEVEL_ERROR, category)
338
343#define EV_WARN_C(category) EV_LOG(omnetpp::LOGLEVEL_WARN, category)
344
349#define EV_INFO_C(category) EV_LOG(omnetpp::LOGLEVEL_INFO, category)
350
355#define EV_DETAIL_C(category) EV_LOG(omnetpp::LOGLEVEL_DETAIL, category)
356
361#define EV_DEBUG_C(category) EV_LOG(omnetpp::LOGLEVEL_DEBUG, category)
362
367#define EV_TRACE_C(category) EV_LOG(omnetpp::LOGLEVEL_TRACE, category)
368
376class SIM_API cLogEntry
377{
378 public:
379 // log statement related
380 LogLevel logLevel;
381 const char *category;
382
383 // C++ source related (where the log statement appears)
384 const void *sourcePointer;
385 const cObject *sourceObject;
386 const cComponent *sourceComponent;
387 const char *sourceFile;
388 int sourceLine;
389 const char *sourceFunction;
390
391 // operating system related
392 clock_t userTime;
393
394 // the actual text of the log statement
395 const char *text;
396 int textLength;
397};
398
399 namespace internal {
400
401//
402// This class captures the context where the log statement appears.
403// NOTE: This class is internal to the logging infrastructure.
404//
405class SIM_API cLogProxy
406{
407 private:
408 // This class is used for buffering the text content to be able to send whole
409 // lines one by one to the active environment.
410 class LogBuffer : public std::basic_stringbuf<char> {
411 public:
412 LogBuffer() {}
413 bool isEmpty() { return pptr() == pbase(); }
414 protected:
415 virtual int sync() override; // invokes getEnvir()->log() for each log line
416 };
417
418 // act likes /dev/null
419 class nullstream : public std::ostream {
420 public:
421 nullstream() : std::ostream(nullptr) {} // results in rdbuf==0 and badbit==true
422 };
423
424 public:
425 static nullstream dummyStream; // EV evaluates to this when in express mode (getEnvir()->disabled())
426
427 private:
428 static LogBuffer buffer; // underlying buffer that contains the text that has been written so far
429 static std::ostream stream; // this singleton is used to avoid allocating a new stream each time a log statement executes
430 static cLogEntry currentEntry; // context of the current (last) log statement that has been executed.
431 static LogLevel previousLogLevel; // log level of the previous log statement
432 static const char *previousCategory; // category of the previous log statement
433
434 private:
435 void fillEntry(LogLevel logLevel, const char *category, const char *sourceFile, int sourceLine, const char *sourceFunction);
436
437 public:
438 cLogProxy(const void *sourcePointer, LogLevel logLevel, const char *category, const char *sourceFile, int sourceLine, const char *sourceFunction);
439 cLogProxy(const cObject *sourceObject, LogLevel logLevel, const char *category, const char *sourceFile, int sourceLine, const char *sourceFunction);
440 cLogProxy(const cComponent *sourceComponent, LogLevel logLevel, const char *category, const char *sourceFile, int sourceLine, const char *sourceFunction);
441 ~cLogProxy();
442
443 std::ostream& getStream() { return stream; }
444 static void flushLastLine();
445};
446
447} // namespace internal
448
449} // namespace omnetpp
450
451#endif
Common base for module and channel classes.
Definition ccomponent.h:51
This class holds various data that is captured when a particular log statement executes....
Definition clog.h:377
This class groups logging related functionality.
Definition clog.h:155
static ComponentLogPredicate componentLogPredicate
Definition clog.h:180
static LogLevel logLevel
Definition clog.h:166
static LogLevel resolveLogLevel(const char *name)
static NoncomponentLogPredicate noncomponentLogPredicate
Definition clog.h:173
static const char * getLogLevelName(LogLevel logLevel)
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition cobject.h:93
LogLevel
Classifies log messages based on detail and importance.
Definition clog.h:34
@ LOGLEVEL_WARN
Definition clog.h:76
@ LOGLEVEL_DETAIL
Definition clog.h:61
@ LOGLEVEL_NOTSET
Definition clog.h:104
@ LOGLEVEL_FATAL
Definition clog.h:94
@ LOGLEVEL_ERROR
Definition clog.h:84
@ LOGLEVEL_TRACE
Definition clog.h:42
@ LOGLEVEL_DEBUG
Definition clog.h:51
@ LOGLEVEL_OFF
Definition clog.h:99
@ LOGLEVEL_INFO
Definition clog.h:69