changes to the logging system

This commit is contained in:
Patrick von Reth 2014-02-05 17:15:50 +01:00
parent 6cc6f08317
commit 2d95122749
4 changed files with 97 additions and 26 deletions

View File

@ -30,6 +30,13 @@ void Hint::setValue(const QString &key, const QVariant &value)
m_data.insert(key.toLower(), value);
}
void Hint::setValue(const QString &key, QObject *value)
{
m_data.insert(key.toLower(), qVariantFromValue(value));
value->setProperty("hint_key",key);
connect(value, SIGNAL(destroyed()), this, SLOT(slotValueDestroyed()));
}
QVariant Hint::value(const QString &k, const QVariant &defaultValue) const
{
QString key(k.toLower());
@ -84,17 +91,15 @@ bool Hint::containsPrivateValue(const void *owner, const QString &key) const
void Hint::slotValueDestroyed()
{
QObject * o = sender();
snoreDebug( SNORE_DEBUG ) << o << o->property("hint_key");
QString key = o->property("hint_key").toString();
if(!o->property("hint_owner").isNull())
{
m_privateData.take(QPair<quintptr,QString>(o->property("hint_owner").value<quintptr>(),key));
m_privateData.take(QPair<quintptr,QString>(o->property("hint_owner").value<quintptr>(), key));
}
else
{
m_data.take(key);
}
}
QDebug operator<<( QDebug debug, const Snore::Hint &hint )

View File

@ -104,6 +104,7 @@ public:
* @return whether the key is set
*/
bool containsPrivateValue(const void *owner, const QString & key ) const;
private slots:
void slotValueDestroyed();

View File

@ -18,15 +18,71 @@
*/
#include "log.h"
#include <iostream>
#include <fstream>
#include <iostream>
#include <QDir>
#include <QMutex>
#include <QApplication>
#include <QTextStream>
using namespace Snore;
int SnoreLog::s_debugLevel = -1;
class Loger
{
public:
static int s_debugLevel;
static QTextStream s_textStream;
static inline int debugLvl()
{
if(s_debugLevel == -1)
{
s_debugLevel = qgetenv("LIBSNORE_DEBUG_LVL").toInt();
}
return s_debugLevel;
}
static inline bool isLogToFileEnabled()
{
static int s_logToFile = -1;
if(s_logToFile == -1)
{
s_logToFile = qgetenv("LIBSNORE_LOG_TO_FILE").toInt();
}
return s_logToFile == 1;
}
static inline QTextStream &logFile()
{
static QTextStream *s_out = NULL;
static QFile *s_file = NULL;
if(!s_out)
{
QString name = QString("%1/libsnore/%2-log.txt").arg(QDir::tempPath(), qApp->applicationName().isEmpty()?QString::number(qApp->applicationPid()):qApp->applicationName());
if(!qgetenv("LIBSNORE_LOGFILE").isNull())
{
name = QString(qgetenv("LIBSNORE_LOGFILE"));
}
std::cout << "Started logging to " << name.toUtf8().constData() << std::endl;
s_file = new QFile(name);
s_file->open(QFile::WriteOnly);
s_out = new QTextStream(s_file);
}
return *s_out;
}
static inline QTextStream &outStream()
{
return s_textStream;
}
};
int Loger::s_debugLevel = -1;
QTextStream Loger::s_textStream(stdout);
SnoreLog::SnoreLog(SnoreDebugLevels lvl):
QDebug(&m_msg),
@ -36,18 +92,24 @@ SnoreLog::SnoreLog(SnoreDebugLevels lvl):
SnoreLog::~SnoreLog()
{
static std::ofstream m_logg(QString("%1/libsnore/%2-log.txt").arg(QDir::tempPath(), qApp->applicationName().isEmpty()?QString::number(qApp->applicationPid()):qApp->applicationName()).toUtf8().constData());
static QMutex m_mutex;
QMutexLocker lock(&m_mutex);
if(debugLvl() >= m_lvl)
if(Loger::debugLvl() >= m_lvl)
{
std::cout << m_msg.toUtf8().constData() << std::endl;
std::cout.flush();
Loger::outStream() << m_msg << "\n";
Loger::outStream().flush();
}
if(Loger::isLogToFileEnabled())
{
Loger::logFile() << m_msg << "\n";
Loger::logFile().flush();
}
m_logg << m_msg.toUtf8().constData() << std::endl;
}
void SnoreLog::setDebugLvl(int i)
{
s_debugLevel = i;
Loger::s_debugLevel = i;
}
void SnoreLog::setOutputDevice(QIODevice *device)
{
Loger::s_textStream.setDevice(device);
}

View File

@ -56,7 +56,12 @@ enum SnoreDebugLevels
* Logg macro use to logg messages.
* snoreDebug( SNORE_DEBUG ) << "Message" << notification;
*/
#if !defined(QT_NO_DEBUG_OUTPUT)
#define snoreDebug(X) Snore::SnoreLog( X ) << Q_FUNC_INFO
#else
#define snoreDebug(X) QNoDebug()
#endif
namespace Snore
@ -85,21 +90,19 @@ public:
*/
static void setDebugLvl(int lvl);
/**
* Sets the output device, the default is stdout
* @param device the output device
*/
static void setOutputDevice(QIODevice *device);
private:
static inline int debugLvl()
{
if(s_debugLevel == -1)
{
s_debugLevel = qgetenv("SNORE_DEBUG_LVL").toInt();
}
return s_debugLevel;
}
static int s_debugLevel;
SnoreDebugLevels m_lvl;
QString m_msg;
};
}