Vidalia  0.3.1
tcglobal.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 tcglobal.h
13 ** \brief Provides common methods and constants used by the torcontrol library
14 */
15 
16 #ifndef _TCGLOBAL_H
17 #define _TCGLOBAL_H
18 
19 #include <QString>
20 #include <QMetaType>
21 
22 namespace tc {
23  /** Helper class to handle formatting log messages with arguments. */
24  class DebugMessage {
25  struct Stream {
26  Stream(QtMsgType t, const QString &fmt)
27  : type(t), buf(fmt), ref(1) {}
28  QtMsgType type; /**< DebugMessage severity level. */
29  QString buf; /**< Log message buffer. */
30  int ref; /**< Reference counter. */
31  } *stream;
32 
33  public:
34  /** Constructs a new DebugMessage with severity <b>t</b> and the message
35  * format <b>fmt</b>. */
36  inline DebugMessage(QtMsgType t, const QString &fmt)
37  : stream(new Stream(t, fmt)) {}
38  inline DebugMessage(const DebugMessage &o)
39  : stream(o.stream) { ++stream->ref; }
40  virtual ~DebugMessage() {
41  if (!--stream->ref) {
42  stream->buf.prepend("torcontrol: ");
43  qt_message_output(stream->type, qPrintable(stream->buf));
44  delete stream;
45  }
46  }
47 
48  inline DebugMessage arg(const QString &a)
49  { stream->buf = stream->buf.arg(a); return *this; }
50  inline DebugMessage arg(int a)
51  { stream->buf = stream->buf.arg(a); return *this; }
52  };
53 }
54 
55 namespace tc {
67  };
68  /** Severity values used in log message and status events. */
69  enum Severity {
70  UnrecognizedSeverity = 0, /**< An unrecognized severity value. */
71  DebugSeverity = (1u<<4), /**< Hyper-verbose events used for debugging. */
72  InfoSeverity = (1u<<3), /**< Verbose events that can occur frequently. */
73  NoticeSeverity = (1u<<2), /**< A not-so-bad event. */
74  WarnSeverity = (1u<<1), /**< An important, but non-fatal event. */
75  ErrorSeverity = (1u<<0) /**< A critical event. */
76  };
77  /** SOCKS error types used by Tor status event notifications. These are
78  * emitted in the TorControl::socksError() signal. */
79  enum SocksError {
80  DangerousSocksTypeError, /**< The SOCKS type uses only IP addresses. */
81  UnknownSocksProtocolError, /**< Unknown SOCKS protocol type. */
82  BadSocksHostnameError /**< Application provided an invalid hostname. */
83  };
84  /** Reasons that use of the user's current Tor version would be
85  * discouraged. */
90  };
91 
92  /** Converts <b>str</b> to a Severity enum value. */
93  Severity severityFromString(const QString &str);
94 
95  /** Converts <b>str</b> to a ConnectionStatusReason enum value. */
97 
98  /** Creates a new message using <b>fmt</b> and a severity level of
99  * QtDebugMsg. */
100  DebugMessage debug(const QString &fmt);
101 
102  /** Creates a new message using <b>fmt</b> and a severity level of
103  * QtWarningMsg. */
104  DebugMessage warn(const QString &fmt);
105 
106  /** Creates a new message using <b>fmt</b> and a severity level of
107  * QtCriticalMsg. */
108  DebugMessage error(const QString &fmt);
109 
110  /** Creates a new message using <b>fmt</b> and a severity level of
111  * QtFatalMsg. */
112  DebugMessage fatal(const QString &fmt);
113 }
114 
118 
119 #endif
120 
tc::DebugMessage::Stream::Stream
Stream(QtMsgType t, const QString &fmt)
Definition: tcglobal.h:26
tc::DebugMessage::arg
DebugMessage arg(int a)
Definition: tcglobal.h:50
tc::DebugMessage::DebugMessage
DebugMessage(QtMsgType t, const QString &fmt)
Definition: tcglobal.h:36
tc::WarnSeverity
@ WarnSeverity
Definition: tcglobal.h:74
tc::ResourceLimitReached
@ ResourceLimitReached
Definition: tcglobal.h:66
tc::DebugMessage::Stream::type
QtMsgType type
Definition: tcglobal.h:28
tc::UnrecognizedReason
@ UnrecognizedReason
Definition: tcglobal.h:57
tc::BadSocksHostnameError
@ BadSocksHostnameError
Definition: tcglobal.h:82
tc::ConnectionStatusReason
ConnectionStatusReason
Definition: tcglobal.h:56
tc::DebugMessage::DebugMessage
DebugMessage(const DebugMessage &o)
Definition: tcglobal.h:38
tc::InfoSeverity
@ InfoSeverity
Definition: tcglobal.h:72
tc::NoticeSeverity
@ NoticeSeverity
Definition: tcglobal.h:73
tc::Severity
Severity
Definition: tcglobal.h:69
tc::DebugMessage::Stream::buf
QString buf
Definition: tcglobal.h:29
tc::ConnectionReset
@ ConnectionReset
Definition: tcglobal.h:62
tc::ConnectionDone
@ ConnectionDone
Definition: tcglobal.h:60
tc::NewTorVersion
@ NewTorVersion
Definition: tcglobal.h:89
tc::warn
DebugMessage warn(const QString &fmt)
Definition: tcglobal.cpp:32
tc::ObsoleteTorVersion
@ ObsoleteTorVersion
Definition: tcglobal.h:87
tc::DebugMessage::arg
DebugMessage arg(const QString &a)
Definition: tcglobal.h:48
tc::IdentityMismatch
@ IdentityMismatch
Definition: tcglobal.h:59
tc::debug
DebugMessage debug(const QString &fmt)
Definition: tcglobal.cpp:24
tc
Definition: tcglobal.cpp:19
tc::DebugMessage::Stream
Definition: tcglobal.h:25
tc::MiscellaneousReason
@ MiscellaneousReason
Definition: tcglobal.h:58
tc::ConnectionRefused
@ ConnectionRefused
Definition: tcglobal.h:61
tc::severityFromString
Severity severityFromString(const QString &str)
Definition: tcglobal.cpp:82
tc::ErrorSeverity
@ ErrorSeverity
Definition: tcglobal.h:75
tc::UnrecognizedSeverity
@ UnrecognizedSeverity
Definition: tcglobal.h:70
tc::ConnectionTimeout
@ ConnectionTimeout
Definition: tcglobal.h:63
tc::connectionStatusReasonFromString
ConnectionStatusReason connectionStatusReasonFromString(const QString &str)
Definition: tcglobal.cpp:55
tc::NoRouteToHost
@ NoRouteToHost
Definition: tcglobal.h:65
tc::TorVersionStatus
TorVersionStatus
Definition: tcglobal.h:86
tc::UnknownSocksProtocolError
@ UnknownSocksProtocolError
Definition: tcglobal.h:81
tc::DebugMessage::stream
struct tc::DebugMessage::Stream * stream
tc::DebugSeverity
@ DebugSeverity
Definition: tcglobal.h:71
tc::fatal
DebugMessage fatal(const QString &fmt)
Definition: tcglobal.cpp:48
tc::UnrecommendedTorVersion
@ UnrecommendedTorVersion
Definition: tcglobal.h:88
tc::DebugMessage
Definition: tcglobal.h:24
tc::error
DebugMessage error(const QString &fmt)
Definition: tcglobal.cpp:40
tc::ConnectionIoError
@ ConnectionIoError
Definition: tcglobal.h:64
tc::DebugMessage::~DebugMessage
virtual ~DebugMessage()
Definition: tcglobal.h:40
tc::DebugMessage::Stream::ref
int ref
Definition: tcglobal.h:30
tc::SocksError
SocksError
Definition: tcglobal.h:79
tc::DangerousSocksTypeError
@ DangerousSocksTypeError
Definition: tcglobal.h:80
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(BootstrapStatus)