Vidalia  0.3.1
Log.h
Go to the documentation of this file.
1 /*
2 ** This file is part of Vidalia, and is subject to the license terms in the
3 ** LICENSE file, found in the top level directory of this distribution. If you
4 ** did not receive the LICENSE file with this file, you may obtain it from the
5 ** Vidalia source package distributed by the Vidalia Project at
6 ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
7 ** including this file, may be copied, modified, propagated, or distributed
8 ** except according to the terms described in the LICENSE file.
9 */
10 
11 /*
12 ** \file Log.h
13 ** \brief Debug message logging
14 */
15 
16 #ifndef _LOG_H
17 #define _LOG_H
18 
19 #include <QObject>
20 #include <QFile>
21 #include <QStringList>
22 #include <QIODevice>
23 #include <QHostAddress>
24 
25 
26 /** The Log class is similar to the QDebug class provided with Qt, but with
27  * finer-grained logging levels, slightly different output (for example, not
28  * everything is wrapped in double quotes), supports using .arg(), and can
29  * still be used even if Qt was compiled with QT_NO_DEBUG_STREAM. */
30 class Log
31 {
32 public:
33  /** Logging severity levels. */
34  enum LogLevel {
35  Debug = 0, /**< Verbose debugging output. */
36  Info, /**< Primarily program flow output. */
37  Notice, /**< Non-failure (but important) events. */
38  Warn, /**< Recoverable failure conditions. */
39  Error, /**< Critical, non-recoverable errors. */
40  Off, /**< No logging output. */
41  Unknown /**< Unknown/invalid log level. */
42  };
43  class LogMessage;
44 
45  /** Default constructor. */
46  Log();
47  /** Destructor. */
48  ~Log();
49 
50  /** Opens a file on disk (or stdout or stderr) to which log messages will be
51  * written. */
52  bool open(FILE *file);
53  /** Opens a file on disk to which log messages will be written. */
54  bool open(QString file);
55  /** Closes the log file. */
56  void close();
57  /** Returns true if the log file is open and ready for writing. */
58  bool isOpen() { return _logFile.isOpen() && _logFile.isWritable(); }
59  /** Returns a string description of the last file error encountered. */
60  QString errorString() { return _logFile.errorString(); }
61 
62  /** Sets the current log level to <b>level</b>. */
63  void setLogLevel(LogLevel level);
64  /** Returns a list of strings representing valid log levels. */
65  static QStringList logLevels();
66  /** Returns a string description of the given LogLevel <b>level</b>. */
67  static inline QString logLevelToString(LogLevel level);
68  /** Returns a LogLevel for the level given by <b>str</b>. */
69  static LogLevel stringToLogLevel(QString str);
70 
71  /** Creates a log message with severity <b>level</b> and initial message
72  * contents <b>message</b>. The log message can be appended to until the
73  * returned LogMessage's destructor is called, at which point the complete
74  * message is written to the log file. */
75  LogMessage log(LogLevel level, QString message);
76  /** Creates a log message with severity <b>level</b>. The log message can be
77  * appended to until the returned LogMessage's destructor is called, at
78  * which point the complete message is written to the log file. */
79  inline LogMessage log(LogLevel level);
80 
81 private:
82  LogLevel _logLevel; /**< Minimum log severity level. */
83  QFile _logFile; /**< Log output destination. */
84 };
85 
86 /** This internal class represents a single message that is to be written to
87  * the log destination. The message is buffered until it is written to the
88  * log in this class's destructor. */
90 {
91 public:
92  struct Stream {
93  Stream(Log::LogLevel t, QIODevice *o)
94  : type(t), out(o), ref(1) {}
96  QIODevice *out;
97  int ref;
98  QString buf;
99  } *stream;
100 
101  inline LogMessage(Log::LogLevel t, QIODevice *o)
102  : stream(new Stream(t,o)) {}
103  inline LogMessage(const LogMessage &o)
104  : stream(o.stream) { ++stream->ref; }
105  inline QString toString() const;
106  ~LogMessage();
107 
108  /* Support both the << and .arg() methods */
109  inline LogMessage &operator<<(const QString &t)
110  { stream->buf += t; return *this; }
111  inline LogMessage arg(const QString &a)
112  { stream->buf = stream->buf.arg(a); return *this; }
113  inline LogMessage &operator<<(const QStringList &a)
114  { stream->buf += a.join(","); return *this; }
115  inline LogMessage arg(const QStringList &a)
116  { stream->buf = stream->buf.arg(a.join(",")); return *this; }
117  inline LogMessage &operator<<(const QHostAddress &a)
118  { stream->buf += a.toString(); return *this; }
119  inline LogMessage arg(const QHostAddress &a)
120  { stream->buf = stream->buf.arg(a.toString()); return *this; }
121  inline LogMessage &operator<<(short a)
122  { stream->buf += QString::number(a); return *this; }
123  inline LogMessage arg(short a)
124  { stream->buf = stream->buf.arg(a); return *this; }
125  inline LogMessage &operator<<(ushort a)
126  { stream->buf += QString::number(a); return *this; }
127  inline LogMessage arg(ushort a)
128  { stream->buf = stream->buf.arg(a); return *this; }
129  inline LogMessage &operator<<(int a)
130  { stream->buf += QString::number(a); return *this; }
131  inline LogMessage arg(int a)
132  { stream->buf = stream->buf.arg(a); return *this; }
133  inline LogMessage &operator<<(uint a)
134  { stream->buf += QString::number(a); return *this; }
135  inline LogMessage arg(uint a)
136  { stream->buf = stream->buf.arg(a); return *this; }
137  inline LogMessage &operator<<(long a)
138  { stream->buf += QString::number(a); return *this; }
139  inline LogMessage arg(long a)
140  { stream->buf = stream->buf.arg(a); return *this; }
141  inline LogMessage &operator<<(ulong a)
142  { stream->buf += QString::number(a); return *this; }
143  inline LogMessage arg(ulong a)
144  { stream->buf = stream->buf.arg(a); return *this; }
145  inline LogMessage &operator<<(qlonglong a)
146  { stream->buf += QString::number(a); return *this; }
147  inline LogMessage arg(qlonglong a)
148  { stream->buf = stream->buf.arg(a); return *this; }
149  inline LogMessage &operator<<(qulonglong a)
150  { stream->buf += QString::number(a); return *this; }
151  inline LogMessage arg(qulonglong a)
152  { stream->buf = stream->buf.arg(a); return *this; }
153 };
154 
155 #endif
156 
struct Log::LogMessage::Stream * stream
LogMessage arg(const QStringList &a)
Definition: Log.h:115
bool isOpen()
Definition: Log.h:58
Stream(Log::LogLevel t, QIODevice *o)
Definition: Log.h:93
static QStringList logLevels()
Definition: Log.cpp:42
LogMessage & operator<<(qlonglong a)
Definition: Log.h:145
LogMessage arg(ushort a)
Definition: Log.h:127
LogMessage(const LogMessage &o)
Definition: Log.h:103
void close()
Definition: Log.cpp:85
static QString logLevelToString(LogLevel level)
Definition: Log.cpp:116
LogMessage & operator<<(const QString &t)
Definition: Log.h:109
LogMessage & operator<<(int a)
Definition: Log.h:129
Log::LogLevel type
Definition: Log.h:95
LogMessage & operator<<(ushort a)
Definition: Log.h:125
LogLevel
Definition: Log.h:34
Log()
Definition: Log.cpp:29
Definition: Log.h:35
LogMessage arg(int a)
Definition: Log.h:131
LogMessage arg(uint a)
Definition: Log.h:135
void setLogLevel(LogLevel level)
Definition: Log.cpp:52
LogMessage(Log::LogLevel t, QIODevice *o)
Definition: Log.h:101
QString toString() const
Definition: Log.cpp:153
LogMessage & operator<<(long a)
Definition: Log.h:137
LogLevel _logLevel
Definition: Log.h:82
LogMessage & operator<<(ulong a)
Definition: Log.h:141
Definition: Log.h:36
LogMessage arg(short a)
Definition: Log.h:123
LogMessage & operator<<(const QStringList &a)
Definition: Log.h:113
LogMessage arg(ulong a)
Definition: Log.h:143
LogMessage arg(const QHostAddress &a)
Definition: Log.h:119
QFile _logFile
Definition: Log.h:83
Definition: Log.h:39
bool open(FILE *file)
Definition: Log.cpp:62
LogMessage & operator<<(uint a)
Definition: Log.h:133
QString errorString()
Definition: Log.h:60
~Log()
Definition: Log.cpp:35
LogMessage & operator<<(short a)
Definition: Log.h:121
LogMessage arg(const QString &a)
Definition: Log.h:111
LogMessage & operator<<(qulonglong a)
Definition: Log.h:149
Definition: Log.h:30
LogMessage arg(qulonglong a)
Definition: Log.h:151
static LogLevel stringToLogLevel(QString str)
Definition: Log.cpp:132
LogMessage arg(qlonglong a)
Definition: Log.h:147
LogMessage & operator<<(const QHostAddress &a)
Definition: Log.h:117
Definition: Log.h:38
Definition: Log.h:40
QIODevice * out
Definition: Log.h:96
LogMessage log(LogLevel level, QString message)
Definition: Log.cpp:109
LogMessage arg(long a)
Definition: Log.h:139