/** * Logger interface * $Id$ * $HeadURL$ * $Author$ */ #pragma once #include #include #include #include #include #include #include #include #include "ngllogger_export.h" namespace NGL { class INGLLogger; // keep in sync with log4qt/level.h enum class LogLevel { Undefined = 0, Trace = 64, Debug = 96, Info = 128, Warning = 150, Error = 182, Fatal = 214, }; class LogStreamHelper { public: inline LogStreamHelper(INGLLogger &logger, LogLevel level, const char *const file, int line, const char *const function) : m_logger(logger) , m_level(level) , m_line(line) , m_file(file) , m_func(function) {} inline ~LogStreamHelper(); // after INGLLogger class definition template inline LogStreamHelper &operator<<(const T &val); private: std::ostringstream m_buffer; NGL::INGLLogger &m_logger; const LogLevel m_level; const int m_line; const char *const m_file; const char *const m_func; }; // Interface class for source code logging. // Use NGLLoggerBootstrap to create an instance of this interface. // Look below in this headerfile for the macros with which to insert logging // code into your source code. // Lookup the log4cxx documentation (http://logging.apache.org/log4cxx/) for // information on logger-hierarchies, logging-levels, and configuration files. class INGLLogger { public: INGLLogger(const INGLLogger &) = delete; INGLLogger &operator=(const INGLLogger &) = delete; INGLLogger() = default; virtual ~INGLLogger() = default; using String = std::string; // Creates an instance of an INGLLogger interface. The fullqualifiedname is // a dottet sequence (i.e. "NGL.thisandthat.mylogger"). The same name will // always return the same interface-instance. It is not required to create // the parent nodes. virtual INGLLogger &getNGLLogger(const char *fullqualifiedname) = 0; virtual INGLLogger &getNGLLogger(const std::string &fullqualifiedname) = 0; // True if this level is enabled. virtual bool isTraceEnabled() const = 0; virtual bool isDebugEnabled() const = 0; virtual bool isInfoEnabled() const = 0; virtual bool isWarningEnabled() const = 0; virtual bool isErrorEnabled() const = 0; virtual bool isFatalEnabled() const = 0; virtual bool isLevelEnabled(LogLevel level) const = 0; // Submit a LOG message. // Use __FILE__ and __NAME__ macros. virtual void log(LogLevel level, const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0; // Submit an TRACE message. virtual void logTrace(const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0; // Submit an DEBUG message. virtual void logDebug(const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0; // Submit an INFO message. virtual void logInfo(const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0; // Submit a WARNING message. virtual void logWarning(const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0; // Submit an ERROR message. virtual void logError(const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0; // Submit a FATAL message. virtual void logFatal(const INGLLogger::String &message, const char *file = nullptr, int line = -1, const char *function = nullptr) = 0; // Writes a startup message into the logfile virtual void logStartup(const char *file, int line, const char *function = nullptr) = 0; // Writes a shutdown message into the logfile virtual void logShutdown(const char *file, int line, const char *function = nullptr) = 0; // Get this loggers fullqualified name. virtual std::string getLoggerName() const = 0; virtual void setNoSpace(bool bNoSpace) = 0; virtual bool noSpace() const = 0; }; // // LogStreamHelper // inline LogStreamHelper::~LogStreamHelper() { m_logger.log(m_level, m_buffer.str(), m_file, m_line, m_func); } template inline LogStreamHelper &LogStreamHelper::operator<<(const T &val) { m_buffer << val; if (!m_logger.noSpace()) m_buffer << " "; return *this; } } //namespace NGL inline std::ostream &operator<<(std::ostream &stream, const QByteArray &str) { stream << std::string(str.data(), str.size()); return stream; } // // operator to ease logging (at least via NglLogging) // inline std::ostream &operator<<(std::ostream &stream, const QPointF &value) { stream << "QPointF(" << QString::number(value.x(), 'f', 6).toStdString() << "#" << QString::number(value.y(), 'f', 6).toStdString() << ")"; return stream; } //---------------------------------------------------------------------------- inline std::ostream &operator<< (std::ostream &stream, const QPoint &value) { stream << "QPoint(" << value.x() << "#" << value.y() << ")"; return stream; } inline std::ostream &operator<< (std::ostream &stream, const QSize &value) { stream << "QSize(w=" << value.width() << ";h=" << value.height() << ")"; return stream; } inline std::ostream &operator<< (std::ostream &stream, const QSizeF &value) { stream << "QSize(w=" << QString::number(value.width(), 'f', 6).toStdString() << ";h=" << QString::number(value.height(), 'f', 6).toStdString() << ")"; return stream; } inline std::ostream &operator<< (std::ostream &stream, const QRect &value) { stream << "QRect(center=" << value.center() << " w=" << value.width() << ";h=" << value.height() << " bottomLeft=" << value.bottomLeft() << " topRight=" << value.topRight() << ")"; return stream; } inline std::ostream &operator<< (std::ostream &stream, const QRectF &value) { stream << "QRectF(center=" << value.center() << " w=" << QString::number(value.width(), 'f', 3).toStdString() << ";h=" << QString::number(value.height(), 'f', 3).toStdString() << " bottomLeft=" << value.bottomLeft() << " topRight=" << value.topRight() << ")"; return stream; } inline std::ostream &operator<< (std::ostream &stream, const QList &value) { stream << "QList(" << value.size() << "): "; for (int i : value) stream << i << ";"; return stream; } inline std::ostream &operator<< (std::ostream &stream, const QList &value) { stream << "QList(" << value.size() << "): "; for (const QPointF &item : value) stream << item << ";"; return stream; } inline std::ostream &operator<<(std::ostream &stream, const QString &str) { return stream << str.toUtf8(); } namespace NGL { namespace NGLLoggerHelper { NGLLOGGER_EXPORT void hexDumpHelper(NGL::INGLLogger &logger, NGL::LogLevel level, const void *a_data, const unsigned int a_size); inline void hexDumpHelper(NGL::INGLLogger &logger, NGL::LogLevel level, const QByteArray &data) { return hexDumpHelper(logger, level, data.data(), data.size()); } } // namespace NGLLoggerHelper NGLLOGGER_EXPORT NGL::INGLLogger& getMemoryLogger(); NGLLOGGER_EXPORT std::string getMemoryData(); } // namespace NGL //============================================================================== // macros //============================================================================== #define LOGFUNCTION Q_FUNC_INFO // log this output every time, no matter what log level is enabled #define NOTICE_LOG(logger) \ NGL::LogStreamHelper(logger, NGL::LogLevel::Info, __FILE__,__LINE__, Q_FUNC_INFO) #ifdef LTS_DEBUG #define TRACE_LOG(logger) \ for (bool log_level_enabled = (logger).isTraceEnabled(); log_level_enabled; log_level_enabled = false) \ NGL::LogStreamHelper(logger, NGL::LogLevel::Trace, __FILE__,__LINE__, Q_FUNC_INFO) #else #define TRACE_LOG(logger) \ for ( ; false; ) \ NGL::LogStreamHelper(logger, NGL::LogLevel::Trace, __FILE__,__LINE__, Q_FUNC_INFO) #endif #define DEBUG_LOG(logger) \ for (bool log_level_enabled = (logger).isDebugEnabled(); log_level_enabled; log_level_enabled = false) \ NGL::LogStreamHelper(logger, NGL::LogLevel::Debug, __FILE__,__LINE__, Q_FUNC_INFO) #define INFO_LOG(logger) \ for (bool log_level_enabled = (logger).isInfoEnabled(); log_level_enabled; log_level_enabled = false) \ NGL::LogStreamHelper(logger, NGL::LogLevel::Info, __FILE__,__LINE__, Q_FUNC_INFO) #define WARNING_LOG(logger) \ for (bool log_level_enabled = (logger).isWarningEnabled(); log_level_enabled; log_level_enabled = false) \ NGL::LogStreamHelper(logger, NGL::LogLevel::Warning, __FILE__,__LINE__, Q_FUNC_INFO) #define ERROR_LOG(logger) \ for (bool log_level_enabled = (logger).isErrorEnabled(); log_level_enabled; log_level_enabled = false) \ NGL::LogStreamHelper(logger, NGL::LogLevel::Error, __FILE__,__LINE__, Q_FUNC_INFO) #define FATAL_LOG(logger) \ for (bool log_level_enabled = (logger).isFatalEnabled(); log_level_enabled; log_level_enabled = false) \ NGL::LogStreamHelper(logger, NGL::LogLevel::Fatal, __FILE__,__LINE__, Q_FUNC_INFO) #define STARTUP_LOG(logger) { (logger).logStartup(__FILE__, __LINE__, Q_FUNC_INFO); } #define SHUTDOWN_LOG(logger) { (logger).logShutdown(__FILE__, __LINE__, Q_FUNC_INFO); } //============================================================================== // use like: // MEMORY_LOG() << " before something nasty"; // do something nasty // MEMORY_LOG() << " after something nasty"; #define MEMORY_LOG() \ for (bool log_level_enabled = (NGL::getMemoryLogger()).isInfoEnabled(); log_level_enabled; log_level_enabled = false) \ NGL::LogStreamHelper(NGL::getMemoryLogger(), NGL::LogLevel::Info, __FILE__,__LINE__, Q_FUNC_INFO) << NGL::getMemoryData() //==============================================================================