Upstream tarball 20080414
[amule.git] / src / Logger.cpp
blob488d91e13cd2f8dd42c08b629b2874e76aac4ef7
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" ) ),
104 const int categoryCount = sizeof( g_debugcats ) / sizeof( g_debugcats[0] );
108 bool CLogger::IsEnabled( DebugType type )
110 #ifdef __DEBUG__
111 int index = (int)type;
113 if ( index >= 0 && index < categoryCount ) {
114 const CDebugCategory& cat = g_debugcats[ index ];
115 wxASSERT( type == cat.GetType() );
117 return ( cat.IsEnabled() && thePrefs::GetVerbose() );
120 wxASSERT( false );
121 #endif
122 return false;
126 void CLogger::SetEnabled( DebugType type, bool enabled )
128 int index = (int)type;
130 if ( index >= 0 && index < categoryCount ) {
131 CDebugCategory& cat = g_debugcats[ index ];
132 wxASSERT( type == cat.GetType() );
134 cat.SetEnabled( enabled );
135 } else {
136 wxASSERT( false );
141 struct LogEntry
143 bool critical;
144 wxString entry;
148 static std::deque<LogEntry*> s_backLog;
149 static wxMutex s_mutex;
152 void PushEntry(bool critical, const wxString& str)
154 wxMutexLocker lock(s_mutex);
156 LogEntry* item = new LogEntry;
157 item->critical = critical;
158 item->entry = str;
160 s_backLog.push_back(item);
164 LogEntry* PopEntry()
166 wxMutexLocker lock(s_mutex);
168 if (s_backLog.empty()) {
169 return NULL;
172 LogEntry* entry = s_backLog.front();
173 s_backLog.pop_front();
175 return entry;
181 void CLogger::FlushPendingEntries()
183 wxCHECK_RET(wxThread::IsMain(), wxT("Must be called by main thread."));
185 LogEntry* entry = NULL;
186 while ((entry = PopEntry())) {
187 CLoggingEvent event(entry->critical, entry->entry);
189 #ifdef CLIENT_GUI
190 theApp->ProcessEvent(event);
191 #else
192 // Try to handle events immediatly when possible (to save to file).
193 if (theApp->applog) {
194 theApp->ProcessEvent(event);
195 } else {
196 theApp->AddPendingEvent(event);
198 #endif
200 delete entry;
205 void CLogger::AddLogLine(
206 const wxString &file,
207 int line,
208 bool critical,
209 const wxString &str)
211 wxString msg;
212 msg << file.AfterLast(wxFileName::GetPathSeparator()).AfterLast(wxT('/')) << wxT("(") << line << wxT("): ") << str;
213 PushEntry(critical, msg);
215 if (wxThread::IsMain()) {
216 FlushPendingEntries();
221 void CLogger::AddLogLine(
222 const wxString &file,
223 int line,
224 bool critical,
225 DebugType type,
226 const wxString& str)
228 int index = (int)type;
230 if ( index >= 0 && index < categoryCount ) {
231 const CDebugCategory& cat = g_debugcats[ index ];
232 wxASSERT(type == cat.GetType());
234 AddLogLine(file, line, critical, cat.GetName() + wxT(": ") + str);
235 } else {
236 wxASSERT( false );
241 const CDebugCategory& CLogger::GetDebugCategory( int index )
243 wxASSERT( index >= 0 && index < categoryCount );
245 return g_debugcats[ index ];
249 unsigned int CLogger::GetDebugCategoryCount()
251 return categoryCount;
255 CLoggerTarget::CLoggerTarget()
260 void CLoggerTarget::DoLogString(const wxChar* msg, time_t)
262 wxCHECK_RET(msg, wxT("Log message is NULL in DoLogString!"));
264 wxString str(msg);
266 // This is much simpler than manually handling all wx log-types.
267 bool critical = str.StartsWith(_("Error: ")) || str.StartsWith(_("Warning: "));
269 CLogger::AddLogLine(__TFILE__, __LINE__, critical, str);
272 // File_checked_for_headers