summaryrefslogtreecommitdiffstats
path: root/src/tools/messageserver/messageserver.cpp
diff options
context:
space:
mode:
authorespringe <eric.springer@nokia.com>2010-10-07 12:48:32 +1000
committerespringe <eric.springer@nokia.com>2010-10-07 12:48:32 +1000
commitd371c6dfff0a03741939de2c4aa34b3b2678986b (patch)
tree53d8703a0956adfa0d974971ee2b7168c45d4580 /src/tools/messageserver/messageserver.cpp
parent4f3a4b55c2249854319d638f1e7a8fa78675a243 (diff)
Extensions to the logging subsystem (From Vitaly Repin)
Taken from QMF merge request #6 by Vitaly Repin: Current version of the QMF logging subsystem has the following issues: 1) Whenever application is linked against QMF, all the syslog messages are prefixed with QMF regardless of the application settings. E.g., Aug 31 00:25:16 vitnote QMF[4959]: [Critical] Test It makes it harder to identify the application which has printed specific message to syslog. It also overwrites the previous calls to "openlog" made by application which is impolite 2) It is not possible to enable/disable logging through syslog in runtime 3) It is not possible to log into file and stderr 4) Output of Q_ASSERT is not logged. In fact, QWarning(), qFatal(), qDebug() do not go to the log 5) Library installs the SIGHUP handler transparently for the application. It is bad because application might want to install its own handler for this signal and it could conflict with the library's logic. The patch attached for your review fixes the issues above. It also adds the following possibilities: - To log into file and stderr, controllable through the configuration file in runtime - To create chain of loggers if needed (e.g., output to file and stderr, to file and syslog etc). Chain of loggers is defined in run-time through configuration file. - Configuration file is per application. Not the only file for all the users of the library.
Diffstat (limited to 'src/tools/messageserver/messageserver.cpp')
-rw-r--r--src/tools/messageserver/messageserver.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/tools/messageserver/messageserver.cpp b/src/tools/messageserver/messageserver.cpp
index 3b6b439c..0a2f0017 100644
--- a/src/tools/messageserver/messageserver.cpp
+++ b/src/tools/messageserver/messageserver.cpp
@@ -52,6 +52,17 @@
#include <newcountnotifier.h>
#include <qcopserver.h>
+extern "C" {
+#ifndef Q_OS_WIN
+#include <sys/socket.h>
+#endif
+#include <signal.h>
+}
+
+#if defined(Q_OS_UNIX)
+int MessageServer::sighupFd[2];
+#endif
+
MessageServer::MessageServer(QObject *parent)
: QObject(parent),
handler(0),
@@ -63,6 +74,25 @@ MessageServer::MessageServer(QObject *parent)
qMailLog(Messaging) << "MessageServer ctor begin";
new QCopServer(this);
+#if defined(Q_OS_UNIX)
+ // SIGHUP handler. We use the trick described here: http://doc.trolltech.com/4.7-snapshot/unix-signals.html
+ // Looks shocking but the trick has certain reasons stated in Steven's book: http://cr.yp.to/docs/selfpipe.html
+ // Use a socket and notifier because signal handlers can't call Qt code
+
+ if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sighupFd))
+ qFatal("Couldn't create HUP socketpair");
+ snHup = new QSocketNotifier(sighupFd[1], QSocketNotifier::Read, this);
+ connect(snHup, SIGNAL(activated(int)), this, SLOT(handleSigHup()));
+
+ struct sigaction hup;
+ hup.sa_handler = MessageServer::hupSignalHandler;
+ sigemptyset(&hup.sa_mask);
+ hup.sa_flags = 0;
+ hup.sa_flags |= SA_RESTART;
+ if (sigaction(SIGHUP, &hup, 0) > 0)
+ qFatal("Couldn't register HUP handler");
+#endif // defined(Q_OS_UNIX)
+
QMailMessageCountMap::iterator it = messageCounts.begin(), end = messageCounts.end();
for ( ; it != end; ++it)
it.value() = 0;
@@ -428,3 +458,26 @@ void MessageServer::cleanupTemporaryMessages()
{
QMailStore::instance()->removeMessages(QMailMessageKey::status(QMailMessage::Temporary), QMailStore::NoRemovalRecord);
}
+
+#if defined(Q_OS_UNIX)
+
+void MessageServer::hupSignalHandler(int)
+{
+ // Can't call Qt code. Write to the socket and the notifier will fire from the Qt event loop
+ char a = 1;
+ ::write(sighupFd[0], &a, sizeof(a));
+}
+
+void MessageServer::handleSigHup()
+{
+ snHup->setEnabled(false);
+ char tmp;
+ ::read(sighupFd[1], &tmp, sizeof(tmp));
+
+ // This is ~/.config/Nokia/Messageserver.conf
+ qMailLoggersRecreate("Nokia", "Messageserver", "Msgsrv");
+
+ snHup->setEnabled(true);
+}
+
+#endif // defined(Q_OS_UNIX)