#31 Squashed merge of commits relating to the plugin system and IContext redesign, from the 'interconnect' branch.

This commit is contained in:
Mathew Sutcliffe
2013-04-17 01:26:54 +01:00
parent 4e812975b4
commit 9916419678
57 changed files with 1517 additions and 1092 deletions

View File

@@ -7,209 +7,177 @@
#define UNICODE
#endif
#include "blackmisc/log.h"
#include "blackmisc/display.h"
#include "blackmisc/debug.h"
#include "blackmisc/context.h"
#include <QTextStream>
#include <QFileInfo>
#include <QtGlobal>
#include "blackmisc/log.h"
#include "blackmisc/display.h"
#include "blackmisc/debug.h"
//TODO do we still need these platform includes here?
#ifdef Q_OS_WIN
# define NOMINMAX
# include <process.h>
# include <windows.h>
# define NOMINMAX
# include <process.h>
# include <windows.h>
#else
# include <unistd.h>
# include <unistd.h>
#endif
namespace BlackMisc
{
QString *CLog::m_applicationName = NULL;
CLog::CLog(TLogType logType) : m_logType (logType)
{
}
CLog::~CLog(void)
{
}
void CLog::setDefaultApplicationName()
CLog::CLog(IContext &context, TLogType logType) : m_context(context), m_logType(logType)
{
//! Get the object from the singleton context and create a new one if not already done
if (m_applicationName == NULL)
{
m_applicationName = static_cast<QString*>(IContext::getInstance().singletonPointer("BlackMisc::CLog::m_applicationName"));
if (m_applicationName == NULL)
{
m_applicationName = new QString;
IContext::getInstance().setSingletonPointer("BlackMisc::CLog::m_applicationName", m_applicationName);
}
}
#ifdef Q_OS_WIN
//! By default, we use the executables name.
if ((*m_applicationName).isEmpty())
{
WCHAR name[1024];
int size = GetModuleFileName (NULL, name, 1023);
QString applicationPath = QString::fromWCharArray(name,size);
(*m_applicationName) = QFileInfo ( applicationPath ).fileName();
}
#else
//! Todo: Check if there a corresponding API in Linux and Mac
//! For the time being, set it to unknown.
if ((*m_applicationName).isEmpty())
{
*m_applicationName = "<Unknown>";
}
#endif
}
void CLog::printWithNewLine( QString &message )
CLog::~CLog(void)
{
//! If we have no displays, we have nothing to do.
if ( hasNoDisplays() )
}
void CLog::printWithNewLine(QString &message)
{
//! If we have no displays, we have nothing to do.
if (hasNoDisplays())
{
return;
}
QChar newLine = '\n';
//! We should print the message with a new line. So check
//! if one is already there. If not append it.
//! We should print the message with a new line. So check
//! if one is already there. If not append it.
if (!message.endsWith(newLine, Qt::CaseInsensitive))
message.append(newLine);
printString (message);
printString(message);
}
void CLog::print( QString &message )
void CLog::print(QString &message)
{
//! If we have no displays, we have nothing to do.
if ( hasNoDisplays() )
//! If we have no displays, we have nothing to do.
if (hasNoDisplays())
{
return;
}
printString (message);
printString(message);
}
void CLog::printString( QString &message )
void CLog::printString(QString &message)
{
QString logDisplay;
//! Just in case, lets set the default name
setDefaultApplicationName ();
//! Just in case, lets set the default name
m_context.setDefaultApplicationName();
//! If the current line is empty, then put some information as
//! the prefix.
//! Be aware: This information must be set by the \sa setLogInformation()
//! before.
if ( m_logLine.isEmpty() )
{
m_logInformation.m_dateTime = QDateTime::currentDateTime();
m_logInformation.m_logType = m_logType;
m_logInformation.m_applicationName = *m_applicationName;
m_logInformation.m_threadId = 0; //getThreadId();
m_logInformation.m_sourceFile = m_sourceFile;
m_logInformation.m_line = m_line;
m_logInformation.m_methodName = m_methodName;
//! If the current line is empty, then put some information as
//! the prefix.
//! Be aware: This information must be set by the \sa setLogInformation()
//! before.
if (m_logLine.isEmpty())
{
m_logInformation.m_dateTime = QDateTime::currentDateTime();
m_logInformation.m_logType = m_logType;
m_logInformation.m_applicationName = m_context.getApplicationName();
m_logInformation.m_threadId = 0; //getThreadId();
m_logInformation.m_sourceFile = m_sourceFile;
m_logInformation.m_line = m_line;
m_logInformation.m_methodName = m_methodName;
m_logLine = message;
}
else
{
m_logLine += message;
}
//! If this is not the end of the line, we are done for now.
if ( ! message.contains('\n') )
{
return;
}
m_logLine = message;
}
else
{
m_logLine += message;
}
for ( TLogDisplayList::iterator it = m_logDisplays.begin(); it != m_logDisplays.end(); ++it)
//! If this is not the end of the line, we are done for now.
if (! message.contains('\n'))
{
return;
}
for (TLogDisplayList::iterator it = m_logDisplays.begin(); it != m_logDisplays.end(); ++it)
{
(*it)->print(m_logInformation, m_logLine);
}
//! Reset everything for the next line
//! Reset everything for the next line
m_logLine.clear();
resetLogInformation();
}
void CLog::attachDisplay(ILogDisplay *display)
{
//! Display must be a valid pointer
{
//! Display must be a valid pointer
if (display == NULL)
{
printf ("Trying to add a NULL pointer\n");
return;
}
//! Check if it is already attached
if ( !m_logDisplays.contains(display) )
{
m_logDisplays.push_back (display);
printf("Trying to add a NULL pointer\n");
return;
}
//! Check if it is already attached
if (!m_logDisplays.contains(display))
{
m_logDisplays.push_back(display);
}
else
{
bWarning << "Couldn't attach the display - already in the list!";
bWarning(m_context) << "Couldn't attach the display - already in the list!";
}
}
}
ILogDisplay* CLog::getDisplay(const QString &displayName)
{
//! Must be a valid name
ILogDisplay *CLog::getDisplay(const QString &displayName)
{
//! Must be a valid name
if (displayName.isEmpty())
{
bWarning << "Cannot return a display with empty name!";
bWarning(m_context) << "Cannot return a display with empty name!";
return NULL;
}
TLogDisplayList::const_iterator it;
//! Loop through the list and find the candidate
for ( it = m_logDisplays.constBegin(); it != m_logDisplays.constEnd(); ++it)
//! Loop through the list and find the candidate
for (it = m_logDisplays.constBegin(); it != m_logDisplays.constEnd(); ++it)
{
//! Does it have the desired name?
if ( (*it)->DisplayName == displayName )
//! Does it have the desired name?
if ((*it)->DisplayName == displayName)
{
return *it;
}
}
}
}
void CLog::dettachDisplay (ILogDisplay *logDisplay)
void CLog::dettachDisplay(ILogDisplay *logDisplay)
{
//! Must be a valid pointer
//! Must be a valid pointer
if (logDisplay == NULL)
{
bWarning << "Cannot remove a NULL displayer!";
bWarning(m_context) << "Cannot remove a NULL displayer!";
return;
}
//! We should have only one, but just in case.
//! We should have only one, but just in case.
m_logDisplays.removeAll(logDisplay);
}
void CLog::dettachDisplay (const QString &displayName)
void CLog::dettachDisplay(const QString &displayName)
{
//! Must be a valid name
//! Must be a valid name
if (displayName.isEmpty())
{
bWarning << "Cannot remove displayer with empty name!";
bWarning(m_context) << "Cannot remove displayer with empty name!";
return;
}
TLogDisplayList::iterator it;
for ( it = m_logDisplays.begin(); it != m_logDisplays.end(); )
for (it = m_logDisplays.begin(); it != m_logDisplays.end();)
{
if ( (*it)->DisplayName == displayName )
if ((*it)->DisplayName == displayName)
{
it = m_logDisplays.erase(it);
}
@@ -220,15 +188,15 @@ namespace BlackMisc
}
}
bool CLog::isAttached (ILogDisplay *logDisplay) const
bool CLog::isAttached(ILogDisplay *logDisplay) const
{
return m_logDisplays.contains(logDisplay);
}
void CLog::setLogInformation (int line, const char *sourceFile, const char *methodName)
void CLog::setLogInformation(int line, const char *sourceFile, const char *methodName)
{
//! We have to make sure, we at least one display.
if ( !hasNoDisplays() )
//! We have to make sure, we at least one display.
if (!hasNoDisplays())
{
m_mutex.lock();
m_posSet++;
@@ -240,11 +208,11 @@ namespace BlackMisc
void CLog::resetLogInformation()
{
//! It should be impossible that this gets called, withoud
//! having a attached display.
bAssert( !hasNoDisplays() );
//! It should be impossible that this gets called, withoud
//! having a attached display.
Q_ASSERT(!hasNoDisplays());
if ( m_posSet > 0 )
if (m_posSet > 0)
{
m_sourceFile = NULL;
m_line = -1;