00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef TORRENT_LOGGING_HPP
00034 #define TORRENT_LOGGING_HPP
00035
00036 #include <iostream>
00037 #include <fstream>
00038
00039 namespace libtorrent { namespace dht
00040 {
00041
00042 class log
00043 {
00044 public:
00045 log(char const* id, std::ostream& stream)
00046 : m_id(id)
00047 , m_enabled(true)
00048 , m_stream(stream)
00049 {
00050 }
00051
00052 char const* id() const
00053 {
00054 return m_id;
00055 }
00056
00057 bool enabled() const
00058 {
00059 return m_enabled;
00060 }
00061
00062 void enable(bool e)
00063 {
00064 m_enabled = e;
00065 }
00066
00067 void flush() { m_stream.flush(); }
00068
00069 template<class T>
00070 log& operator<<(T const& x)
00071 {
00072 m_stream << x;
00073 return *this;
00074 }
00075
00076 private:
00077 char const* m_id;
00078 bool m_enabled;
00079 std::ostream& m_stream;
00080 };
00081
00082 class log_event
00083 {
00084 public:
00085 log_event(log& log)
00086 : log_(log)
00087 {
00088 if (log_.enabled())
00089 log_ << '[' << log.id() << "] ";
00090 }
00091
00092 ~log_event()
00093 {
00094 if (log_.enabled())
00095 {
00096 log_ << "\n";
00097 log_.flush();
00098 }
00099 }
00100
00101 template<class T>
00102 log_event& operator<<(T const& x)
00103 {
00104 log_ << x;
00105 return *this;
00106 }
00107
00108 operator bool() const
00109 {
00110 return log_.enabled();
00111 }
00112
00113 private:
00114 log& log_;
00115 };
00116
00117 class inverted_log_event : public log_event
00118 {
00119 public:
00120 inverted_log_event(log& log) : log_event(log) {}
00121
00122 operator bool() const
00123 {
00124 return !log_event::operator bool();
00125 }
00126 };
00127
00128 } }
00129
00130 #define TORRENT_DECLARE_LOG(name) \
00131 libtorrent::dht::log& name ## _log()
00132
00133 #define TORRENT_DEFINE_LOG(name) \
00134 libtorrent::dht::log& name ## _log() \
00135 { \
00136 static std::ofstream log_file("dht.log", std::ios::app); \
00137 static libtorrent::dht::log instance(#name, log_file); \
00138 return instance; \
00139 }
00140
00141 #define TORRENT_LOG(name) \
00142 if (libtorrent::dht::inverted_log_event event_object__ = name ## _log()); \
00143 else static_cast<log_event&>(event_object__)
00144
00145 #endif
00146