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

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

Issue 5747779603267584: Issue #1234 - Rework strings in debug facility (Closed)
Patch Set: Fixed spaces Created Feb. 25, 2015, 3:24 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
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 19
20 #include "PluginDebug.h" 20 #include "PluginDebug.h"
21 #include "PluginMutex.h" 21 #include "PluginMutex.h"
22 #include "PluginSettings.h" 22 #include "PluginSettings.h"
23 23
24
25 class CPluginDebugLock : public CPluginMutex 24 class CPluginDebugLock : public CPluginMutex
26 { 25 {
27 26
28 private: 27 private:
29 28
30 static CComAutoCriticalSection s_criticalSectionDebugLock; 29 static CComAutoCriticalSection s_criticalSectionDebugLock;
31 30
32 public: 31 public:
33 32
34 CPluginDebugLock() : CPluginMutex(L"DebugFile", PLUGIN_ERROR_MUTEX_DEBUG_FILE) 33 CPluginDebugLock() : CPluginMutex(L"DebugFile", PLUGIN_ERROR_MUTEX_DEBUG_FILE)
35 { 34 {
36 s_criticalSectionDebugLock.Lock(); 35 s_criticalSectionDebugLock.Lock();
37 } 36 }
38 37
39 ~CPluginDebugLock() 38 ~CPluginDebugLock()
40 { 39 {
41 s_criticalSectionDebugLock.Unlock(); 40 s_criticalSectionDebugLock.Unlock();
42 } 41 }
43 }; 42 };
44 43
45 CComAutoCriticalSection CPluginDebugLock::s_criticalSectionDebugLock; 44 CComAutoCriticalSection CPluginDebugLock::s_criticalSectionDebugLock;
46 45
46 void CPluginDebug::DebugSystemException(const std::system_error& ex, int errorId , int errorSubid, const std::string& description)
47 {
48 std::string message = description + ", " + ex.code().message() + ", " + ex.wha t();
49 DEBUG_ERROR_LOG(ex.code().value(), errorId, errorSubid, message);
50 }
47 51
48 #ifdef ENABLE_DEBUG_INFO 52 #ifdef ENABLE_DEBUG_INFO
49 53
50 void CPluginDebug::Debug(const CString& text, DWORD dwProcessId, DWORD dwThreadI d) 54 void DebugLegacy(const CString& text, DWORD dwProcessId, DWORD dwThreadId)
Oleksandr 2015/02/27 07:50:15 Isn't this changeset about converting CStrings to
Eric 2015/02/27 14:26:56 It is, but I've split the work into two parts. Fir
51 { 55 {
52 #ifdef USE_CONSOLE 56 #ifdef USE_CONSOLE
53 CONSOLE("%s", CT2A(text.GetString(), CP_UTF8)); 57 CONSOLE("%s", CT2A(text.GetString(), CP_UTF8));
54 #endif 58 #endif
55 59
56 if (CPluginSettings::HasInstance()) 60 if (CPluginSettings::HasInstance())
57 { 61 {
58 #ifdef ENABLE_DEBUG_SPLIT_FILE 62 #ifdef ENABLE_DEBUG_SPLIT_FILE
59 CPluginSettings* settings = CPluginSettings::GetInstance(); 63 CPluginSettings* settings = CPluginSettings::GetInstance();
60 64
(...skipping 13 matching lines...) Expand all
74 } 78 }
75 if (dwThreadId == 0) 79 if (dwThreadId == 0)
76 { 80 {
77 dwThreadId = ::GetCurrentThreadId(); 81 dwThreadId = ::GetCurrentThreadId();
78 } 82 }
79 83
80 CStringA processInfo; 84 CStringA processInfo;
81 processInfo.Format("%4.4u.%4.4u - ", dwProcessId, dwThreadId); 85 processInfo.Format("%4.4u.%4.4u - ", dwProcessId, dwThreadId);
82 #endif 86 #endif
83 SYSTEMTIME st; 87 SYSTEMTIME st;
84 ::GetSystemTime(&st); 88 ::GetSystemTime(&st);
Oleksandr 2015/02/27 07:50:15 Probably easier to replace this with std::localtim
Eric 2015/02/27 14:26:56 Yes, we can do that when we rewrite this implement
85 89
86 CStringA sysTime; 90 CStringA sysTime;
87 sysTime.Format("%2.2d:%2.2d:%2.2d.%3.3d - ", st.wHour, st.wMinute, st.wSecon d, st.wMilliseconds); 91 sysTime.Format("%2.2d:%2.2d:%2.2d.%3.3d - ", st.wHour, st.wMinute, st.wSecon d, st.wMilliseconds);
88 92
89 CPluginDebugLock lock; 93 CPluginDebugLock lock;
90 if (lock.IsLocked()) 94 if (lock.IsLocked())
91 { 95 {
92 std::ofstream debugFile; 96 std::ofstream debugFile;
93 97
94 #ifdef ENABLE_DEBUG_SPLIT_FILE 98 #ifdef ENABLE_DEBUG_SPLIT_FILE
95 debugFile.open(GetDataPath(L"debug_" + processor + L".txt"), std::ios::app ); 99 debugFile.open(GetDataPath(L"debug_" + processor + L".txt"), std::ios::app );
96 #else 100 #else
97 debugFile.open(GetDataPath(L"debug.txt"), std::ios::app); 101 debugFile.open(GetDataPath(L"debug.txt"), std::ios::app);
98 #endif 102 #endif
99 int pos = 0; 103 int pos = 0;
100 CStringA line = text.Tokenize(L"\n\r", pos); 104 CStringA line = text.Tokenize(L"\n\r", pos);
Oleksandr 2015/02/27 07:50:15 Looks like just a place for wcstok_s again
Eric 2015/02/27 14:26:56 Actually, we don't need to tokenize at all here. T
101 105
102 while (pos >= 0) 106 while (pos >= 0)
103 { 107 {
104 debugFile.write(sysTime.GetBuffer(), sysTime.GetLength()); 108 debugFile.write(sysTime.GetBuffer(), sysTime.GetLength());
105 #ifndef ENABLE_DEBUG_SPLIT_FILE 109 #ifndef ENABLE_DEBUG_SPLIT_FILE
106 debugFile.write(processInfo.GetBuffer(), processInfo.GetLength()); 110 debugFile.write(processInfo.GetBuffer(), processInfo.GetLength());
107 #endif 111 #endif
108 debugFile.write(line.GetBuffer(), line.GetLength()); 112 debugFile.write(line.GetBuffer(), line.GetLength());
109 debugFile.write("\n", 1); 113 debugFile.write("\n", 1);
110 114
111 line = text.Tokenize(L"\n\r", pos); 115 line = text.Tokenize(L"\n\r", pos);
112 } 116 }
113 117
114 debugFile.flush(); 118 debugFile.flush();
115 } 119 }
116 } 120 }
117 } 121 }
118 122
119 void CPluginDebug::DebugClear() 123 void CPluginDebug::Debug(const std::string& text, DWORD processId, DWORD threadI d)
120 { 124 {
121 CPluginDebugLock lock; 125 DebugLegacy(CString(text.c_str()), processId, threadId);
122 if (lock.IsLocked()) 126 }
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 127
128 for (int i = 1; i <= 10; i++) 128 void CPluginDebug::Debug(const std::wstring& text, DWORD processId, DWORD thread Id)
129 { 129 {
130 std::wstring x = std::to_wstring(i); 130 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 } 131 }
136 132
137 #endif 133 #endif
138 134
139 #if (defined ENABLE_DEBUG_INFO || defined ENABLE_DEBUG_SELFTEST) 135 #if (defined ENABLE_DEBUG_INFO)
140 136
141 void CPluginDebug::DebugError(const CString& error) 137 void CPluginDebug::DebugException(const std::exception& ex)
142 { 138 {
139 auto error = std::string("!!! Exception:") + ex.what();
143 #ifdef ENABLE_DEBUG_ERROR 140 #ifdef ENABLE_DEBUG_ERROR
144 Debug(error); 141 Debug(error);
145 #endif 142 #endif
146 143
147 DEBUG_SELFTEST("************************************************************** ******************\n" + error + "\n********************************************* ***********************************") 144 DEBUG_SELFTEST("************************************************************** ******************\n" + error + "\n********************************************* ***********************************")
148 } 145 }
149 146
150 void CPluginDebug::DebugErrorCode(DWORD errorCode, const CString& error, DWORD d wProcessId, DWORD dwThreadId) 147 void DebugErrorCodeLegacy(DWORD errorCode, const CString& error, DWORD dwProcess Id, DWORD dwThreadId)
Oleksandr 2015/02/27 07:50:15 Same here. Why do we need legacy functions after t
Eric 2015/02/27 14:26:56 See above. Same reason.
151 { 148 {
152 CString errorCodeText; 149 CString errorCodeText;
153 errorCodeText.Format(L"%u (0x%8.8x)", errorCode, errorCode); 150 errorCodeText.Format(L"%u (0x%8.8x)", errorCode, errorCode);
154 151
155 CString finalError = error + L". error=" + errorCodeText; 152 CString finalError = error + L". error=" + errorCodeText;
156 153
157 #ifdef ENABLE_DEBUG_ERROR 154 #ifdef ENABLE_DEBUG_ERROR
158 Debug(finalError, dwProcessId, dwThreadId); 155 DebugLegacy(finalError, dwProcessId, dwThreadId);
159 #endif 156 #endif
160 157
161 DEBUG_SELFTEST(L"************************************************************* *******************\n" + finalError + "\n*************************************** *****************************************") 158 DEBUG_SELFTEST(L"************************************************************* *******************\n" + finalError + "\ n************************************** ******************************************")
159 }
160
161 void CPluginDebug::DebugErrorCode(DWORD errorCode, const std::string& error, DWO RD processId, DWORD threadId)
162 {
163 DebugErrorCodeLegacy(errorCode, CString(error.c_str()), processId, threadId);
162 } 164 }
163 165
164 #endif 166 #endif
165 167
166 // ============================================================================ 168 // ============================================================================
167 // Debug result 169 // Debug result
168 // ============================================================================ 170 // ============================================================================
169 171
170 #ifdef ENABLE_DEBUG_RESULT 172 #ifdef ENABLE_DEBUG_RESULT
171 173
172 void CPluginDebug::DebugResult(const CString& text) 174 void DebugResultLegacy(const CString& text)
173 { 175 {
174 SYSTEMTIME st; 176 SYSTEMTIME st;
175 ::GetSystemTime(&st); 177 ::GetSystemTime(&st);
176 178
177 CStringA sysTime; 179 CStringA sysTime;
178 sysTime.Format("%2.2d:%2.2d:%2.2d.%3.3d - ", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); 180 sysTime.Format("%2.2d:%2.2d:%2.2d.%3.3d - ", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
179 181
180 CStringA textA = text; 182 CStringA textA = text;
181 183
182 CPluginDebugLock lock; 184 CPluginDebugLock lock;
183 if (lock.IsLocked()) 185 if (lock.IsLocked())
184 { 186 {
185 std::ofstream debugFile; 187 std::ofstream debugFile;
186 188
187 debugFile.open(GetDataPath(L"debug_result.txt"), std::ios::app); 189 debugFile.open(GetDataPath(L"debug_result.txt"), std::ios::app);
188 debugFile.write(sysTime.GetBuffer(), sysTime.GetLength()); 190 debugFile.write(sysTime.GetBuffer(), sysTime.GetLength());
189 debugFile.write(LPCSTR(textA), textA.GetLength()); 191 debugFile.write(LPCSTR(textA), textA.GetLength());
190 debugFile.write("\n", 1); 192 debugFile.write("\n", 1);
191 debugFile.flush(); 193 debugFile.flush();
192 } 194 }
193 } 195 }
194 196
195 void CPluginDebug::DebugResultDomain(const CString& domain) 197 void CPluginDebug::DebugResult(const std::wstring& text)
198 {
199 DebugResultLegacy(ToCString(text));
200 }
201
202 void CPluginDebug::DebugResultDomain(const std::wstring& domain)
196 { 203 {
197 DebugResult(L"================================================================ ================================================================================ ==========================================="); 204 DebugResult(L"================================================================ ================================================================================ ===========================================");
198 DebugResult(domain); 205 DebugResult(domain);
199 DebugResult(L"================================================================ ================================================================================ ==========================================="); 206 DebugResult(L"================================================================ ================================================================================ ===========================================");
200 } 207 }
201 208
202 209
203 void CPluginDebug::DebugResultBlocking(const CString& type, const std::wstring& src, const std::wstring& domain) 210 void CPluginDebug::DebugResultBlocking(const std::wstring& type, const std::wstr ing& src, const std::wstring& domain)
204 { 211 {
205 CString srcTrunc = ToCString(src); 212 CString srcTrunc = ToCString(src);
206 if (src.length() > 100) 213 if (src.length() > 100)
207 { 214 {
208 srcTrunc = srcTrunc.Left(67) + L"..." + srcTrunc.Right(30); 215 srcTrunc = srcTrunc.Left(67) + L"..." + srcTrunc.Right(30);
209 } 216 }
210 217
211 CString blocking; 218 CString blocking;
212 blocking.Format(L"Blocked %-12s %-20s %s", type, domain.empty()? L"-" : ToC String(domain), srcTrunc); 219 blocking.Format(L"Blocked %-12s %-20s %s", ToCString(type), domain.empty()? L"-" : ToCString(domain), srcTrunc);
213 220
214 DebugResult(blocking); 221 DebugResultLegacy(blocking);
215 } 222 }
216 223
217 224
218 void CPluginDebug::DebugResultHiding(const CString& tag, const CString& id, cons t CString& filter) 225 void CPluginDebug::DebugResultHiding(const std::wstring& tag, const std::wstring & id, const std::wstring& filter)
219 { 226 {
220 CString srcTrunc = id; 227 CString srcTrunc = ToCString(id);
221 if (srcTrunc.GetLength() > 100) 228 if (srcTrunc.GetLength() > 100)
222 { 229 {
223 srcTrunc = srcTrunc.Left(67) + L"..." + srcTrunc.Right(30); 230 srcTrunc = srcTrunc.Left(67) + L"..." + srcTrunc.Right(30);
224 } 231 }
225 232
226 CString blocking; 233 CString blocking;
227 blocking.Format(L"Hidden %-12s - %s %s", tag, srcTrunc, filter); 234 blocking.Format(L"Hidden %-12s - %s %s", ToCString(tag), srcTrunc, ToCStri ng(filter));
228 235
229 DebugResult(blocking); 236 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 } 237 }
241 238
242 #endif // ENABLE_DEBUG_RESULT 239 #endif // ENABLE_DEBUG_RESULT
243 240
244 241
245 #ifdef ENABLE_DEBUG_RESULT_IGNORED 242 #ifdef ENABLE_DEBUG_RESULT_IGNORED
246 243
247 void CPluginDebug::DebugResultIgnoring(const CString& type, const std::wstring& src, const std::wstring& domain) 244 void CPluginDebug::DebugResultIgnoring(const std::wstring& type, const std::wstr ing& src, const std::wstring& domain)
248 { 245 {
249 CString srcTrunc = ToCString(src); 246 CString srcTrunc = ToCString(src);
250 if (src.length() > 100) 247 if (src.length() > 100)
251 { 248 {
252 srcTrunc = srcTrunc.Left(67) + L"..." + srcTrunc.Right(30); 249 srcTrunc = srcTrunc.Left(67) + L"..." + srcTrunc.Right(30);
253 } 250 }
254 251
255 CString blocking; 252 CString blocking;
256 blocking.Format(L"Ignored %-12s %s %s", type, domain.empty()? L"-" : ToCStr ing(domain), srcTrunc); 253 blocking.Format(L"Ignored %-12s %s %s", ToCString(type), domain.empty()? L" -" : ToCString(domain), srcTrunc);
257 254
258 DebugResult(blocking); 255 DebugResultLegacy(blocking);
259 } 256 }
260 257
261 #endif // ENABLE_DEBUG_RESULT_IGNORED 258 #endif // ENABLE_DEBUG_RESULT_IGNORED
OLDNEW

Powered by Google App Engine
This is Rietveld