QXmppLogger.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2008-2012 The QXmpp developers
  3. *
  4. * Authors:
  5. * Manjeet Dahiya
  6. * Jeremy Lainé
  7. *
  8. * Source:
  9. * http://code.google.com/p/qxmpp
  10. *
  11. * This file is a part of QXmpp library.
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. */
  24. #include <iostream>
  25. #include <QChildEvent>
  26. #include <QDateTime>
  27. #include <QFile>
  28. #include <QMetaType>
  29. #include <QTextStream>
  30. #include "QXmppLogger.h"
  31. QXmppLogger* QXmppLogger::m_logger = 0;
  32. static const char *typeName(QXmppLogger::MessageType type)
  33. {
  34. switch (type)
  35. {
  36. case QXmppLogger::DebugMessage:
  37. return "DEBUG";
  38. case QXmppLogger::InformationMessage:
  39. return "INFO";
  40. case QXmppLogger::WarningMessage:
  41. return "WARNING";
  42. case QXmppLogger::ReceivedMessage:
  43. return "RECEIVED";
  44. case QXmppLogger::SentMessage:
  45. return "SENT";
  46. default:
  47. return "";
  48. }
  49. }
  50. static QString formatted(QXmppLogger::MessageType type, const QString& text)
  51. {
  52. return QDateTime::currentDateTime().toString() + " " +
  53. QString::fromLatin1(typeName(type)) + " " +
  54. text;
  55. }
  56. static void relaySignals(QXmppLoggable *from, QXmppLoggable *to)
  57. {
  58. QObject::connect(from, SIGNAL(logMessage(QXmppLogger::MessageType,QString)),
  59. to, SIGNAL(logMessage(QXmppLogger::MessageType,QString)));
  60. QObject::connect(from, SIGNAL(setGauge(QString,double)),
  61. to, SIGNAL(setGauge(QString,double)));
  62. QObject::connect(from, SIGNAL(updateCounter(QString,qint64)),
  63. to, SIGNAL(updateCounter(QString,qint64)));
  64. }
  65. /// Constructs a new QXmppLoggable.
  66. ///
  67. /// \param parent
  68. QXmppLoggable::QXmppLoggable(QObject *parent)
  69. : QObject(parent)
  70. {
  71. QXmppLoggable *logParent = qobject_cast<QXmppLoggable*>(parent);
  72. if (logParent) {
  73. relaySignals(this, logParent);
  74. }
  75. }
  76. /// \cond
  77. void QXmppLoggable::childEvent(QChildEvent *event)
  78. {
  79. QXmppLoggable *child = qobject_cast<QXmppLoggable*>(event->child());
  80. if (!child)
  81. return;
  82. if (event->added()) {
  83. relaySignals(child, this);
  84. } else if (event->removed()) {
  85. disconnect(child, SIGNAL(logMessage(QXmppLogger::MessageType,QString)),
  86. this, SIGNAL(logMessage(QXmppLogger::MessageType,QString)));
  87. disconnect(child, SIGNAL(setGauge(QString,double)),
  88. this, SIGNAL(setGauge(QString,double)));
  89. disconnect(child, SIGNAL(updateCounter(QString,qint64)),
  90. this, SIGNAL(updateCounter(QString,qint64)));
  91. }
  92. }
  93. /// \endcond
  94. class QXmppLoggerPrivate
  95. {
  96. public:
  97. QXmppLoggerPrivate(QXmppLogger *qq);
  98. QXmppLogger::LoggingType loggingType;
  99. QFile *logFile;
  100. QString logFilePath;
  101. QXmppLogger::MessageTypes messageTypes;
  102. private:
  103. QXmppLogger *q;
  104. };
  105. QXmppLoggerPrivate::QXmppLoggerPrivate(QXmppLogger *qq)
  106. : loggingType(QXmppLogger::NoLogging),
  107. logFile(0),
  108. logFilePath("QXmppClientLog.log"),
  109. messageTypes(QXmppLogger::AnyMessage),
  110. q(qq)
  111. {
  112. }
  113. /// Constructs a new QXmppLogger.
  114. ///
  115. /// \param parent
  116. QXmppLogger::QXmppLogger(QObject *parent)
  117. : QObject(parent)
  118. {
  119. d = new QXmppLoggerPrivate(this);
  120. // make it possible to pass QXmppLogger::MessageType between threads
  121. qRegisterMetaType< QXmppLogger::MessageType >("QXmppLogger::MessageType");
  122. }
  123. QXmppLogger::~QXmppLogger()
  124. {
  125. delete d;
  126. }
  127. /// Returns the default logger.
  128. ///
  129. QXmppLogger* QXmppLogger::getLogger()
  130. {
  131. if(!m_logger)
  132. m_logger = new QXmppLogger();
  133. return m_logger;
  134. }
  135. /// Returns the handler for logging messages.
  136. ///
  137. QXmppLogger::LoggingType QXmppLogger::loggingType()
  138. {
  139. return d->loggingType;
  140. }
  141. /// Sets the handler for logging messages.
  142. ///
  143. /// \param type
  144. void QXmppLogger::setLoggingType(QXmppLogger::LoggingType type)
  145. {
  146. if (d->loggingType != type) {
  147. d->loggingType = type;
  148. reopen();
  149. }
  150. }
  151. /// Returns the types of messages to log.
  152. ///
  153. QXmppLogger::MessageTypes QXmppLogger::messageTypes()
  154. {
  155. return d->messageTypes;
  156. }
  157. /// Sets the types of messages to log.
  158. ///
  159. /// \param types
  160. void QXmppLogger::setMessageTypes(QXmppLogger::MessageTypes types)
  161. {
  162. d->messageTypes = types;
  163. }
  164. /// Add a logging message.
  165. ///
  166. /// \param type
  167. /// \param text
  168. void QXmppLogger::log(QXmppLogger::MessageType type, const QString& text)
  169. {
  170. // filter messages
  171. if (!d->messageTypes.testFlag(type))
  172. return;
  173. switch(d->loggingType)
  174. {
  175. case QXmppLogger::FileLogging:
  176. if (!d->logFile) {
  177. d->logFile = new QFile(d->logFilePath);
  178. d->logFile->open(QIODevice::WriteOnly | QIODevice::Append);
  179. }
  180. QTextStream(d->logFile) << formatted(type, text) << "\n";
  181. break;
  182. case QXmppLogger::StdoutLogging:
  183. std::cout << qPrintable(formatted(type, text)) << std::endl;
  184. break;
  185. case QXmppLogger::SignalLogging:
  186. emit message(type, text);
  187. break;
  188. default:
  189. break;
  190. }
  191. }
  192. /// Sets the given \a gauge to \a value.
  193. ///
  194. /// NOTE: the base implementation does nothing.
  195. void QXmppLogger::setGauge(const QString &gauge, double value)
  196. {
  197. Q_UNUSED(gauge);
  198. Q_UNUSED(value);
  199. }
  200. /// Updates the given \a counter by \a amount.
  201. ///
  202. /// NOTE: the base implementation does nothing.
  203. void QXmppLogger::updateCounter(const QString &counter, qint64 amount)
  204. {
  205. Q_UNUSED(counter);
  206. Q_UNUSED(amount);
  207. }
  208. /// Returns the path to which logging messages should be written.
  209. ///
  210. /// \sa loggingType()
  211. QString QXmppLogger::logFilePath()
  212. {
  213. return d->logFilePath;
  214. }
  215. /// Sets the path to which logging messages should be written.
  216. ///
  217. /// \param path
  218. ///
  219. /// \sa setLoggingType()
  220. void QXmppLogger::setLogFilePath(const QString &path)
  221. {
  222. if (d->logFilePath != path) {
  223. d->logFilePath = path;
  224. reopen();
  225. }
  226. }
  227. /// If logging to a file, causes the file to be re-opened.
  228. ///
  229. void QXmppLogger::reopen()
  230. {
  231. if (d->logFile) {
  232. delete d->logFile;
  233. d->logFile = 0;
  234. }
  235. }