signon  8.42
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 
38 {
39 public:
40  enum LogOutput {
41  Syslog = 0,
43  };
44 
46  {
47  m_pInstance = NULL;
48  if (m_logOutput == Syslog) {
49  closelog();
50  }
51  }
52 
53  static void initialize(LogOutput logOutput)
54  {
55  if (m_pInstance)
56  return;
57 
58  m_pInstance = new SignonTrace(logOutput);
59  }
60 
61  static void output(QtMsgType type, const char *msg)
62  {
63  if (!m_pInstance)
64  return;
65 
66  if (!criticalsEnabled()) {
67  if (type <= QtCriticalMsg) return;
68  } else if (!debugEnabled()) {
69  if (type <= QtDebugMsg) return;
70  }
71 
72  int priority;
73  switch (type) {
74  case QtWarningMsg: priority = LOG_WARNING; break;
75  case QtCriticalMsg: priority = LOG_CRIT; break;
76  case QtFatalMsg: priority = LOG_EMERG; break;
77  case QtDebugMsg:
78  /* fall through */
79  default: priority = LOG_INFO; break;
80  }
81 
82  syslog(priority, "%s", msg);
83  }
84 
85 private:
86  SignonTrace(LogOutput logOutput):
87  m_logOutput(logOutput)
88  {
89  if (logOutput == Syslog) {
90  openlog(NULL, LOG_PID, LOG_DAEMON);
91  qInstallMsgHandler(output);
92  }
93  }
94 
95  static SignonTrace *m_pInstance;
96  LogOutput m_logOutput;
97 };
98 
99 SignonTrace *SignonTrace::m_pInstance = 0;
100 
101 } //namespace SignOn
102 
103 #endif // SIGNONTRACE_H