Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ctkLogger #1181

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Libs/Core/Testing/Cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ SIMPLE_TEST( ctkFileLoggerTest )
SIMPLE_TEST( ctkHighPrecisionTimerTest )
SIMPLE_TEST( ctkLinearValueProxyTest )
SIMPLE_TEST( ctkLoggerTest1 )
set_property(TEST ctkLoggerTest1 PROPERTY PASS_REGULAR_EXPRESSION "logger.debug\nlogger.info\nlogger.trace\nlogger.warn\nlogger.error\nlogger.fatal")
SIMPLE_TEST( ctkModelTesterTest1 )
SIMPLE_TEST( ctkModelTesterTest2 )
SIMPLE_TEST( ctkPimplTest1 )
Expand Down
170 changes: 157 additions & 13 deletions Libs/Core/Testing/Cpp/ctkLoggerTest1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,181 @@
=========================================================================*/

// Qt includes
#include <QCoreApplication>
#include <QPointer>
#include <QStack>

// CTK includes
#include <ctkErrorLogLevel.h>
#include <ctkLogger.h>
#include <ctkCoreTestingMacros.h>
#include <ctkUtils.h>

// STL includes
#include <cstdlib>
#include <iostream>

namespace
{

// ----------------------------------------------------------------------------
class ctkQtMessageHandler : public QObject
{
public:
typedef ctkQtMessageHandler Self ;
explicit ctkQtMessageHandler()
{
qInstallMessageHandler(ctkQtMessageHandler::messageHandler);
Self::Handlers.push(this);
}
~ctkQtMessageHandler()
{
// Restore previous handler
qInstallMessageHandler(this->Previous);
Self::Handlers.pop();
}

static ctkQtMessageHandler* current()
{
return Self::Handlers.empty() ? nullptr : Self::Handlers.last();
}

static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
Q_UNUSED(context);
if (!Self::current())
{
return;
}
Self::current()->Messages.append(QPair<QtMsgType, QString>(type, msg));
}

static QStack<QPointer<ctkQtMessageHandler>> Handlers;

QtMessageHandler Previous{nullptr};
QList<QPair<QtMsgType, QString>> Messages;
};

QStack<QPointer<ctkQtMessageHandler>> ctkQtMessageHandler::Handlers;

} // end of anonymous namespace

//-----------------------------------------------------------------------------
int TestDefaults();
int TestSetLogLevel();
int TestLogging();
int TestSoftAssert();

// ----------------------------------------------------------------------------
int ctkLoggerTest1(int argc, char * argv [] )
{
Q_UNUSED(argc);
Q_UNUSED(argv);

//--------------------------------------------------------------------
ctkLogger logger("LoggerTest");
CHECK_EXIT_SUCCESS(TestDefaults());
CHECK_EXIT_SUCCESS(TestSetLogLevel());
CHECK_EXIT_SUCCESS(TestLogging());
CHECK_EXIT_SUCCESS(TestSoftAssert());

logger.debug("logger.debug");
logger.info("logger.info");
logger.trace("logger.trace");
logger.warn("logger.warn");
logger.error("logger.error");
logger.fatal("logger.fatal");
return EXIT_SUCCESS;
}

// ----------------------------------------------------------------------------
int TestDefaults()
{
ctkLogger logger("Logger.TestDefaults");

#ifndef QT_NO_DEBUG
CHECK_INT(logger.logLevel(), ctkErrorLogLevel::LogLevel::Debug);
#else
CHECK_INT(logger.logLevel(), ctkErrorLogLevel::LogLevel::Warning);
#endif
return EXIT_SUCCESS;
}

// This should log a warning "Assertion `5 == 6` failed ..."
CTK_SOFT_ASSERT(5 == 6);
// ----------------------------------------------------------------------------
int TestSetLogLevel()
{
ctkLogger logger("Logger.TestSetLogLevel");

// This should not log anything
CTK_SOFT_ASSERT(8 == 8);
// Test setting and getting log level
logger.setLogLevel(ctkErrorLogLevel::LogLevel::Info);
CHECK_INT(logger.logLevel(), ctkErrorLogLevel::LogLevel::Info);

return EXIT_SUCCESS;
}

// ----------------------------------------------------------------------------
int TestLogging()
{
ctkLogger logger("Logger.TestLogging");

logger.setLogLevel(ctkErrorLogLevel::LogLevel::Debug);

QList<QPair<QtMsgType, QString>> messages;
{
ctkQtMessageHandler handler;
logger.debug("logger.debug");
logger.info("logger.info");
logger.trace("logger.trace"); // Not logged because "trace" < "debug" level
logger.warn("logger.warn");
logger.error("logger.error");
logger.fatal("logger.fatal");

CHECK_NOT_NULL(handler.current());
messages = handler.current()->Messages;
}
CHECK_INT(messages.count(), 5);

QPair<QtMsgType, QString> message;

message = messages.takeFirst();
CHECK_INT(message.first, QtMsgType::QtDebugMsg);
CHECK_QSTRING(message.second, QString("logger.debug"));

message = messages.takeFirst();
CHECK_INT(message.first, QtMsgType::QtInfoMsg);
CHECK_QSTRING(message.second, QString("logger.info"));

message = messages.takeFirst();
CHECK_INT(message.first, QtMsgType::QtWarningMsg);
CHECK_QSTRING(message.second, QString("logger.warn"));

message = messages.takeFirst();
CHECK_INT(message.first, QtMsgType::QtCriticalMsg);
CHECK_QSTRING(message.second, QString("logger.error"));

message = messages.takeFirst();
CHECK_INT(message.first, QtMsgType::QtCriticalMsg);
CHECK_QSTRING(message.second, QString("logger.fatal"));

return EXIT_SUCCESS;
}

// ----------------------------------------------------------------------------
int TestSoftAssert()
{
ctkLogger logger("Logger.TestSoftAssert");

QList<QPair<QtMsgType, QString>> messages;
{
ctkQtMessageHandler handler;

// This should log a warning "Assertion `5 == 6` failed ..."
CTK_SOFT_ASSERT(5 == 6);

// This should not log anything
CTK_SOFT_ASSERT(8 == 8);

CHECK_NOT_NULL(handler.current());
messages = handler.current()->Messages;
}
CHECK_INT(messages.count(), 1);

QPair<QtMsgType, QString> message;

message = messages.takeFirst();
CHECK_INT(message.first, QtMsgType::QtWarningMsg);
CHECK_BOOL(message.second.startsWith("Assertion `5 == 6` failed"), true);

return EXIT_SUCCESS;
}
20 changes: 10 additions & 10 deletions Libs/Core/ctkErrorLogLevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ class CTK_CORE_EXPORT ctkErrorLogLevel : public QObject

enum LogLevel
{
None = 0x0,
Unknown = 0x1,
Status = 0x2,
Trace = 0x4,
Debug = 0x8,
Info = 0x10,
Warning = 0x20,
Error = 0x40,
Critical = 0x80,
Fatal = 0x100
Unknown = 0,
Status,
Trace,
Debug,
Info,
Warning,
Error,
Critical,
Fatal,
None,
};
Q_DECLARE_FLAGS(LogLevels, LogLevel)

Expand Down
Loading