Upstream tarball 20080512
[amule.git] / src / Logger.h
blob844f18537a75af1e61581ce6b2291499fad4bdb5
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (C) 2005-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 //
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
8 // respective authors.
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #ifndef LOGGER_H
26 #define LOGGER_H
28 #include <wx/log.h>
29 #include <wx/event.h>
32 enum DebugType
34 //! General warnings/errors.
35 logGeneral = 0,
36 //! Warnings/Errors for the main hashing thread.
37 logHasher,
38 //! Warnings/Errors for client-objects.
39 logClient,
40 //! Warnings/Errors for the local client protocol.
41 logLocalClient,
42 //! Warnings/Errors for the remote client protocol.
43 logRemoteClient,
44 //! Warnings/Errors when parsing packets.
45 logPacketErrors,
46 //! Warnings/Errors for the CFile class.
47 logCFile,
48 //! Warnings/Errors related to reading/writing files.
49 logFileIO,
50 //! Warnings/Errors when using the zLib library.
51 logZLib,
52 //! Warnings/Errors for the AICH-syncronization thread.
53 logAICHThread,
54 //! Warnings/Errors for transfering AICH hash-sets.
55 logAICHTransfer,
56 //! Warnings/Errors when recovering with AICH.
57 logAICHRecovery,
58 //! Warnings/Errors for the CListenSocket class.
59 logListenSocket,
60 //! Warnings/Errors for Client-Credits.
61 logCredits,
62 //! Warnings/Errors for the client UDP socket.
63 logClientUDP,
64 //! Warnings/Errors for the download-queue.
65 logDownloadQueue,
66 //! Warnings/Errors for the IP-Filter.
67 logIPFilter,
68 //! Warnings/Errors for known-files.
69 logKnownFiles,
70 //! Warnings/Errors for part-files.
71 logPartFile,
72 //! Warnings/Errors for SHA-hashset creation.
73 logSHAHashSet,
74 //! Warnings/Errors for servers, server connections.
75 logServer,
76 //! Warnings/Errors for proxy.
77 logProxy,
78 //! Warnings/Errors related to searching.
79 logSearch,
80 //! Warnings/Errors related to the server UDP socket.
81 logServerUDP,
82 //! Warning/Errors related to Kademlia UDP comunication on client
83 logClientKadUDP,
84 //! Warning/Errors related to Kademlia Search
85 logKadSearch,
86 //! Warning/Errors related to Kademlia Routing
87 logKadRouting,
88 //! Warning/Errors related to Kademlia Indexing
89 logKadIndex,
90 //! Warning/Errors related to Kademlia Main Thread
91 logKadMain,
92 //! Warning/Errors related to Kademlia Preferences
93 logKadPrefs,
94 //! Warnings/Errors related to partfile importer
95 logPfConvert,
96 //! Warnings/Errors related to the basic UDP socket-class.
97 logMuleUDP,
98 //! Warnings/Errors related to the thread-scheduler.
99 logThreads,
100 //! Warnings/Errors related to the Universal Plug and Play subsystem.
101 logUPnP,
102 //! Warnings/Errors related to the UDP Firewall Tester
103 logKadUdpFwTester,
104 //! Warnings/Errors related to Kad packet tracking.
105 logKadPacketTracking,
106 //! Warnings/Errors related to Kad entry tracking.
107 logKadEntryTracking
108 // IMPORTANT NOTE: when you add values to this enum, update the g_debugcats
109 // array in Logger.cpp!
115 * Container-class for the debugging categories.
117 class CDebugCategory
119 public:
121 * Constructor.
123 * @param type The actual debug-category type.
124 * @param name The user-readable name.
126 CDebugCategory( DebugType type, const wxString& name );
130 * Returns true if the category is enabled.
132 bool IsEnabled() const;
135 * Enables/Disables the category.
137 void SetEnabled( bool );
141 * Returns the user-readable name.
143 const wxString& GetName() const;
146 * Returns the actual type.
148 DebugType GetType() const;
150 private:
151 //! The user-readable name.
152 wxString m_name;
153 //! The actual type.
154 DebugType m_type;
155 //! Whenever or not the category is enabled.
156 bool m_enabled;
161 * Namespace containing functions for logging operations.
163 namespace CLogger
166 * Returns true if debug-messages should be generated for a specific category.
168 bool IsEnabled( DebugType );
171 * Enables or disables debug-messages for a specific category.
173 void SetEnabled( DebugType type, bool enabled );
177 * Logs the specified line of text.
179 * @param file
180 * @param line
181 * @param critical If true, then the message will be made visible directly to the user.
182 * @param str The actual line of text.
184 * This function is thread-safe. If it is called by the main thread, the
185 * event will be sent directly to the application, otherwise it will be
186 * queued in the event-loop.
188 void AddLogLine(
189 const wxString &file,
190 int line,
191 bool critical,
192 const wxString &str);
195 * Logs the specified line of text, prefixed with the name of the DebugType.
197 * @param file
198 * @param line
199 * @param critical If true, then the message will be made visible directly to the user.
200 * @param type The debug-category, the name of which will be prepended to the line.
201 * @param str The actual line of text.
203 * This function is thread-safe. If it is called by the main thread, the
204 * event will be sent directly to the application, otherwise it will be
205 * queued in the event-loop.
207 void AddLogLine(
208 const wxString &file,
209 int line,
210 bool critical,
211 DebugType type,
212 const wxString &str);
216 * Ensures that any pending entries are sent to the app.
218 * TODO: Ensure that entries are appended to the
219 * logfile even when queued to avoid risk of
220 * data loss.
222 void FlushPendingEntries();
225 * Returns a category specified by index.
227 const CDebugCategory& GetDebugCategory( int index );
230 * Returns the number of debug-categories.
232 unsigned int GetDebugCategoryCount();
237 * This class forwards log-lines from wxWidgets to CLogger.
239 class CLoggerTarget : public wxLog
241 public:
242 CLoggerTarget();
245 * @see wxLog::DoLogString
247 void DoLogString(const wxChar *msg, time_t);
251 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_LOGLINE, -1)
254 /** This event is sent when a log-line is queued. */
255 class CLoggingEvent : public wxEvent
257 public:
258 CLoggingEvent(bool critical, const wxString& msg)
259 : wxEvent(-1, MULE_EVT_LOGLINE)
260 , m_critical(critical)
261 // Deep copy, to avoid thread-unsafe reference counting. */
262 , m_msg(msg.c_str(), msg.Length())
266 const wxString& Message() const {
267 return m_msg;
270 bool IsCritical() const {
271 return m_critical;
274 wxEvent* Clone() const {
275 return new CLoggingEvent(m_critical, m_msg);
278 private:
279 bool m_critical;
280 wxString m_msg;
284 typedef void (wxEvtHandler::*MuleLogEventFunction)(CLoggingEvent&);
286 //! Event-handler for completed hashings of new shared files and partfiles.
287 #define EVT_MULE_LOGGING(func) \
288 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_LOGLINE, -1, -1, \
289 (wxObjectEventFunction) (wxEventFunction) \
290 wxStaticCastEvent(MuleLogEventFunction, &func), (wxObject*) NULL),
294 * These macros should be used when logging. The
295 * AddLogLineM macro will simply call one of the
296 * two CLogger::AddLogLine functions depending on
297 * paramteres, but AddDebugLogLineM will only log
298 * a message if the message is either critical or
299 * the specified debug-type is enabled in the
300 * preferences.
302 #if defined(MULEUNIT)
303 #define AddDebugLogLineM(critical, type, string) do {} while (false)
304 #define AddLogLineM(...) do {} while (false)
305 #else
306 #ifdef __DEBUG__
307 #define AddDebugLogLineM(critical, type, string) \
308 do { \
309 if (critical || CLogger::IsEnabled(type)) { \
310 CLogger::AddLogLine(__TFILE__, __LINE__, critical, type, string); \
312 } while (false)
313 #else
314 #define AddDebugLogLineM(critical, type, string) \
315 do { \
316 if (critical) { \
317 CLogger::AddLogLine(__TFILE__, __LINE__, critical, type, string); \
319 } while (false)
320 #endif
322 #define AddLogLineM(...) CLogger::AddLogLine(__TFILE__, __LINE__, __VA_ARGS__)
323 #endif
325 #endif
326 // File_checked_for_headers