f49d270da7aea5a59cc3530b45a17c6ca2a3b9db
[quassel.git] / src / common / logger.h
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 #ifndef _LOGGER_H_
22 #define _LOGGER_H_
23
24 #include <QString>
25 #include <QTextStream>
26
27 class Logger {
28   public:
29     enum LogLevel {
30       DebugLevel,
31       InfoLevel,
32       WarningLevel,
33       ErrorLevel
34     };
35
36     inline Logger(LogLevel level) : stream(new Stream(level)) {}
37     ~Logger();
38
39     inline Logger &operator<<(const char* t) { stream->internalStream << QString::fromAscii(t); return *this; }
40     inline Logger &operator<<(QChar t) { stream->internalStream << t; return *this; }
41     inline Logger &operator<<(bool t) { stream->internalStream << (t ? "true" : "false"); return *this; }
42     inline Logger &operator<<(char t) { stream->internalStream << t; return *this; }
43     inline Logger &operator<<(signed short t) { stream->internalStream << t; return *this; }
44     inline Logger &operator<<(unsigned short t) { stream->internalStream << t; return *this; }
45     inline Logger &operator<<(signed int t) { stream->internalStream << t; return *this; }
46     inline Logger &operator<<(unsigned int t) { stream->internalStream << t; return *this; }
47     inline Logger &operator<<(signed long t) { stream->internalStream << t; return *this; }
48     inline Logger &operator<<(unsigned long t) { stream->internalStream << t; return *this; }
49     inline Logger &operator<<(qint64 t) { stream->internalStream << QString::number(t); return *this; }
50     inline Logger &operator<<(quint64 t) { stream->internalStream << QString::number(t); return *this; }
51     inline Logger &operator<<(float t) { stream->internalStream << t; return *this; }
52     inline Logger &operator<<(double t) { stream->internalStream << t; return *this; }
53     inline Logger &operator<<(const QString & t) { stream->internalStream << t; return *this; }
54     inline Logger &operator<<(const QLatin1String &t) { stream->internalStream << t.latin1(); return *this; }
55     inline Logger &operator<<(const QByteArray & t) { stream->internalStream << t ; return *this; }
56     inline Logger &operator<<(const void * t) { stream->internalStream << t; return *this; }
57
58     void log();
59   private:
60     struct Stream {
61       Stream(LogLevel level)
62       : internalStream(&buffer, QIODevice::WriteOnly),
63         logLevel(level) {}
64       QTextStream internalStream;
65       QString buffer;
66       LogLevel logLevel;
67     } *stream;
68 };
69
70 inline Logger quDebug() { return Logger(Logger::DebugLevel); }
71 inline Logger quInfo() { return Logger(Logger::InfoLevel); }
72 inline Logger quWarning() { return Logger(Logger::WarningLevel); }
73 inline Logger quError() { return Logger(Logger::ErrorLevel); }
74
75 #endif