Quassel IRC  Pre-Release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
logger.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2015 by the Quassel Project *
3  * devel@quassel-irc.org *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) version 3. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #include <QFile>
22 #include <QTextStream>
23 #include <QDateTime>
24 
25 #ifdef HAVE_SYSLOG
26 # include <syslog.h>
27 #endif
28 
29 #include "logger.h"
30 #include "quassel.h"
31 
33 {
34  log();
35 }
36 
37 
39 {
41  return;
42 
43  switch (_logLevel) {
45  _buffer.prepend("Debug: ");
46  break;
47  case Quassel::InfoLevel:
48  _buffer.prepend("Info: ");
49  break;
51  _buffer.prepend("Warning: ");
52  break;
54  _buffer.prepend("Error: ");
55  break;
56  default:
57  break;
58  }
59 
60 #ifdef HAVE_SYSLOG
61  if (Quassel::logToSyslog()) {
62  int prio;
63  switch (_logLevel) {
65  prio = LOG_DEBUG;
66  break;
67  case Quassel::InfoLevel:
68  prio = LOG_INFO;
69  break;
71  prio = LOG_WARNING;
72  break;
74  prio = LOG_ERR;
75  break;
76  default:
77  prio = LOG_INFO;
78  break;
79  }
80  syslog(prio|LOG_USER, "%s", qPrintable(_buffer));
81  }
82 #endif
83 
84  // if we neither use syslog nor have a logfile we log to stdout
85 
87  _buffer.prepend(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss "));
88 
89  QTextStream out(stdout);
90  if (Quassel::logFile() && Quassel::logFile()->isOpen()) {
91  _buffer.remove(QChar('\n'));
92  out.setDevice(Quassel::logFile());
93  }
94 
95  out << _buffer << endl;
96  }
97 }
98 
99 
100 #if QT_VERSION < 0x050000
101 void Logger::logMessage(QtMsgType type, const char *msg)
102 {
103  switch (type) {
104  case QtDebugMsg:
105  Logger(Quassel::DebugLevel) << msg;
106  break;
107  case QtWarningMsg:
109  break;
110  case QtCriticalMsg:
111  Logger(Quassel::ErrorLevel) << msg;
112  break;
113  case QtFatalMsg:
114  Logger(Quassel::ErrorLevel) << msg;
116  return;
117  }
118 }
119 #else
120 void Logger::logMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
121 {
122  Q_UNUSED(context)
123 
124  switch (type) {
125  case QtDebugMsg:
126  Logger(Quassel::DebugLevel) << msg.toLocal8Bit().constData();
127  break;
128  case QtWarningMsg:
129  Logger(Quassel::WarningLevel) << msg.toLocal8Bit().constData();
130  break;
131  case QtCriticalMsg:
132  Logger(Quassel::ErrorLevel) << msg.toLocal8Bit().constData();
133  break;
134  case QtFatalMsg:
135  Logger(Quassel::ErrorLevel) << msg.toLocal8Bit().constData();
136  Quassel::logFatalMessage(msg.toLocal8Bit().constData());
137  return;
138  }
139 }
140 #endif