Added support for various additional datatypes.
[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 "types.h"
25
26 #include <QString>
27 #include <QStringList>
28 #include <QTextStream>
29
30 class Logger {
31   public:
32     enum LogLevel {
33       DebugLevel,
34       InfoLevel,
35       WarningLevel,
36       ErrorLevel
37     };
38
39     inline Logger(LogLevel level) : stream(new Stream(level)) {}
40     ~Logger();
41
42     inline Logger &operator<<(const char* t) { stream->internalStream << QString::fromAscii(t); return *this; }
43     inline Logger &operator<<(QChar t) { stream->internalStream << t; return *this; }
44     inline Logger &operator<<(bool t) { stream->internalStream << (t ? "true" : "false"); return *this; }
45     inline Logger &operator<<(char t) { stream->internalStream << t; return *this; }
46     inline Logger &operator<<(signed short t) { stream->internalStream << t; return *this; }
47     inline Logger &operator<<(unsigned short t) { stream->internalStream << t; return *this; }
48     inline Logger &operator<<(signed int t) { stream->internalStream << t; return *this; }
49     inline Logger &operator<<(unsigned int t) { stream->internalStream << t; return *this; }
50     inline Logger &operator<<(signed long t) { stream->internalStream << t; return *this; }
51     inline Logger &operator<<(unsigned long t) { stream->internalStream << t; return *this; }
52     inline Logger &operator<<(qint64 t) { stream->internalStream << QString::number(t); return *this; }
53     inline Logger &operator<<(quint64 t) { stream->internalStream << QString::number(t); return *this; }
54     inline Logger &operator<<(float t) { stream->internalStream << t; return *this; }
55     inline Logger &operator<<(double t) { stream->internalStream << t; return *this; }
56     inline Logger &operator<<(const QString & t) { stream->internalStream << t; return *this; }
57     inline Logger &operator<<(const QLatin1String &t) { stream->internalStream << t.latin1(); return *this; }
58     inline Logger &operator<<(const QByteArray & t) { stream->internalStream << t ; return *this; }
59     inline Logger &operator<<(const void * t) { stream->internalStream << t; return *this; }
60     inline Logger &operator<<(const QStringList & t) { stream->internalStream << t.join(" "); return *this; }
61     inline Logger &operator<<(const BufferId & t) { stream->internalStream << QVariant::fromValue(t).toInt(); return *this; }
62     inline Logger &operator<<(const NetworkId & t) { stream->internalStream << QVariant::fromValue(t).toInt(); return *this; }
63     inline Logger &operator<<(const UserId & t) { stream->internalStream << QVariant::fromValue(t).toInt(); return *this; }
64     inline Logger &operator<<(const MsgId & t) { stream->internalStream << QVariant::fromValue(t).toInt(); return *this; }
65     inline Logger &operator<<(const IdentityId & t) { stream->internalStream << QVariant::fromValue(t).toInt(); return *this; }
66     inline Logger &operator<<(const AccountId & t) { stream->internalStream << QVariant::fromValue(t).toInt(); return *this; }
67
68     void log();
69   private:
70     struct Stream {
71       Stream(LogLevel level)
72       : internalStream(&buffer, QIODevice::WriteOnly),
73         logLevel(level) {}
74       QTextStream internalStream;
75       QString buffer;
76       LogLevel logLevel;
77     } *stream;
78 };
79
80 inline Logger quDebug() { return Logger(Logger::DebugLevel); }
81 inline Logger quInfo() { return Logger(Logger::InfoLevel); }
82 inline Logger quWarning() { return Logger(Logger::WarningLevel); }
83 inline Logger quError() { return Logger(Logger::ErrorLevel); }
84
85 #endif