Log.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _LOG_H
00018 #define _LOG_H
00019
00020 #include <QObject>
00021 #include <QFile>
00022 #include <QStringList>
00023 #include <QIODevice>
00024 #include <QHostAddress>
00025
00026
00027
00028
00029
00030
00031 class Log
00032 {
00033 public:
00034
00035 enum LogLevel {
00036 Debug = 0,
00037 Info,
00038 Notice,
00039 Warn,
00040 Error,
00041 Off,
00042 Unknown
00043 };
00044 class LogMessage;
00045
00046
00047 Log();
00048
00049 ~Log();
00050
00051
00052
00053 bool open(FILE *file);
00054
00055 bool open(QString file);
00056
00057 void close();
00058
00059 bool isOpen() { return _logFile.isOpen() && _logFile.isWritable(); }
00060
00061 QString errorString() { return _logFile.errorString(); }
00062
00063
00064 void setLogLevel(LogLevel level);
00065
00066 static QStringList logLevels();
00067
00068 static inline QString logLevelToString(LogLevel level);
00069
00070 static LogLevel stringToLogLevel(QString str);
00071
00072
00073
00074
00075
00076 LogMessage log(LogLevel level, QString message);
00077
00078
00079
00080 inline LogMessage log(LogLevel level);
00081
00082 private:
00083 LogLevel _logLevel;
00084 QFile _logFile;
00085 };
00086
00087
00088
00089
00090 class Log::LogMessage
00091 {
00092 public:
00093 struct Stream {
00094 Stream(Log::LogLevel t, QIODevice *o)
00095 : type(t), out(o), ref(1) {}
00096 Log::LogLevel type;
00097 QIODevice *out;
00098 int ref;
00099 QString buf;
00100 } *stream;
00101
00102 inline LogMessage(Log::LogLevel t, QIODevice *o)
00103 : stream(new Stream(t,o)) {}
00104 inline LogMessage(const LogMessage &o)
00105 : stream(o.stream) { ++stream->ref; }
00106 inline QString toString() const;
00107 ~LogMessage();
00108
00109
00110 inline LogMessage &operator<<(const QString &t)
00111 { stream->buf += t; return *this; }
00112 inline LogMessage arg(const QString &a)
00113 { stream->buf = stream->buf.arg(a); return *this; }
00114 inline LogMessage &operator<<(const QStringList &a)
00115 { stream->buf += a.join(","); return *this; }
00116 inline LogMessage arg(const QStringList &a)
00117 { stream->buf = stream->buf.arg(a.join(",")); return *this; }
00118 inline LogMessage &operator<<(const QHostAddress &a)
00119 { stream->buf += a.toString(); return *this; }
00120 inline LogMessage arg(const QHostAddress &a)
00121 { stream->buf = stream->buf.arg(a.toString()); return *this; }
00122 inline LogMessage &operator<<(short a)
00123 { stream->buf += QString::number(a); return *this; }
00124 inline LogMessage arg(short a)
00125 { stream->buf = stream->buf.arg(a); return *this; }
00126 inline LogMessage &operator<<(ushort a)
00127 { stream->buf += QString::number(a); return *this; }
00128 inline LogMessage arg(ushort a)
00129 { stream->buf = stream->buf.arg(a); return *this; }
00130 inline LogMessage &operator<<(int a)
00131 { stream->buf += QString::number(a); return *this; }
00132 inline LogMessage arg(int a)
00133 { stream->buf = stream->buf.arg(a); return *this; }
00134 inline LogMessage &operator<<(uint a)
00135 { stream->buf += QString::number(a); return *this; }
00136 inline LogMessage arg(uint a)
00137 { stream->buf = stream->buf.arg(a); return *this; }
00138 inline LogMessage &operator<<(long a)
00139 { stream->buf += QString::number(a); return *this; }
00140 inline LogMessage arg(long a)
00141 { stream->buf = stream->buf.arg(a); return *this; }
00142 inline LogMessage &operator<<(ulong a)
00143 { stream->buf += QString::number(a); return *this; }
00144 inline LogMessage arg(ulong a)
00145 { stream->buf = stream->buf.arg(a); return *this; }
00146 inline LogMessage &operator<<(qlonglong a)
00147 { stream->buf += QString::number(a); return *this; }
00148 inline LogMessage arg(qlonglong a)
00149 { stream->buf = stream->buf.arg(a); return *this; }
00150 inline LogMessage &operator<<(qulonglong a)
00151 { stream->buf += QString::number(a); return *this; }
00152 inline LogMessage arg(qulonglong a)
00153 { stream->buf = stream->buf.arg(a); return *this; }
00154 };
00155
00156 #endif
00157