#1254 Use static methods instead of macros

This commit is contained in:
Magne Sjaastad 2017-02-27 13:17:32 +01:00
parent 97d7e5b809
commit bf1f8fcbaa
5 changed files with 94 additions and 120 deletions

View File

@ -337,14 +337,14 @@ bool RiaApplication::loadProject(const QString& projectFileName, ProjectLoadActi
closeProject();
RI_LOG_INFO_QSTR(QString("Starting to open project file : '%1'").arg(projectFileName));
RiaLogging::info(QString("Starting to open project file : '%1'").arg(projectFileName));
// Open the project file and read the serialized data.
// Will initialize itself.
if (!QFile::exists(projectFileName))
{
RI_LOG_ERROR_QSTR(QString("File does not exist : '%1'").arg(projectFileName));
RiaLogging::info(QString("File does not exist : '%1'").arg(projectFileName));
return false;
}
@ -547,7 +547,7 @@ bool RiaApplication::loadProject(const QString& projectFileName, ProjectLoadActi
// Execute command objects, and release the mutex when the queue is empty
executeCommandObjects();
RI_LOG_INFO_QSTR(QString("Completed open of project file : '%1'").arg(projectFileName));
RiaLogging::info(QString("Completed open of project file : '%1'").arg(projectFileName));
return true;
}

View File

@ -30,6 +30,8 @@
#include <cstdio>
#endif
#include "QString"
@ -43,15 +45,15 @@ class RiaDefaultConsoleLogger : public RiaLogger
public:
RiaDefaultConsoleLogger();
virtual int level() const;
virtual void setLevel(int logLevel);
virtual void error( const char* message, const char* fileName, int lineNumber);
virtual void warning(const char* message, const char* fileName, int lineNumber);
virtual void info( const char* message, const char* fileName, int lineNumber);
virtual void debug( const char* message, const char* fileName, int lineNumber);
virtual int level() const override;
virtual void setLevel(int logLevel) override;
virtual void error( const char* message) override;
virtual void warning(const char* message) override;
virtual void info( const char* message) override;
virtual void debug( const char* message) override;
private:
static void writeMessageToConsole(const char* prefix, const char* message, const char* fileName, int lineNumber);
static void writeMessageToConsole(const char* prefix, const char* message);
static void writeToConsole(const std::string& str);
private:
@ -87,42 +89,42 @@ void RiaDefaultConsoleLogger::setLevel(int logLevel)
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaDefaultConsoleLogger::error(const char* message, const char* fileName, int lineNumber)
void RiaDefaultConsoleLogger::error(const char* message)
{
writeMessageToConsole("ERROR: ", message, fileName, lineNumber);
writeMessageToConsole("ERROR: ", message);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaDefaultConsoleLogger::warning(const char* message, const char*, int)
void RiaDefaultConsoleLogger::warning(const char* message)
{
writeMessageToConsole("warn: ", message, NULL, 0);
writeMessageToConsole("warn: ", message);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaDefaultConsoleLogger::info(const char* message, const char*, int)
void RiaDefaultConsoleLogger::info(const char* message)
{
writeMessageToConsole("info: ", message, NULL, 0);
writeMessageToConsole("info: ", message);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaDefaultConsoleLogger::debug(const char* message, const char*, int)
void RiaDefaultConsoleLogger::debug(const char* message)
{
writeMessageToConsole("debug: ", message, NULL, 0);
writeMessageToConsole("debug: ", message);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaDefaultConsoleLogger::writeMessageToConsole(const char* prefix, const char* message, const char* fileName, int lineNumber)
void RiaDefaultConsoleLogger::writeMessageToConsole(const char* prefix, const char* message)
{
std::ostringstream oss;
@ -138,11 +140,6 @@ void RiaDefaultConsoleLogger::writeMessageToConsole(const char* prefix, const ch
oss << "<no message>" << std::endl;
}
if (fileName)
{
oss << " -file " << RiaLogger::shortFileName(fileName) << ", line " << lineNumber << std::endl;
}
writeToConsole(oss.str());
}
@ -167,44 +164,6 @@ void RiaDefaultConsoleLogger::writeToConsole(const std::string& str)
#endif
}
//==================================================================================================
//
//
//
//==================================================================================================
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const char* RiaLogger::shortFileName(const char* fileName)
{
// VF_ASSERT(fileName);
const char* ptrToLastSlash = strrchr(fileName, '/');
#ifdef WIN32
const char* ptrToLastBwdSlash = strrchr(fileName, '\\');
if (ptrToLastBwdSlash > ptrToLastSlash)
{
ptrToLastSlash = ptrToLastBwdSlash;
}
#endif
if (ptrToLastSlash)
{
return ptrToLastSlash + 1;
}
else
{
return fileName;
}
}
//==================================================================================================
//
//
@ -247,3 +206,47 @@ void RiaLogging::deleteLoggerInstance()
sm_logger = NULL;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaLogging::error(const QString& message)
{
if (sm_logger && sm_logger->level() >= RI_LL_ERROR)
{
sm_logger->error(message.toLatin1().constData());
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaLogging::warning(const QString& message)
{
if (sm_logger && sm_logger->level() >= RI_LL_WARNING)
{
sm_logger->warning(message.toLatin1().constData());
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaLogging::info(const QString& message)
{
if (sm_logger && sm_logger->level() >= RI_LL_INFO)
{
sm_logger->info(message.toLatin1().constData());
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaLogging::debug(const QString& message)
{
if (sm_logger && sm_logger->level() >= RI_LL_DEBUG)
{
sm_logger->debug(message.toLatin1().constData());
}
}

View File

@ -18,6 +18,7 @@
#pragma once
class QString;
enum RILogLevel
{
@ -42,13 +43,10 @@ public:
virtual int level() const = 0;
virtual void setLevel(int logLevel) = 0;
virtual void error( const char* message, const char* fileName, int lineNumber) = 0;
virtual void warning(const char* message, const char* fileName, int lineNumber) = 0;
virtual void info( const char* message, const char* fileName, int lineNumber) = 0;
virtual void debug( const char* message, const char* fileName, int lineNumber) = 0;
protected:
static const char* shortFileName(const char* fileName);
virtual void error( const char* message) = 0;
virtual void warning(const char* message) = 0;
virtual void info( const char* message) = 0;
virtual void debug( const char* message) = 0;
};
@ -65,25 +63,12 @@ public:
static void setLoggerInstance(RiaLogger* loggerInstance);
static void deleteLoggerInstance();
static void error( const QString& message);
static void warning(const QString& message);
static void info( const QString& message);
static void debug( const QString& message);
private:
static RiaLogger* sm_logger;
};
// Helper macros for writing log messages
#define RI_LOG_ERROR_2(theLogger, theMessage) if ((theLogger)->level() >= RI_LL_ERROR) { (theLogger)->error((theMessage), __FILE__, __LINE__); }
#define RI_LOG_WARNING_2(theLogger, theMessage) if ((theLogger)->level() >= RI_LL_WARNING) { (theLogger)->warning((theMessage), __FILE__, __LINE__); }
#define RI_LOG_INFO_2(theLogger, theMessage) if ((theLogger)->level() >= RI_LL_INFO) { (theLogger)->info((theMessage), __FILE__, __LINE__); }
#define RI_LOG_DEBUG_2(theLogger, theMessage) if ((theLogger)->level() >= RI_LL_DEBUG) { (theLogger)->debug((theMessage), __FILE__, __LINE__); }
#define RI_LOG_ERROR(theMessage) RI_LOG_ERROR_2( RiaLogging::loggerInstance(), theMessage)
#define RI_LOG_ERROR_QSTR(theMessage) RI_LOG_ERROR_2( RiaLogging::loggerInstance(), (theMessage).toLatin1().constData())
#define RI_LOG_WARNING(theMessage) RI_LOG_WARNING_2( RiaLogging::loggerInstance(), theMessage)
#define RI_LOG_WARNING_QSTR(theMessage) RI_LOG_WARNING_2( RiaLogging::loggerInstance(), (theMessage).toLatin1().constData())
#define RI_LOG_INFO(theMessage) RI_LOG_INFO_2( RiaLogging::loggerInstance(), theMessage)
#define RI_LOG_INFO_QSTR(theMessage) RI_LOG_INFO_2( RiaLogging::loggerInstance(), (theMessage).toLatin1().constData())
#define RI_LOG_DEBUG(theMessage) RI_LOG_DEBUG_2( RiaLogging::loggerInstance(), theMessage)
#define RI_LOG_DEBUG_QSTR(theMessage) RI_LOG_DEBUG_2( RiaLogging::loggerInstance(), (theMessage).toLatin1().constData())

View File

@ -54,7 +54,7 @@ RiuMessagePanel::RiuMessagePanel(QDockWidget* parent)
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMessagePanel::addMessage(RILogLevel messageLevel, const QString& msg, const QString& srcInfo)
void RiuMessagePanel::addMessage(RILogLevel messageLevel, const QString& msg)
{
QColor clr(Qt::black);
if (messageLevel == RI_LL_ERROR) clr = Qt::red;
@ -68,15 +68,6 @@ void RiuMessagePanel::addMessage(RILogLevel messageLevel, const QString& msg, co
m_textEdit->setCurrentCharFormat(form);
m_textEdit->appendPlainText(msg);
if (!srcInfo.isEmpty())
{
form.setForeground(clr);
form.setFontWeight(QFont::Normal);
form.setFontItalic(true);
m_textEdit->setCurrentCharFormat(form);
m_textEdit->appendPlainText(" " + srcInfo);
}
m_textEdit->moveCursor(QTextCursor::End);
m_textEdit->ensureCursorVisible();
}
@ -113,7 +104,8 @@ void RiuMessagePanel::slotShowContextMenu(const QPoint& pos)
void RiuMessagePanel::slotClearMessages()
{
m_textEdit->clear();
RI_LOG_INFO("Message window cleared.");
RiaLogging::info("Message window cleared.");
}
@ -155,43 +147,43 @@ void RiuMessagePanelLogger::setLevel(int logLevel)
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMessagePanelLogger::error(const char* message, const char* fileName, int lineNumber)
void RiuMessagePanelLogger::error(const char* message)
{
writeToMessagePanel(RI_LL_ERROR, message, fileName, lineNumber);
writeToMessagePanel(RI_LL_ERROR, message);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMessagePanelLogger::warning(const char* message, const char* fileName, int lineNumber)
void RiuMessagePanelLogger::warning(const char* message)
{
writeToMessagePanel(RI_LL_WARNING, message, fileName, lineNumber);
writeToMessagePanel(RI_LL_WARNING, message);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMessagePanelLogger::info(const char* message, const char* fileName, int lineNumber)
void RiuMessagePanelLogger::info(const char* message)
{
writeToMessagePanel(RI_LL_INFO, message, fileName, lineNumber);
writeToMessagePanel(RI_LL_INFO, message);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMessagePanelLogger::debug(const char* message, const char* fileName, int lineNumber)
void RiuMessagePanelLogger::debug(const char* message)
{
writeToMessagePanel(RI_LL_DEBUG, message, fileName, lineNumber);
writeToMessagePanel(RI_LL_DEBUG, message);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMessagePanelLogger::writeToMessagePanel(RILogLevel messageLevel, const char* message, const char* fileName, int lineNumber)
void RiuMessagePanelLogger::writeToMessagePanel(RILogLevel messageLevel, const char* message)
{
if (messageLevel > m_logLevel)
{
@ -200,13 +192,7 @@ void RiuMessagePanelLogger::writeToMessagePanel(RILogLevel messageLevel, const c
if (m_messagePanel)
{
QString codeLocation;
if (messageLevel == RI_LL_ERROR)
{
codeLocation = QString("(file %1, line %2)").arg(RiaLogger::shortFileName(fileName)).arg(lineNumber);
}
m_messagePanel->addMessage(messageLevel, message, codeLocation);
m_messagePanel->addMessage(messageLevel, message);
}
}

View File

@ -39,7 +39,7 @@ class RiuMessagePanel : public QWidget
public:
RiuMessagePanel(QDockWidget* parent);
void addMessage(RILogLevel messageLevel, const QString& msg, const QString& srcInfo);
void addMessage(RILogLevel messageLevel, const QString& msg);
virtual QSize sizeHint () const;
private slots:
@ -65,13 +65,13 @@ public:
virtual int level() const override;
virtual void setLevel(int logLevel) override;
virtual void error( const char* message, const char* fileName, int lineNumber) override;
virtual void warning(const char* message, const char* fileName, int lineNumber) override;
virtual void info( const char* message, const char* fileName, int lineNumber) override;
virtual void debug( const char* message, const char* fileName, int lineNumber) override;
virtual void error( const char* message) override;
virtual void warning(const char* message) override;
virtual void info( const char* message) override;
virtual void debug( const char* message) override;
private:
void writeToMessagePanel(RILogLevel messageLevel, const char* message, const char* fileName, int lineNumber);
void writeToMessagePanel(RILogLevel messageLevel, const char* message);
private:
QPointer<RiuMessagePanel> m_messagePanel;