signon  8.40
signontrace.h
Go to the documentation of this file.
00001 /*
00002  * This file is part of signon
00003  *
00004  * Copyright (C) 2009-2010 Nokia Corporation.
00005  *
00006  * Contact: Aurel Popirtac <ext-aurel.popirtac@nokia.com>
00007  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public License
00011  * version 2.1 as published by the Free Software Foundation.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  */
00023 #ifndef SIGNONTRACE_H
00024 #define SIGNONTRACE_H
00025 
00026 #include <syslog.h>
00027 
00028 #include <QCoreApplication>
00029 #include <QFile>
00030 #include <QDebug>
00031 #include <QDateTime>
00032 
00033 #include "signond-common.h"
00034 
00035 namespace SignOn {
00036 
00037 template <typename T = void>
00038 class SignonTrace
00039 {
00040     SignonTrace() {}
00041 
00042 public:
00043     ~SignonTrace()
00044     {
00045         m_pInstance = NULL;
00046         closelog();
00047     }
00048 
00049     static void initialize()
00050     {
00051         if (m_pInstance)
00052             return;
00053 
00054         m_pInstance = new SignonTrace<T>();
00055         openlog(NULL, LOG_PID, LOG_DAEMON);
00056         qInstallMsgHandler(output);
00057     }
00058 
00059     static void output(QtMsgType type, const char *msg)
00060     {
00061         if (!m_pInstance)
00062             return;
00063 
00064         if (!criticalsEnabled()) {
00065             if (type <= QtCriticalMsg) return;
00066         } else if (!debugEnabled()) {
00067             if (type <= QtDebugMsg) return;
00068         }
00069 
00070         int priority;
00071         switch (type) {
00072             case QtWarningMsg: priority = LOG_WARNING; break;
00073             case QtCriticalMsg: priority = LOG_CRIT; break;
00074             case QtFatalMsg: priority = LOG_EMERG; break;
00075             case QtDebugMsg:
00076                 /* fall through */
00077             default: priority = LOG_INFO; break;
00078         }
00079 
00080         syslog(priority, "%s", msg);
00081      }
00082 
00083 private:
00084     static SignonTrace<T> *m_pInstance;
00085 };
00086 
00087 static void initializeTrace() {
00088     SignonTrace<>::initialize();
00089 }
00090 
00091 template <typename T>
00092 SignonTrace<T> *SignonTrace<T>::m_pInstance = 0;
00093 
00094 } //namespace SignOn
00095 
00096 #endif // SIGNONTRACE_H