Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/plugin/PluginDebug.cpp

Issue 5137721374801920: Issue #1173 - Default behavior for catch-all blocks
Patch Set: rebase Created March 3, 2015, 8:06 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/plugin/PluginDebug.h ('k') | src/plugin/PluginDebugMacros.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include "PluginStdAfx.h" 18 #include "PluginStdAfx.h"
19
20 #include "PluginDebug.h" 19 #include "PluginDebug.h"
20 #include "Exception.h"
21 #include "PluginMutex.h" 21 #include "PluginMutex.h"
22 #include "PluginSettings.h" 22 #include "PluginSettings.h"
23 23
24 #if (defined ENABLE_DEBUG_INFO)
25 struct EntryPointHandlers
26 {
27 static void Unknown(std::string name)
28 {
29 CPluginDebug::Debug("Unknown exception in catch-all for entry point " + name );
30 }
31 static void Exception(std::exception& ex, std::string name)
32 {
33 CPluginDebug::DebugOrdinaryException(ex,
34 PLUGIN_ERROR_ENTRY_POINT, PLUGIN_ERROR_ENTRY_POINT_CATCHALL_EXCEPTION,
35 "std::exception in catch-all for entry point " + name);
36 }
37 static void LogicError(std::logic_error& ex, std::string name)
38 {
39 CPluginDebug::DebugOrdinaryException(ex,
40 PLUGIN_ERROR_ENTRY_POINT, PLUGIN_ERROR_ENTRY_POINT_CATCHALL_EXCEPTION,
41 "std::logic_error in catch-all for entry point " + name);
42 }
43 static void RuntimeError(std::runtime_error& ex, std::string name)
44 {
45 CPluginDebug::DebugOrdinaryException(ex,
46 PLUGIN_ERROR_ENTRY_POINT, PLUGIN_ERROR_ENTRY_POINT_CATCHALL_EXCEPTION,
47 "std::runtime_error in catch-all for entry point " + name);
48 }
49 static void SystemError(std::system_error& ex, std::string name)
50 {
51 CPluginDebug::DebugSystemException(ex,
52 PLUGIN_ERROR_ENTRY_POINT, PLUGIN_ERROR_ENTRY_POINT_CATCHALL_EXCEPTION,
53 "std::system_error in catch-all for entry point " + name);
54 }
55 };
56 #endif
57
58 void EntryPointExceptionDefault(std::string name)
59 {
60 #if (defined ENABLE_DEBUG_INFO)
61 CatchAllVoid<EntryPointHandlers>::Handler(name);
62 #endif
63 }
64
24 65
25 class CPluginDebugLock : public CPluginMutex 66 class CPluginDebugLock : public CPluginMutex
26 { 67 {
27 68
28 private: 69 private:
29 70
30 static CComAutoCriticalSection s_criticalSectionDebugLock; 71 static CComAutoCriticalSection s_criticalSectionDebugLock;
31 72
32 public: 73 public:
33 74
34 CPluginDebugLock() : CPluginMutex(L"DebugFile", PLUGIN_ERROR_MUTEX_DEBUG_FILE) 75 CPluginDebugLock() : CPluginMutex(L"DebugFile", PLUGIN_ERROR_MUTEX_DEBUG_FILE)
35 { 76 {
36 s_criticalSectionDebugLock.Lock(); 77 s_criticalSectionDebugLock.Lock();
37 } 78 }
38 79
39 ~CPluginDebugLock() 80 ~CPluginDebugLock()
40 { 81 {
41 s_criticalSectionDebugLock.Unlock(); 82 s_criticalSectionDebugLock.Unlock();
42 } 83 }
43 }; 84 };
44 85
45 CComAutoCriticalSection CPluginDebugLock::s_criticalSectionDebugLock; 86 CComAutoCriticalSection CPluginDebugLock::s_criticalSectionDebugLock;
46 87
88 void CPluginDebug::DebugSystemException(const std::system_error& ex, int errorId , int errorSubid, const std::string& description)
89 {
90 std::string message = description + ", " + ex.code().message() + ", " + ex.wha t();
91 DEBUG_ERROR_LOG(ex.code().value(), errorId, errorSubid, message);
92 }
93
94 void CPluginDebug::DebugOrdinaryException(const std::exception& ex, int errorId, int errorSubid, const std::string& description)
95 {
96 std::string message = description + ", " + ex.what();
97 DEBUG_ERROR_LOG(0, errorId, errorSubid, message);
98 }
99
100
47 101
48 #ifdef ENABLE_DEBUG_INFO 102 #ifdef ENABLE_DEBUG_INFO
49 103
50 void CPluginDebug::Debug(const CString& text, DWORD dwProcessId, DWORD dwThreadI d) 104 void DebugLegacy(const CString& text, DWORD dwProcessId, DWORD dwThreadId)
51 { 105 {
52 #ifdef USE_CONSOLE 106 #ifdef USE_CONSOLE
53 CONSOLE("%s", CT2A(text.GetString(), CP_UTF8)); 107 CONSOLE("%s", CT2A(text.GetString(), CP_UTF8));
54 #endif 108 #endif
55 109
56 if (CPluginSettings::HasInstance()) 110 if (CPluginSettings::HasInstance())
57 { 111 {
58 #ifdef ENABLE_DEBUG_SPLIT_FILE 112 #ifdef ENABLE_DEBUG_SPLIT_FILE
59 CPluginSettings* settings = CPluginSettings::GetInstance(); 113 CPluginSettings* settings = CPluginSettings::GetInstance();
60 114
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 debugFile.write("\n", 1); 163 debugFile.write("\n", 1);
110 164
111 line = text.Tokenize(L"\n\r", pos); 165 line = text.Tokenize(L"\n\r", pos);
112 } 166 }
113 167
114 debugFile.flush(); 168 debugFile.flush();
115 } 169 }
116 } 170 }
117 } 171 }
118 172
119 void CPluginDebug::DebugClear() 173 void CPluginDebug::Debug(const std::string& text, DWORD processId, DWORD threadI d)
120 { 174 {
121 CPluginDebugLock lock; 175 DebugLegacy(CString(text.c_str()), processId, threadId);
122 if (lock.IsLocked()) 176 }
123 {
124 DeleteFileW(GetDataPath(L"debug.txt").c_str());
125 DeleteFileW(GetDataPath(L"debug_main_ui.txt").c_str());
126 DeleteFileW(GetDataPath(L"debug_main_thread.txt").c_str());
127 177
128 for (int i = 1; i <= 10; i++) 178 void CPluginDebug::Debug(const std::wstring& text, DWORD processId, DWORD thread Id)
129 { 179 {
130 std::wstring x = std::to_wstring(i); 180 DebugLegacy(ToCString(text), processId, threadId);
131 DeleteFileW(GetDataPath(L"debug_tab" + x + L"_ui.txt").c_str());
132 DeleteFileW(GetDataPath(L"debug_tab" + x + L"_thread.txt").c_str());
133 }
134 }
135 } 181 }
136 182
137 #endif 183 #endif
138 184
139 #if (defined ENABLE_DEBUG_INFO || defined ENABLE_DEBUG_SELFTEST) 185 #if (defined ENABLE_DEBUG_INFO)
140 186
141 void CPluginDebug::DebugError(const CString& error) 187 void CPluginDebug::DebugException(const std::exception& ex)
142 { 188 {
189 auto error = std::string("!!! Exception:") + ex.what();
143 #ifdef ENABLE_DEBUG_ERROR 190 #ifdef ENABLE_DEBUG_ERROR
144 Debug(error); 191 Debug(error);
145 #endif 192 #endif
146 193
147 DEBUG_SELFTEST("************************************************************** ******************\n" + error + "\n********************************************* ***********************************") 194 DEBUG_SELFTEST("************************************************************** ******************\n" + error + "\n********************************************* ***********************************")
148 } 195 }
149 196
150 void CPluginDebug::DebugErrorCode(DWORD errorCode, const CString& error, DWORD d wProcessId, DWORD dwThreadId) 197 void DebugErrorCodeLegacy(DWORD errorCode, const CString& error, DWORD dwProcess Id, DWORD dwThreadId)
151 { 198 {
152 CString errorCodeText; 199 CString errorCodeText;
153 errorCodeText.Format(L"%u (0x%8.8x)", errorCode, errorCode); 200 errorCodeText.Format(L"%u (0x%8.8x)", errorCode, errorCode);
154 201
155 CString finalError = error + L". error=" + errorCodeText; 202 CString finalError = error + L". error=" + errorCodeText;
156 203
157 #ifdef ENABLE_DEBUG_ERROR 204 #ifdef ENABLE_DEBUG_ERROR
158 Debug(finalError, dwProcessId, dwThreadId); 205 DebugLegacy(finalError, dwProcessId, dwThreadId);
159 #endif 206 #endif
160 207
161 DEBUG_SELFTEST(L"************************************************************* *******************\n" + finalError + "\n*************************************** *****************************************") 208 DEBUG_SELFTEST(L"************************************************************* *******************\n" + finalError + "\ n************************************** ******************************************")
209 }
210
211 void CPluginDebug::DebugErrorCode(DWORD errorCode, const std::string& error, DWO RD processId, DWORD threadId)
212 {
213 DebugErrorCodeLegacy(errorCode, CString(error.c_str()), processId, threadId);
162 } 214 }
163 215
164 #endif 216 #endif
165 217
166 // ============================================================================ 218 // ============================================================================
167 // Debug result 219 // Debug result
168 // ============================================================================ 220 // ============================================================================
169 221
170 #ifdef ENABLE_DEBUG_RESULT 222 #ifdef ENABLE_DEBUG_RESULT
171 223
172 void CPluginDebug::DebugResult(const CString& text) 224 void DebugResultLegacy(const CString& text)
173 { 225 {
174 SYSTEMTIME st; 226 SYSTEMTIME st;
175 ::GetSystemTime(&st); 227 ::GetSystemTime(&st);
176 228
177 CStringA sysTime; 229 CStringA sysTime;
178 sysTime.Format("%2.2d:%2.2d:%2.2d.%3.3d - ", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 230 sysTime.Format("%2.2d:%2.2d:%2.2d.%3.3d - ", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
179 231
180 CStringA textA = text; 232 CStringA textA = text;
181 233
182 CPluginDebugLock lock; 234 CPluginDebugLock lock;
183 if (lock.IsLocked()) 235 if (lock.IsLocked())
184 { 236 {
185 std::ofstream debugFile; 237 std::ofstream debugFile;
186 238
187 debugFile.open(GetDataPath(L"debug_result.txt"), std::ios::app); 239 debugFile.open(GetDataPath(L"debug_result.txt"), std::ios::app);
188 debugFile.write(sysTime.GetBuffer(), sysTime.GetLength()); 240 debugFile.write(sysTime.GetBuffer(), sysTime.GetLength());
189 debugFile.write(LPCSTR(textA), textA.GetLength()); 241 debugFile.write(LPCSTR(textA), textA.GetLength());
190 debugFile.write("\n", 1); 242 debugFile.write("\n", 1);
191 debugFile.flush(); 243 debugFile.flush();
192 } 244 }
193 } 245 }
194 246
195 void CPluginDebug::DebugResultDomain(const CString& domain) 247 void CPluginDebug::DebugResult(const std::wstring& text)
248 {
249 DebugResultLegacy(ToCString(text));
250 }
251
252 void CPluginDebug::DebugResultDomain(const std::wstring& domain)
196 { 253 {
197 DebugResult(L"================================================================ ================================================================================ ==========================================="); 254 DebugResult(L"================================================================ ================================================================================ ===========================================");
198 DebugResult(domain); 255 DebugResult(domain);
199 DebugResult(L"================================================================ ================================================================================ ==========================================="); 256 DebugResult(L"================================================================ ================================================================================ ===========================================");
200 } 257 }
201 258
202 259
203 void CPluginDebug::DebugResultBlocking(const CString& type, const std::wstring& src, const std::wstring& domain) 260 void CPluginDebug::DebugResultBlocking(const std::wstring& type, const std::wstr ing& src, const std::wstring& domain)
204 { 261 {
205 CString srcTrunc = ToCString(src); 262 CString srcTrunc = ToCString(src);
206 if (src.length() > 100) 263 if (src.length() > 100)
207 { 264 {
208 srcTrunc = srcTrunc.Left(67) + L"..." + srcTrunc.Right(30); 265 srcTrunc = srcTrunc.Left(67) + L"..." + srcTrunc.Right(30);
209 } 266 }
210 267
211 CString blocking; 268 CString blocking;
212 blocking.Format(L"Blocked %-12s %-20s %s", type, domain.empty()? L"-" : ToC String(domain), srcTrunc); 269 blocking.Format(L"Blocked %-12s %-20s %s", ToCString(type), domain.empty()? L"-" : ToCString(domain), srcTrunc);
213 270
214 DebugResult(blocking); 271 DebugResultLegacy(blocking);
215 } 272 }
216 273
217 274
218 void CPluginDebug::DebugResultHiding(const CString& tag, const CString& id, cons t CString& filter) 275 void CPluginDebug::DebugResultHiding(const std::wstring& tag, const std::wstring & id, const std::wstring& filter)
219 { 276 {
220 CString srcTrunc = id; 277 CString srcTrunc = ToCString(id);
221 if (srcTrunc.GetLength() > 100) 278 if (srcTrunc.GetLength() > 100)
222 { 279 {
223 srcTrunc = srcTrunc.Left(67) + L"..." + srcTrunc.Right(30); 280 srcTrunc = srcTrunc.Left(67) + L"..." + srcTrunc.Right(30);
224 } 281 }
225 282
226 CString blocking; 283 CString blocking;
227 blocking.Format(L"Hidden %-12s - %s %s", tag, srcTrunc, filter); 284 blocking.Format(L"Hidden %-12s - %s %s", ToCString(tag), srcTrunc, ToCStri ng(filter));
228 285
229 DebugResult(blocking); 286 DebugResultLegacy(blocking);
230 }
231
232
233 void CPluginDebug::DebugResultClear()
234 {
235 CPluginDebugLock lock;
236 if (lock.IsLocked())
237 {
238 DeleteFileW(GetDataPath(L"debug_result.txt").c_str());
239 }
240 } 287 }
241 288
242 #endif // ENABLE_DEBUG_RESULT 289 #endif // ENABLE_DEBUG_RESULT
243 290
244 291
245 #ifdef ENABLE_DEBUG_RESULT_IGNORED 292 #ifdef ENABLE_DEBUG_RESULT_IGNORED
246 293
247 void CPluginDebug::DebugResultIgnoring(const CString& type, const std::wstring& src, const std::wstring& domain) 294 void CPluginDebug::DebugResultIgnoring(const std::wstring& type, const std::wstr ing& src, const std::wstring& domain)
248 { 295 {
249 CString srcTrunc = ToCString(src); 296 CString srcTrunc = ToCString(src);
250 if (src.length() > 100) 297 if (src.length() > 100)
251 { 298 {
252 srcTrunc = srcTrunc.Left(67) + L"..." + srcTrunc.Right(30); 299 srcTrunc = srcTrunc.Left(67) + L"..." + srcTrunc.Right(30);
253 } 300 }
254 301
255 CString blocking; 302 CString blocking;
256 blocking.Format(L"Ignored %-12s %s %s", type, domain.empty()? L"-" : ToCStr ing(domain), srcTrunc); 303 blocking.Format(L"Ignored %-12s %s %s", ToCString(type), domain.empty()? L" -" : ToCString(domain), srcTrunc);
257 304
258 DebugResult(blocking); 305 DebugResultLegacy(blocking);
259 } 306 }
260 307
261 #endif // ENABLE_DEBUG_RESULT_IGNORED 308 #endif // ENABLE_DEBUG_RESULT_IGNORED
OLDNEW
« no previous file with comments | « src/plugin/PluginDebug.h ('k') | src/plugin/PluginDebugMacros.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld