2 // This file is part of the aMule Project.
4 // Copyright (C) 2005-2008 aMule Team ( admin@amule.org / http://www.amule.org )
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
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
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
)
43 bool CDebugCategory::IsEnabled() const
49 void CDebugCategory::SetEnabled( bool enabled
)
55 const wxString
& CDebugCategory::GetName() const
61 DebugType
CDebugCategory::GetType() const
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
)
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() );
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
);
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
;
160 s_backLog
.push_back(item
);
166 wxMutexLocker
lock(s_mutex
);
168 if (s_backLog
.empty()) {
172 LogEntry
* entry
= s_backLog
.front();
173 s_backLog
.pop_front();
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
);
190 theApp
->ProcessEvent(event
);
192 // Try to handle events immediatly when possible (to save to file).
193 if (theApp
->applog
) {
194 theApp
->ProcessEvent(event
);
196 theApp
->AddPendingEvent(event
);
205 void CLogger::AddLogLine(
206 const wxString
&file
,
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
,
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
);
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!"));
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