fixed a new bison warning in Ubuntu 13.4
[amule.git] / src / Logger.h
blob9f9b0393d288e9d9757b7a683ba10f101c0c4cc7
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2005-2011 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.
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 //! Warnings/Errors related to HTTP traffic
114 logHTTP,
115 //! Warnings/Errors related to Boost Asio networking
116 logAsio
117 // IMPORTANT NOTE: when you add values to this enum, update the g_debugcats
118 // array in Logger.cpp!
124 * Container-class for the debugging categories.
126 class CDebugCategory
128 public:
130 * Constructor.
132 * @param type The actual debug-category type.
133 * @param name The user-readable name.
135 CDebugCategory( DebugType type, const wxString& name )
136 : m_name(name), m_type(type), m_enabled(false)
141 * Returns true if the category is enabled.
143 bool IsEnabled() const { return m_enabled; }
146 * Enables/Disables the category.
148 void SetEnabled( bool enabled ) { m_enabled = enabled; }
152 * Returns the user-readable name.
154 const wxString& GetName() const { return m_name; }
157 * Returns the actual type.
159 DebugType GetType() const { return m_type; }
161 private:
162 //! The user-readable name.
163 wxString m_name;
164 //! The actual type.
165 DebugType m_type;
166 //! Whenever or not the category is enabled.
167 bool m_enabled;
172 * Functions for logging operations.
174 class CLogger: public wxEvtHandler
176 public:
178 * Returns true if debug-messages should be generated for a specific category.
180 #ifdef __DEBUG__
181 bool IsEnabled( DebugType ) const;
182 #else
183 bool IsEnabled( DebugType ) const { return false; }
184 #endif
187 * Enables or disables debug-messages for a specific category.
189 void SetEnabled( DebugType type, bool enabled );
192 * Returns true if logging to stdout is enabled
194 bool IsEnabledStdoutLog() const { return m_StdoutLog; }
197 * Enables or disables logging to stdout.
199 void SetEnabledStdoutLog(bool enabled) { m_StdoutLog = enabled; }
203 * Logs the specified line of text, prefixed with the name of the DebugType.
204 * (except for logStandard)
206 * @param file
207 * @param line
208 * @param critical If true, then the message will be made visible directly to the user.
209 * @param type The debug-category, the name of which will be prepended to the line.
210 * @param str The actual line of text.
212 * This function is thread-safe. If it is called by the main thread, the
213 * event will be sent directly to the application, otherwise it will be
214 * queued in the event-loop.
216 void AddLogLine(
217 const wxString &file,
218 int line,
219 bool critical,
220 DebugType type,
221 const wxString &str,
222 bool toStdout = false,
223 bool toGUI = true);
225 // for UPnP
226 void AddLogLine(
227 const wxString &file,
228 int line,
229 bool critical,
230 DebugType type,
231 const std::ostringstream &msg);
233 void AddLogLine(
234 const wxString &file,
235 int line,
236 bool critical,
237 const std::ostringstream &msg);
241 * Emergency log for crashes.
243 void EmergencyLog(const wxString &message, bool closeLog = true);
246 * Returns a category specified by index.
248 const CDebugCategory& GetDebugCategory( int index );
251 * Returns the number of debug-categories.
253 unsigned int GetDebugCategoryCount();
256 * Open Logfile, true on success
258 bool OpenLogfile(const wxString & name);
261 * Close Logfile
263 void CloseLogfile();
266 * Get name of Logfile
268 const wxString & GetLogfileName() const {
269 return m_LogfileName;
273 * Event handler
275 void OnLoggingEvent(class CLoggingEvent& evt);
278 * Construct
280 CLogger() {
281 applog = NULL;
282 m_StdoutLog = false;
283 m_count = 0;
286 private:
287 class wxFFileOutputStream* applog; // the logfile
288 wxString m_LogfileName;
289 wxString m_ApplogBuf;
290 bool m_StdoutLog;
291 int m_count; // output line counter
292 wxMutex m_lineLock;
295 * Write all waiting log info to the logfile
297 void FlushApplog();
300 * Really output a single line
302 void DoLine(const wxString & line, bool toStdout, bool toGUI);
305 * Really output several lines
307 void DoLines(const wxString & lines, bool critical, bool toStdout, bool toGUI);
309 DECLARE_EVENT_TABLE()
312 extern CLogger theLogger;
315 * This class forwards log-lines from wxWidgets to CLogger.
317 class CLoggerTarget : public wxLog
319 public:
320 CLoggerTarget();
323 * @see wxLog::DoLogString
325 #if wxCHECK_VERSION(2, 9, 0)
326 void DoLogText(const wxString &msg);
327 #else
328 void DoLogString(const wxChar *msg, time_t);
329 #endif
333 DECLARE_LOCAL_EVENT_TYPE(MULE_EVT_LOGLINE, -1)
336 /** This event is sent when a log-line is queued. */
337 class CLoggingEvent : public wxEvent
339 public:
340 CLoggingEvent(bool critical, bool toStdout, bool toGUI, const wxString& msg)
341 : wxEvent(-1, MULE_EVT_LOGLINE)
342 , m_critical(critical)
343 , m_stdout(toStdout)
344 , m_GUI(toGUI)
345 // Deep copy, to avoid thread-unsafe reference counting. */
346 , m_msg(msg.c_str(), msg.Length())
350 const wxString& Message() const {
351 return m_msg;
354 bool IsCritical() const {
355 return m_critical;
358 bool ToStdout() const {
359 return m_stdout;
362 bool ToGUI() const {
363 return m_GUI;
366 wxEvent* Clone() const {
367 return new CLoggingEvent(m_critical, m_stdout, m_GUI, m_msg);
370 private:
371 bool m_critical;
372 bool m_stdout;
373 bool m_GUI;
374 wxString m_msg;
378 typedef void (wxEvtHandler::*MuleLogEventFunction)(CLoggingEvent&);
380 //! Event-handler for completed hashings of new shared files and partfiles.
381 #define EVT_MULE_LOGGING(func) \
382 DECLARE_EVENT_TABLE_ENTRY(MULE_EVT_LOGLINE, -1, -1, \
383 (wxObjectEventFunction) (wxEventFunction) \
384 wxStaticCastEvent(MuleLogEventFunction, &func), (wxObject*) NULL),
387 // access the logfile for EC
388 class CLoggerAccess
390 private:
391 class wxFFileInputStream * m_logfile;
392 class wxCharBuffer * m_buffer;
393 size_t m_bufferlen;
394 size_t m_pos;
396 bool m_ready;
397 public:
399 // construct/destruct
401 CLoggerAccess();
402 ~CLoggerAccess();
404 // Reset (used when logfile is cleared)
406 void Reset();
408 // get a String (if there is one)
410 bool GetString(wxString & s);
412 // is a String available ?
414 bool HasString();
419 * These macros should be used when logging. The
420 * AddLogLineM macro will simply call one of the
421 * two CLogger::AddLogLine functions depending on
422 * parameters, but AddDebugLogLine* will only log
423 * a message if the message is either critical or
424 * the specified debug-type is enabled in the
425 * preferences.
426 * AddLogLineMS will also always print to stdout.
428 #ifdef MULEUNIT
429 #define AddDebugLogLineN(...) do {} while (false)
430 #define AddLogLineN(...) do {} while (false)
431 #define AddLogLineNS(...) do {} while (false)
432 #define AddDebugLogLineC(...) do {} while (false)
433 #define AddLogLineC(...) do {} while (false)
434 #define AddLogLineCS(...) do {} while (false)
435 #else
436 // Macro for UPnP. This is not a debug macro, but wants its category printed nevertheless (sigh).
437 #define AddLogLineU(critical, type, string) theLogger.AddLogLine(__TFILE__, __LINE__, critical, type, string)
438 // Macros for 'N'on critical logging
439 #ifdef __DEBUG__
440 #define AddDebugLogLineN(type, string) if (theLogger.IsEnabled(type)) theLogger.AddLogLine(__TFILE__, __LINE__, false, type, string)
441 #else
442 #define AddDebugLogLineN(type, string) do {} while (false)
443 #endif
444 #define AddLogLineN(string) theLogger.AddLogLine(__TFILE__, __LINE__, false, logStandard, string)
445 #define AddLogLineNS(string) theLogger.AddLogLine(__TFILE__, __LINE__, false, logStandard, string, true)
446 // Macros for 'C'ritical logging
447 #define AddDebugLogLineC(type, string) theLogger.AddLogLine(__TFILE__, __LINE__, true, type, string)
448 #define AddLogLineC(string) theLogger.AddLogLine(__TFILE__, __LINE__, true, logStandard, string)
449 #define AddLogLineCS(string) theLogger.AddLogLine(__TFILE__, __LINE__, true, logStandard, string, true)
450 // Macros for logging to logfile only
451 #ifdef __DEBUG__
452 #define AddDebugLogLineF(type, string) if (theLogger.IsEnabled(type)) theLogger.AddLogLine(__TFILE__, __LINE__, false, type, string, false, false)
453 #else
454 #define AddDebugLogLineF(type, string) do {} while (false)
455 #endif
456 #define AddLogLineF(string) theLogger.AddLogLine(__TFILE__, __LINE__, false, logStandard, string, false, false)
457 #endif
459 #endif
460 // File_checked_for_headers