Skip to content

Commit

Permalink
Fix assert macro redifine
Browse files Browse the repository at this point in the history
  • Loading branch information
shuyuan1 committed Oct 6, 2018
1 parent a28cd14 commit 5871c5a
Showing 1 changed file with 76 additions and 41 deletions.
117 changes: 76 additions & 41 deletions src/qmllogging.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// QmlLogging v1.3
// Single-header only, Easylogging++ wrapper for advanced
// Single-header only, Easylogging++ wrapper for advanced
// logging support for QML applications
//
// Requires:
Expand Down Expand Up @@ -40,13 +40,15 @@
#endif // !defined(_QMLLOGGING_AVOID_QDEBUG)

// NOTE: Include easylogging++ according to your configurations
#include <easylogging++.h>
#include "../../easyloggingpp/src/easylogging++.h"

namespace el {
namespace qml {
#define LogStrT QString
#define LogT const LogStrT&

#undef assert

#define FUNCTION_DEFINER(LEVEL, FN_NAME)\
Q_INVOKABLE void FN_NAME(LogT t, LogT t2 = LogStrT(), LogT t3 = LogStrT(), LogT t4 = LogStrT(),\
LogT t5 = LogStrT(), LogT t6 = LogStrT(), LogT t7 = LogStrT(), LogT t8 = LogStrT(),\
Expand All @@ -57,42 +59,61 @@ namespace qml {
LogT t5 = LogStrT(), LogT t6 = LogStrT(), LogT t7 = LogStrT(), LogT t8 = LogStrT(),\
LogT t9 = LogStrT(), LogT t10 = LogStrT(), LogT t11 = LogStrT(), LogT t12 = LogStrT()) {\
if (!m_hasError) { CVLOG(vlevel, m_loggerId.c_str()) << t << t2 << t3 << t4 << t5 << t6 << t7 << t8 << t9 << t10 << t11 << t12;}}

class VersionInfo : el::base::StaticClass {

class VersionInfo : el::base::StaticClass
{
public:
static inline int getMajor() { return version()[0].digitValue(); }
static inline int getMinor() { return version()[2].digitValue(); }
static inline const QString version(void) { return QString("1.3"); }
static inline const QString releaseDate(void) { return QString("13-04-2014 2144hrs"); }
static inline int getMajor()
{
return version()[0].digitValue();
}
static inline int getMinor()
{
return version()[2].digitValue();
}
static inline const QString version(void)
{
return QString("1.3");
}
static inline const QString releaseDate(void)
{
return QString("13-04-2014 2144hrs");
}
};

class TimeTracker : el::base::NoCopy {
class TimeTracker : el::base::NoCopy
{
public:
typedef el::base::PerformanceTracker Tracker;
typedef QHash<QString, QSharedPointer<Tracker>> HashMap;

virtual ~TimeTracker(void) {

virtual ~TimeTracker(void)
{
m_timedBlocks.clear();
}

void setLoggerId(const std::string& loggerId) {

void setLoggerId(const std::string &loggerId)
{
m_loggerId = loggerId;
}

void timeBegin(const HashMap::key_type& blockName) {

void timeBegin(const HashMap::key_type &blockName)
{
if (m_loggerId.empty()) {
ELPP_INTERNAL_ERROR("Set loggerID first!", false);
return;
}
m_timedBlocks.insert(blockName,
QSharedPointer<Tracker>(new Tracker(blockName.toStdString(), _ELPP_MIN_UNIT, m_loggerId)));
m_timedBlocks.insert(blockName,
QSharedPointer<Tracker>(new Tracker(blockName.toStdString(), _ELPP_MIN_UNIT, m_loggerId)));
}
void timeEnd(const HashMap::key_type& blockName) {
void timeEnd(const HashMap::key_type &blockName)
{
if (m_timedBlocks.contains(blockName)) {
m_timedBlocks.remove(blockName);
}
}
void timeCheck(const HashMap::key_type& blockName, QString checkpointId = QString()) {
void timeCheck(const HashMap::key_type &blockName, QString checkpointId = QString())
{
HashMap::iterator iterator = m_timedBlocks.find(blockName);
if (iterator != m_timedBlocks.end()) {
(*iterator)->checkpoint(checkpointId.toStdString().c_str());
Expand All @@ -112,33 +133,41 @@ class QmlLogging : public QQuickItem
{
Q_OBJECT
public:
static void registerNew(QQmlContext* rootContext, const char* loggerId = "qml") {
static void registerNew(QQmlContext *rootContext, const char *loggerId = "qml")
{
qml::s_defaultLoggerId = std::string(loggerId);
qml::s_qmlLogging = QSharedPointer<QmlLogging>(new QmlLogging);
qmlRegisterType<QmlLogging>("org.easylogging.qml",
qml::VersionInfo::getMajor(),
qml::VersionInfo::getMajor(),
qml::VersionInfo::getMinor(),
"QmlLogging");
if (rootContext != nullptr) {
rootContext->setContextProperty("log",
static_cast<QObject*>(qml::s_qmlLogging.data()));
static_cast<QObject *>(qml::s_qmlLogging.data()));
}
}

QmlLogging(QQuickItem *parent = 0) : QQuickItem(parent),
m_hasError(false), m_errorString(QString()) {
m_hasError(false), m_errorString(QString())
{
el::Loggers::addFlag(LoggingFlag::DisableApplicationAbortOnFatalLog);
setObjectName("QmlLogging");
el::Loggers::getLogger("QmlLogging");
CLOG_AFTER_N(1, WARNING, "QmlLogging")
<< "Multiple instances of QmlLogging registered";
CLOG_AFTER_N(1, WARNING, "QmlLogging")
<< "Multiple instances of QmlLogging registered";
m_loggerId = qml::s_defaultLoggerId;
el::Loggers::getLogger(m_loggerId);
m_tracker.setLoggerId(m_loggerId);
}

bool hasError(void) const { return m_hasError; }
QString errorString(void) const { return m_errorString; }
bool hasError(void) const
{
return m_hasError;
}
QString errorString(void) const
{
return m_errorString;
}
private:
std::string m_loggerId;
bool m_hasError;
Expand All @@ -153,23 +182,27 @@ class QmlLogging : public QQuickItem
FUNCTION_DEFINER(FATAL, fatal)
FUNCTION_DEFINER(TRACE, trace)
FUNCTION_DEFINER_V(verbose)

// Time tracker functions
Q_INVOKABLE inline void timeBegin(const QString& blockName) {
Q_INVOKABLE inline void timeBegin(const QString &blockName)
{
m_tracker.timeBegin(blockName);
}

Q_INVOKABLE inline void timeEnd(const QString& blockName) {

Q_INVOKABLE inline void timeEnd(const QString &blockName)
{
m_tracker.timeEnd(blockName);
}

Q_INVOKABLE inline void timeCheck(const QString& blockName,
const QString& checkpointId = QString()) {

Q_INVOKABLE inline void timeCheck(const QString &blockName,
const QString &checkpointId = QString())
{
m_tracker.timeCheck(blockName, checkpointId);
}

// Count functions
Q_INVOKABLE inline void count(const QString& msg) {
Q_INVOKABLE inline void count(const QString &msg)
{
if (!m_hasError) {
QHash<QString, int>::iterator iterator = m_counters.find(msg);
if (iterator == m_counters.end()) {
Expand All @@ -178,13 +211,15 @@ class QmlLogging : public QQuickItem
CLOG(INFO, m_loggerId.c_str()) << msg << " {" << ++*iterator << "}";
}
}

Q_INVOKABLE inline void countEnd(const QString& msg) {

Q_INVOKABLE inline void countEnd(const QString &msg)
{
m_counters.remove(msg);
}

// Assertion functions
Q_INVOKABLE inline void assert(bool condition, const QString& msg) {
Q_INVOKABLE inline void assert(bool condition, const QString &msg)
{
if (_ELPP_DEBUG_LOG && !condition) {
CLOG(FATAL, m_loggerId.c_str()) << "Assertion failed: [" << msg << "] ";
}
Expand Down

0 comments on commit 5871c5a

Please sign in to comment.