Introduce possibility to use syslog.
[quassel.git] / src / common / logger.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "logger.h"
22 #include "quassel.h"
23
24 #ifdef HAVE_SYSLOG_H
25 #include <syslog.h>
26 #else
27 #include <QFile>
28 #include <QTextStream>
29 #include <QDateTime>
30 #endif
31
32 Logger::~Logger() {
33   QDateTime date = QDateTime::currentDateTime();
34
35   switch (_logLevel) {
36     case DebugLevel:
37       _buffer.prepend("Debug: ");
38       break;
39     case InfoLevel:
40       _buffer.prepend("Info: ");
41       break;
42     case WarningLevel:
43       _buffer.prepend("Warning: ");
44       break;
45     case ErrorLevel:
46       _buffer.prepend("Error: ");
47       break;
48     default:
49       Q_ASSERT(_logLevel); // There should be no unknown log level
50       break;
51   }
52 #ifndef HAVE_SYSLOG_H
53   _buffer.prepend(date.toString("yyyy-MM-dd hh:mm:ss "));
54 #endif
55   log();
56 }
57
58 void Logger::log() {
59   LogLevel lvl;
60   if (Quassel::optionValue("loglevel") == "Debug") lvl = DebugLevel;
61   else if (Quassel::optionValue("loglevel") == "Info") lvl = InfoLevel;
62   else if (Quassel::optionValue("loglevel") == "Warning") lvl = WarningLevel;
63   else if (Quassel::optionValue("loglevel") == "Error") lvl = ErrorLevel;
64   else lvl = InfoLevel;
65
66   if(_logLevel < lvl) return;
67
68 #ifdef HAVE_SYSLOG_H
69   if (Quassel::isOptionSet("syslog")) {
70     int prio;
71     switch (lvl) {
72       case DebugLevel:
73         prio = LOG_DEBUG;
74         break;
75       case InfoLevel:
76         prio = LOG_INFO;
77         break;
78       case WarningLevel:
79         prio = LOG_WARNING;
80         break;
81       case ErrorLevel:
82         prio = LOG_ERR;
83         break;
84       default:
85         prio = LOG_INFO;
86         break;
87     }
88     syslog(LOG_USER & prio, "%s", qPrintable(_buffer));
89   }
90 #else
91   // if we can't open logfile we log to stdout
92   QTextStream out(stdout);
93   QFile file;
94   if(!Quassel::optionValue("logfile").isEmpty()) {
95     file.setFileName(Quassel::optionValue("logfile"));
96     if (file.open(QIODevice::Append | QIODevice::Text)) {
97       out.setDevice(&file);
98       _buffer.remove(QChar('\n'));
99     }
100   }
101   out << _buffer << endl;
102   if(file.isOpen()) file.close();
103 #endif
104 }
105
106
107 void Logger::logMessage(QtMsgType type, const char *msg) {
108   switch (type) {
109   case QtDebugMsg:
110     Logger(Logger::DebugLevel) << msg;
111     break;
112   case QtWarningMsg:
113     Logger(Logger::WarningLevel) << msg;
114     break;
115   case QtCriticalMsg:
116     Logger(Logger::ErrorLevel) << msg;
117     break;
118   case QtFatalMsg:
119     Logger(Logger::ErrorLevel) << msg;
120     Quassel::logFatalMessage(msg);
121     return;
122   }
123 }