aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorJeremy Lainé <jeremy.laine@m4x.org>2010-03-08 17:48:16 +0000
committerJeremy Lainé <jeremy.laine@m4x.org>2010-03-08 17:48:16 +0000
commit9bc160b82ac13d823223c99a1fe885343bb251b1 (patch)
treefe750f71fbbc6ed46900f5625232ee54c407f83d /source
parentf123cb3997c0a12b00580dd2458bb3b308721486 (diff)
downloadqxmpp-9bc160b82ac13d823223c99a1fe885343bb251b1.tar.gz
refactor logging
Diffstat (limited to 'source')
-rw-r--r--source/QXmppClient.cpp10
-rw-r--r--source/QXmppClient.h2
-rw-r--r--source/QXmppLogger.cpp34
-rw-r--r--source/QXmppLogger.h10
-rw-r--r--source/QXmppReconnectionManager.cpp3
-rw-r--r--source/QXmppStream.cpp5
-rw-r--r--source/QXmppStream.h1
-rw-r--r--source/QXmppUtils.cpp4
-rw-r--r--source/QXmppUtils.h4
9 files changed, 41 insertions, 32 deletions
diff --git a/source/QXmppClient.cpp b/source/QXmppClient.cpp
index 454302b2..278fab77 100644
--- a/source/QXmppClient.cpp
+++ b/source/QXmppClient.cpp
@@ -23,6 +23,7 @@
#include "QXmppClient.h"
+#include "QXmppLogger.h"
#include "QXmppStream.h"
#include "QXmppRoster.h"
#include "QXmppMessage.h"
@@ -185,7 +186,7 @@ void QXmppClient::connectToServer(const QString& host,
else
{
qWarning("QXmppClient::connectToServer: Invalid bareJid");
- log(QString("Invalid bareJid"));
+ logger() << QString("Invalid bareJid");
}
}
@@ -521,3 +522,10 @@ bool QXmppClient::handleStreamElement(const QDomElement &element)
{
return false;
}
+
+/// Return the QXmppLogger associated with the client.
+
+QXmppLogger &QXmppClient::logger()
+{
+ return QXmppLogger::defaultLogger();
+}
diff --git a/source/QXmppClient.h b/source/QXmppClient.h
index 74d075a4..4339535d 100644
--- a/source/QXmppClient.h
+++ b/source/QXmppClient.h
@@ -32,6 +32,7 @@
#include "QXmppConfiguration.h"
#include "QXmppPresence.h"
+class QXmppLogger;
class QXmppStream;
class QXmppPresence;
class QXmppMessage;
@@ -194,6 +195,7 @@ public:
QXmppClient::StreamError getXmppStreamError();
virtual bool handleStreamElement(const QDomElement &element);
+ virtual QXmppLogger &logger();
public slots:
void sendPacket(const QXmppPacket&);
diff --git a/source/QXmppLogger.cpp b/source/QXmppLogger.cpp
index 2a2a979e..dac41b19 100644
--- a/source/QXmppLogger.cpp
+++ b/source/QXmppLogger.cpp
@@ -35,12 +35,17 @@ QXmppLogger::QXmppLogger()
{
}
-QXmppLogger* QXmppLogger::getLogger()
+QXmppLogger &QXmppLogger::defaultLogger()
{
if(!m_logger)
m_logger = new QXmppLogger();
- return m_logger;
+ return *m_logger;
+}
+
+QXmppLogger* QXmppLogger::getLogger()
+{
+ return &defaultLogger();
}
void QXmppLogger::setLoggingType(QXmppLogger::LoggingType log)
@@ -53,7 +58,7 @@ QXmppLogger::LoggingType QXmppLogger::loggingType()
return m_loggingType;
}
-void QXmppLogger::log(const QString& str)
+QXmppLogger &QXmppLogger::operator<<(const QByteArray &str)
{
switch(m_loggingType)
{
@@ -65,34 +70,19 @@ void QXmppLogger::log(const QString& str)
m_file.close();
break;
case QXmppLogger::STDOUT:
- std::cout<<qPrintable(str)<<std::endl;
+ std::cout<<str.constData()<<std::endl;
break;
case QXmppLogger::NONE:
break;
default:
break;
}
+ return *this;
}
-void QXmppLogger::log(const QByteArray& str)
+QXmppLogger &QXmppLogger::operator<<(const QString &str)
{
- switch(m_loggingType)
- {
- case QXmppLogger::FILE:
- m_file.open(QIODevice::Append);
- m_stream.setDevice(&m_file);
- m_stream << QTime::currentTime().toString("hh:mm:ss.zzz") << " : "<<
- str << "\n\n";
- m_file.close();
- break;
- case QXmppLogger::STDOUT:
- std::cout<<str.constData()<<std::endl;
- break;
- case QXmppLogger::NONE:
- break;
- default:
- break;
- }
+ return (*this << str.toLocal8Bit());
}
QXmppLogger::LoggingType QXmppLogger::getLoggingType()
diff --git a/source/QXmppLogger.h b/source/QXmppLogger.h
index 66c857a2..dc964e06 100644
--- a/source/QXmppLogger.h
+++ b/source/QXmppLogger.h
@@ -39,13 +39,15 @@ public:
STDOUT
};
- static QXmppLogger* getLogger();
+ static QXmppLogger &defaultLogger();
QXmppLogger::LoggingType loggingType();
void setLoggingType(QXmppLogger::LoggingType);
- void log(const QString& str);
- void log(const QByteArray& str);
- // deprecated accessors, use the form without "get" instead
+ QXmppLogger& operator<<(const QByteArray &b);
+ QXmppLogger& operator<<(const QString &str);
+
+ // deprecated methods
+ static QXmppLogger* Q_DECL_DEPRECATED getLogger();
QXmppLogger::LoggingType Q_DECL_DEPRECATED getLoggingType();
private:
diff --git a/source/QXmppReconnectionManager.cpp b/source/QXmppReconnectionManager.cpp
index 24557d9f..4cb2a838 100644
--- a/source/QXmppReconnectionManager.cpp
+++ b/source/QXmppReconnectionManager.cpp
@@ -24,6 +24,7 @@
#include "QXmppReconnectionManager.h"
#include "QXmppClient.h"
+#include "QXmppLogger.h"
#include "QXmppUtils.h"
QXmppReconnectionManager::QXmppReconnectionManager(QXmppClient* client) :
@@ -82,7 +83,7 @@ void QXmppReconnectionManager::reconnect()
{
if(m_client)
{
- log(QString("QXmppReconnectionManager::reconnect()"));
+ m_client->logger() << QString("QXmppReconnectionManager::reconnect()");
emit reconnectingNow();
m_client->connectToServer(m_client->getConfiguration());
}
diff --git a/source/QXmppStream.cpp b/source/QXmppStream.cpp
index b2142c2f..bf4af1f9 100644
--- a/source/QXmppStream.cpp
+++ b/source/QXmppStream.cpp
@@ -240,6 +240,11 @@ void QXmppStream::sendNonSASLAuthQuery( const QString &to )
sendPacket(authQuery);
}
+void QXmppStream::log(const QString &data)
+{
+ m_client->logger() << data;
+}
+
void QXmppStream::parser(const QByteArray& data)
{
QDomDocument doc;
diff --git a/source/QXmppStream.h b/source/QXmppStream.h
index 4fea2a0b..b8e57e96 100644
--- a/source/QXmppStream.h
+++ b/source/QXmppStream.h
@@ -143,6 +143,7 @@ private:
int m_authStep;
QXmppConfiguration& getConfiguration();
+ void log(const QString&);
void parser(const QByteArray&);
void sendStartStream();
void sendEndStream();
diff --git a/source/QXmppUtils.cpp b/source/QXmppUtils.cpp
index 256a4beb..820f3a13 100644
--- a/source/QXmppUtils.cpp
+++ b/source/QXmppUtils.cpp
@@ -118,12 +118,12 @@ void helperToXmlAddTextElement(QXmlStreamWriter* stream, const QString& name,
void log(const QString& str)
{
- QXmppLogger::getLogger()->log(str);
+ QXmppLogger::defaultLogger() << str;
}
void log(const QByteArray& str)
{
- QXmppLogger::getLogger()->log(str);
+ QXmppLogger::defaultLogger() << str;
}
QString escapeString(const QString& str)
diff --git a/source/QXmppUtils.h b/source/QXmppUtils.h
index 6a66e599..7251bde2 100644
--- a/source/QXmppUtils.h
+++ b/source/QXmppUtils.h
@@ -52,8 +52,8 @@ void helperToXmlAddTextElement(QXmlStreamWriter* stream, const QString& name,
void helperToXmlAddNumberElement(QXmlStreamWriter* stream, const QString& name,
int value);
-void log(const QString& str);
-void log(const QByteArray& str);
+void Q_DECL_DEPRECATED log(const QString& str);
+void Q_DECL_DEPRECATED log(const QByteArray& str);
QString escapeString(const QString& str);
QString unescapeString(const QString& str);