Upstream tarball 9964
[amule.git] / src / Logger.h
blob2f14609c0577c7105e0d0cab0f36e67a5612ce5f
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>
30 #include <iosfwd>
33 enum DebugType
35 //! Standard warning, not debug
36 logStandard = -1,
37 //! General warnings/errors.
38 logGeneral = 0,
39 //! Warnings/Errors for the main hashing thread.
40 logHasher,
41 //! Warnings/Errors for client-objects.
42 logClient,
43 //! Warnings/Errors for the local client protocol.
44 logLocalClient,
45 //! Warnings/Errors for the remote client protocol.
46 logRemoteClient,
47 //! Warnings/Errors when parsing packets.
48 logPacketErrors,
49 //! Warnings/Errors for the CFile class.
50 logCFile,
51 //! Warnings/Errors related to reading/writing files.
52 logFileIO,
53 //! Warnings/Errors when using the zLib library.
54 logZLib,
55 //! Warnings/Errors for the AICH-syncronization thread.
56 logAICHThread,
57 //! Warnings/Errors for transfering AICH hash-sets.
58 logAICHTransfer,
59 //! Warnings/Errors when recovering with AICH.
60 logAICHRecovery,
61 //! Warnings/Errors for the CListenSocket class.
62 logListenSocket,
63 //! Warnings/Errors for Client-Credits.
64 logCredits,
65 //! Warnings/Errors for the client UDP socket.
66 logClientUDP,
67 //! Warnings/Errors for the download-queue.
68 logDownloadQueue,
69 //! Warnings/Errors for the IP-Filter.
70 logIPFilter,
71 //! Warnings/Errors for known-files.
72 logKnownFiles,
73 //! Warnings/Errors for part-files.
74 logPartFile,
75 //! Warnings/Errors for SHA-hashset creation.
76 logSHAHashSet,
77 //! Warnings/Errors for servers, server connections.
78 logServer,
79 //! Warnings/Errors for proxy.
80 logProxy,
81 //! Warnings/Errors related to searching.
82 logSearch,
83 //! Warnings/Errors related to the server UDP socket.
84 logServerUDP,
85 //! Warning/Errors related to Kademlia UDP comunication on client
86 logClientKadUDP,
87 //! Warning/Errors related to Kademlia Search
88 logKadSearch,
89 //! Warning/Errors related to Kademlia Routing
90 logKadRouting,
91 //! Warning/Errors related to Kademlia Indexing
92 logKadIndex,
93 //! Warning/Errors related to Kademlia Main Thread
94 logKadMain,
95 //! Warning/Errors related to Kademlia Preferences
96 logKadPrefs,
97 //! Warnings/Errors related to partfile importer
98 logPfConvert,
99 //! Warnings/Errors related to the basic UDP socket-class.
100 logMuleUDP,
101 //! Warnings/Errors related to the thread-scheduler.
102 logThreads,
103 //! Warnings/Errors related to the Universal Plug and Play subsystem.
104 logUPnP,
105 //! Warnings/Errors related to the UDP Firewall Tester
106 logKadUdpFwTester,
107 //! Warnings/Errors related to Kad packet tracking.
108 logKadPacketTracking,
109 //! Warnings/Errors related to Kad entry tracking.
110 logKadEntryTracking,
111 //! Full log of external connection packets
112 logEC
113 // IMPORTANT NOTE: when you add values to this enum, update the g_debugcats
114 // array in Logger.cpp!
120 * Container-class for the debugging categories.
122 class CDebugCategory
124 public:
126 * Constructor.
128 * @param type The actual debug-category type.
129 * @param name The user-readable name.
131 CDebugCategory( DebugType type, const wxString& name )
132 : m_name(name), m_type(type), m_enabled(false)
137 * Returns true if the category is enabled.
139 bool IsEnabled() const { return m_enabled; }
142 * Enables/Disables the category.
144 void SetEnabled( bool enabled ) { m_enabled = enabled; }
148 * Returns the user-readable name.
150 const wxString& GetName() const { return m_name; }
153 * Returns the actual type.
155 DebugType GetType() const { return m_type; }
157 private:
158 //! The user-readable name.
159 wxString m_name;
160 //! The actual type.
161 DebugType m_type;
162 //! Whenever or not the category is enabled.
163 bool m_enabled;
168 * Functions for logging operations.
170 class CLogger: public wxEvtHandler
172 public:
174 * Returns true if debug-messages should be generated for a specific category.
176 #ifdef __DEBUG__
177 bool IsEnabled( DebugType ) const;
178 #else
179 bool IsEnabled( DebugType ) const { return false; }
180 #endif
183 * Enables or disables debug-messages for a specific category.
185 void SetEnabled( DebugType type, bool enabled );
188 * Returns true if logging to stdout is enabled
190 bool IsEnabledStdoutLog() const { return m_StdoutLog; }
193 * Enables or disables logging to stdout.
195 void SetEnabledStdoutLog(bool enabled) { m_StdoutLog = enabled; }
199 * Logs the specified line of text, prefixed with the name of the DebugType.
200 * (except for logStandard)
202 * @param file
203 * @param line
204 * @param critical If true, then the message will be made visible directly to the user.
205 * @param type The debug-category, the name of which will be prepended to the line.
206 * @param str The actual line of text.
208 * This function is thread-safe. If it is called by the main thread, the
209 * event will be sent directly to the application, otherwise it will be
210 * queued in the event-loop.
212 void AddLogLine(
213 const wxString &file,
214 int line,
215 bool critical,
216 DebugType type,
217 const wxString &str,
218 bool toStdout = false,
219 bool toGUI = true);
221 // for UPnP
222 void AddLogLine(
223 const wxString &file,
224 int line,
225 bool critical,
226 DebugType type,
227 const std::ostringstream &msg);
229 void AddLogLine(
230 const wxString &file,
231 int line,
232 bool critical,
233 const std::ostringstream &msg);
237 * Returns a category specified by index.
239 const CDebugCategory& GetDebugCategory( int index );
242 * Returns the number of debug-categories.
244 unsigned int GetDebugCategoryCount();
247 * Open Logfile, true on success
249 bool OpenLogfile(const wxString & name);
252 * Close Logfile
254 void CloseLogfile();
257 * Get name of Logfile
259 const wxString & GetLogfileName() const {
260 return m_LogfileName;
264 * Event handler
266 void OnLoggingEvent(class CLoggingEvent& evt);
269 * Construct
271 CLogger() {
272 applog = NULL;
273 m_StdoutLog = false;
274 m_count = 0;
277 private:
278 class wxFFileOutputStream* applog; // the logfile
279 wxString m_LogfileName;
280 wxString m_ApplogBuf;
281 bool m_StdoutLog;
282 int m_count; // output line counter
285 * Write all waiting log info to the logfile
287 void FlushApplog();
290 * Really output a single line
292 void DoLine(const wxString & line, bool toStdout, bool toGUI);
294 DECLARE_EVENT_TABLE()
297 extern CLogger theLogger;
300 * This class forwards log-lines from wxWidgets to CLogger.
302 class CLoggerTarget : public wxLog
304 public:
305 CLoggerTarget();
308 * @see wxLog::DoLogString
310 #if wxCHECK_VERSION(2, 9, 0)
311 void DoLogText(const wxString &msg);
312 #else
313 void DoLogString(const wxChar *msg, time_t);
314 #endif
318 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_LOGLINE, -1)
321 /** This event is sent when a log-line is queued. */
322 class CLoggingEvent : public wxEvent
324 public:
325 CLoggingEvent(bool critical, bool toStdout, bool toGUI, const wxString& msg)
326 : wxEvent(-1, MULE_EVT_LOGLINE)
327 , m_critical(critical)
328 , m_stdout(toStdout)
329 , m_GUI(toGUI)
330 // Deep copy, to avoid thread-unsafe reference counting. */
331 , m_msg(msg.c_str(), msg.Length())
335 const wxString& Message() const {
336 return m_msg;
339 bool IsCritical() const {
340 return m_critical;
343 bool ToStdout() const {
344 return m_stdout;
347 bool ToGUI() const {
348 return m_GUI;
351 wxEvent* Clone() const {
352 return new CLoggingEvent(m_critical, m_stdout, m_GUI, m_msg);
355 private:
356 bool m_critical;
357 bool m_stdout;
358 bool m_GUI;
359 wxString m_msg;
363 typedef void (wxEvtHandler::*MuleLogEventFunction)(CLoggingEvent&);
365 //! Event-handler for completed hashings of new shared files and partfiles.
366 #define EVT_MULE_LOGGING(func) \
367 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_LOGLINE, -1, -1, \
368 (wxObjectEventFunction) (wxEventFunction) \
369 wxStaticCastEvent(MuleLogEventFunction, &func), (wxObject*) NULL),
372 // access the logfile for EC
373 class CLoggerAccess
375 private:
376 class wxFFileInputStream * m_logfile;
377 class wxCharBuffer * m_buffer;
378 size_t m_bufferlen;
379 size_t m_pos;
381 bool m_ready;
382 public:
384 // construct/destruct
386 CLoggerAccess();
387 ~CLoggerAccess();
389 // get a String (if there is one)
391 bool GetString(wxString & s);
393 // is a String available ?
395 bool HasString();
400 * These macros should be used when logging. The
401 * AddLogLineM macro will simply call one of the
402 * two CLogger::AddLogLine functions depending on
403 * parameters, but AddDebugLogLineM will only log
404 * a message if the message is either critical or
405 * the specified debug-type is enabled in the
406 * preferences.
407 * AddLogLineMS will also always print to stdout.
409 #if defined(MULEUNIT)
410 #define AddDebugLogLineM(...) do {} while (false)
411 #define AddLogLineM(...) do {} while (false)
412 #define AddLogLineMS(...) do {} while (false)
413 #define AddDebugLogLineN(...) do {} while (false)
414 #define AddLogLineN(...) do {} while (false)
415 #define AddLogLineNS(...) do {} while (false)
416 #define AddDebugLogLineC(...) do {} while (false)
417 #define AddLogLineC(...) do {} while (false)
418 #define AddLogLineCS(...) do {} while (false)
419 #else
420 // Macros passing critical flag (legacy)
421 #define AddDebugLogLineM(critical, type, string) theLogger.AddLogLine(__TFILE__, __LINE__, critical, type, string)
422 #define AddLogLineM(critical, string) theLogger.AddLogLine(__TFILE__, __LINE__, critical, logStandard, string)
423 #define AddLogLineMS(critical, string) theLogger.AddLogLine(__TFILE__, __LINE__, critical, logStandard, string, true)
424 // Macro for UPnP. This is not a debug macro, but wants its category printed nevertheless (sigh).
425 #define AddLogLineU(critical, type, string) theLogger.AddLogLine(__TFILE__, __LINE__, critical, type, string)
426 // Macros for 'N'on critical logging
427 #ifdef __DEBUG__
428 #define AddDebugLogLineN(type, string) theLogger.AddLogLine(__TFILE__, __LINE__, false, type, string)
429 #else
430 #define AddDebugLogLineN(type, string) do {} while (false)
431 #endif
432 #define AddLogLineN(string) theLogger.AddLogLine(__TFILE__, __LINE__, false, logStandard, string)
433 #define AddLogLineNS(string) theLogger.AddLogLine(__TFILE__, __LINE__, false, logStandard, string, true)
434 // Macros for 'C'ritical logging
435 #define AddDebugLogLineC(type, string) theLogger.AddLogLine(__TFILE__, __LINE__, true, type, string)
436 #define AddLogLineC(string) theLogger.AddLogLine(__TFILE__, __LINE__, true, logStandard, string)
437 #define AddLogLineCS(string) theLogger.AddLogLine(__TFILE__, __LINE__, true, logStandard, string, true)
438 // Macros for logging to logfile only
439 #define AddDebugLogLineF(type, string) theLogger.AddLogLine(__TFILE__, __LINE__, false, type, string, false, false)
440 #define AddLogLineF(string) theLogger.AddLogLine(__TFILE__, __LINE__, false, logStandard, string, false, false)
441 #endif
443 #endif
444 // File_checked_for_headers