signon  8.58
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 class SignonTrace
00038 {
00039 public:
00040     enum LogOutput {
00041         Syslog = 0,
00042         Stdout,
00043     };
00044 
00045     ~SignonTrace()
00046     {
00047         m_pInstance = NULL;
00048         if (m_logOutput == Syslog) {
00049             closelog();
00050         }
00051     }
00052 
00053     static void initialize(LogOutput logOutput)
00054     {
00055         if (m_pInstance)
00056             return;
00057 
00058         m_pInstance = new SignonTrace(logOutput);
00059     }
00060 
00061     static void output(QtMsgType type, const char *msg)
00062     {
00063         if (!m_pInstance)
00064             return;
00065 
00066         if (!criticalsEnabled()) {
00067             if (type <= QtCriticalMsg) return;
00068         } else if (!debugEnabled()) {
00069             if (type <= QtDebugMsg) return;
00070         }
00071 
00072         int priority;
00073         switch (type) {
00074             case QtWarningMsg: priority = LOG_WARNING; break;
00075             case QtCriticalMsg: priority = LOG_CRIT; break;
00076             case QtFatalMsg: priority = LOG_EMERG; break;
00077             case QtDebugMsg:
00078                 /* fall through */
00079             default: priority = LOG_INFO; break;
00080         }
00081 
00082         syslog(priority, "%s", msg);
00083     }
00084 
00085 private:
00086     SignonTrace(LogOutput logOutput):
00087         m_logOutput(logOutput)
00088     {
00089         if (logOutput == Syslog) {
00090             openlog(NULL, LOG_PID, LOG_DAEMON);
00091             qInstallMsgHandler(output);
00092         }
00093     }
00094 
00095     static SignonTrace *m_pInstance;
00096     LogOutput m_logOutput;
00097 };
00098 
00099 SignonTrace *SignonTrace::m_pInstance = 0;
00100 
00101 } //namespace SignOn
00102 
00103 #endif // SIGNONTRACE_H