signon  8.41
signontrace.h
Go to the documentation of this file.
1 /*
2  * This file is part of signon
3  *
4  * Copyright (C) 2009-2010 Nokia Corporation.
5  *
6  * Contact: Aurel Popirtac <ext-aurel.popirtac@nokia.com>
7  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * version 2.1 as published by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  */
23 #ifndef SIGNONTRACE_H
24 #define SIGNONTRACE_H
25 
26 #include <syslog.h>
27 
28 #include <QCoreApplication>
29 #include <QFile>
30 #include <QDebug>
31 #include <QDateTime>
32 
33 #include "signond-common.h"
34 
35 namespace SignOn {
36 
37 template <typename T = void>
39 {
40  SignonTrace() {}
41 
42 public:
44  {
45  m_pInstance = NULL;
46  closelog();
47  }
48 
49  static void initialize()
50  {
51  if (m_pInstance)
52  return;
53 
54  m_pInstance = new SignonTrace<T>();
55  openlog(NULL, LOG_PID, LOG_DAEMON);
56  qInstallMsgHandler(output);
57  }
58 
59  static void output(QtMsgType type, const char *msg)
60  {
61  if (!m_pInstance)
62  return;
63 
64  if (!criticalsEnabled()) {
65  if (type <= QtCriticalMsg) return;
66  } else if (!debugEnabled()) {
67  if (type <= QtDebugMsg) return;
68  }
69 
70  int priority;
71  switch (type) {
72  case QtWarningMsg: priority = LOG_WARNING; break;
73  case QtCriticalMsg: priority = LOG_CRIT; break;
74  case QtFatalMsg: priority = LOG_EMERG; break;
75  case QtDebugMsg:
76  /* fall through */
77  default: priority = LOG_INFO; break;
78  }
79 
80  syslog(priority, "%s", msg);
81  }
82 
83 private:
84  static SignonTrace<T> *m_pInstance;
85 };
86 
87 static void initializeTrace() {
89 }
90 
91 template <typename T>
92 SignonTrace<T> *SignonTrace<T>::m_pInstance = 0;
93 
94 } //namespace SignOn
95 
96 #endif // SIGNONTRACE_H