Upstream tarball 20080512
[amule.git] / src / Logger.cpp
blob5002f440791db93b68653d7ce0906ca14edd3e44
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 #include "Logger.h"
26 #include "amule.h"
27 #include "Preferences.h"
29 #include <wx/filename.h>
32 DEFINE_LOCAL_EVENT_TYPE(MULE_EVT_LOGLINE)
35 CDebugCategory::CDebugCategory( DebugType type, const wxString& name )
36 : m_name( name ),
37 m_type( type )
39 m_enabled = false;
43 bool CDebugCategory::IsEnabled() const
45 return m_enabled;
49 void CDebugCategory::SetEnabled( bool enabled )
51 m_enabled = enabled;
55 const wxString& CDebugCategory::GetName() const
57 return m_name;
61 DebugType CDebugCategory::GetType() const
63 return m_type;
66 CDebugCategory g_debugcats[] = {
67 CDebugCategory( logGeneral, wxT("General") ),
68 CDebugCategory( logHasher, wxT("Hasher") ),
69 CDebugCategory( logClient, wxT("ED2k Client") ),
70 CDebugCategory( logLocalClient, wxT("Local Client Protocol") ),
71 CDebugCategory( logRemoteClient, wxT("Remote Client Protocol") ),
72 CDebugCategory( logPacketErrors, wxT("Packet Parsing Errors") ),
73 CDebugCategory( logCFile, wxT("CFile") ),
74 CDebugCategory( logFileIO, wxT("FileIO") ),
75 CDebugCategory( logZLib, wxT("ZLib") ),
76 CDebugCategory( logAICHThread, wxT("AICH-Hasher") ),
77 CDebugCategory( logAICHTransfer, wxT("AICH-Transfer") ),
78 CDebugCategory( logAICHRecovery, wxT("AICH-Recovery") ),
79 CDebugCategory( logListenSocket, wxT("ListenSocket") ),
80 CDebugCategory( logCredits, wxT("Credits") ),
81 CDebugCategory( logClientUDP, wxT("ClientUDPSocket") ),
82 CDebugCategory( logDownloadQueue, wxT("DownloadQueue") ),
83 CDebugCategory( logIPFilter, wxT("IPFilter") ),
84 CDebugCategory( logKnownFiles, wxT("KnownFileList") ),
85 CDebugCategory( logPartFile, wxT("PartFiles") ),
86 CDebugCategory( logSHAHashSet, wxT("SHAHashSet") ),
87 CDebugCategory( logServer, wxT("Servers") ),
88 CDebugCategory( logProxy, wxT("Proxy") ),
89 CDebugCategory( logSearch, wxT("Searching") ),
90 CDebugCategory( logServerUDP, wxT("ServerUDP") ),
91 CDebugCategory( logClientKadUDP, wxT("Client Kademlia UDP") ),
92 CDebugCategory( logKadSearch, wxT("Kademlia Search") ),
93 CDebugCategory( logKadRouting, wxT("Kademlia Routing") ),
94 CDebugCategory( logKadIndex, wxT("Kademlia Indexing") ),
95 CDebugCategory( logKadMain, wxT("Kademlia Main Thread") ),
96 CDebugCategory( logKadPrefs, wxT("Kademlia Preferences") ),
97 CDebugCategory( logPfConvert, wxT("PartFileConvert") ),
98 CDebugCategory( logMuleUDP, wxT("MuleUDPSocket" ) ),
99 CDebugCategory( logThreads, wxT("ThreadScheduler" ) ),
100 CDebugCategory( logUPnP, wxT("Universal Plug and Play" ) ),
101 CDebugCategory( logKadUdpFwTester, wxT("Kademlia UDP Firewall Tester") ),
102 CDebugCategory( logKadPacketTracking, wxT("Kademlia Packet Tracking") ),
103 CDebugCategory( logKadEntryTracking, wxT("Kademlia Entry Tracking") )
107 const int categoryCount = sizeof( g_debugcats ) / sizeof( g_debugcats[0] );
111 bool CLogger::IsEnabled( DebugType type )
113 #ifdef __DEBUG__
114 int index = (int)type;
116 if ( index >= 0 && index < categoryCount ) {
117 const CDebugCategory& cat = g_debugcats[ index ];
118 wxASSERT( type == cat.GetType() );
120 return ( cat.IsEnabled() && thePrefs::GetVerbose() );
123 wxASSERT( false );
124 #endif
125 return false;
129 void CLogger::SetEnabled( DebugType type, bool enabled )
131 int index = (int)type;
133 if ( index >= 0 && index < categoryCount ) {
134 CDebugCategory& cat = g_debugcats[ index ];
135 wxASSERT( type == cat.GetType() );
137 cat.SetEnabled( enabled );
138 } else {
139 wxASSERT( false );
144 struct LogEntry
146 bool critical;
147 wxString entry;
151 static std::deque<LogEntry*> s_backLog;
152 static wxMutex s_mutex;
155 void PushEntry(bool critical, const wxString& str)
157 wxMutexLocker lock(s_mutex);
159 LogEntry* item = new LogEntry;
160 item->critical = critical;
161 item->entry = str;
163 s_backLog.push_back(item);
167 LogEntry* PopEntry()
169 wxMutexLocker lock(s_mutex);
171 if (s_backLog.empty()) {
172 return NULL;
175 LogEntry* entry = s_backLog.front();
176 s_backLog.pop_front();
178 return entry;
184 void CLogger::FlushPendingEntries()
186 wxCHECK_RET(wxThread::IsMain(), wxT("Must be called by main thread."));
188 LogEntry* entry = NULL;
189 while ((entry = PopEntry())) {
190 CLoggingEvent event(entry->critical, entry->entry);
192 #ifdef CLIENT_GUI
193 theApp->ProcessEvent(event);
194 #else
195 // Try to handle events immediatly when possible (to save to file).
196 if (theApp->applog) {
197 theApp->ProcessEvent(event);
198 } else {
199 theApp->AddPendingEvent(event);
201 #endif
203 delete entry;
208 void CLogger::AddLogLine(
209 const wxString &file,
210 int line,
211 bool critical,
212 const wxString &str)
214 wxString msg;
215 msg << file.AfterLast(wxFileName::GetPathSeparator()).AfterLast(wxT('/')) << wxT("(") << line << wxT("): ") << str;
216 PushEntry(critical, msg);
218 if (wxThread::IsMain()) {
219 FlushPendingEntries();
224 void CLogger::AddLogLine(
225 const wxString &file,
226 int line,
227 bool critical,
228 DebugType type,
229 const wxString& str)
231 int index = (int)type;
233 if ( index >= 0 && index < categoryCount ) {
234 const CDebugCategory& cat = g_debugcats[ index ];
235 wxASSERT(type == cat.GetType());
237 AddLogLine(file, line, critical, cat.GetName() + wxT(": ") + str);
238 } else {
239 wxASSERT( false );
244 const CDebugCategory& CLogger::GetDebugCategory( int index )
246 wxASSERT( index >= 0 && index < categoryCount );
248 return g_debugcats[ index ];
252 unsigned int CLogger::GetDebugCategoryCount()
254 return categoryCount;
258 CLoggerTarget::CLoggerTarget()
263 void CLoggerTarget::DoLogString(const wxChar* msg, time_t)
265 wxCHECK_RET(msg, wxT("Log message is NULL in DoLogString!"));
267 wxString str(msg);
269 // This is much simpler than manually handling all wx log-types.
270 bool critical = str.StartsWith(_("Error: ")) || str.StartsWith(_("Warning: "));
272 CLogger::AddLogLine(__TFILE__, __LINE__, critical, str);
275 // File_checked_for_headers