OLD | NEW |
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 #ifndef _PLUGIN_DEBUG_H_ | 18 #ifndef _PLUGIN_DEBUG_H_ |
19 #define _PLUGIN_DEBUG_H_ | 19 #define _PLUGIN_DEBUG_H_ |
20 | 20 |
| 21 #include <memory> |
| 22 #include <mutex> |
| 23 |
| 24 namespace Trace |
| 25 { |
| 26 /** |
| 27 */ |
| 28 struct Location |
| 29 { |
| 30 std::string prefixText; |
| 31 std::string postfixText; |
| 32 |
| 33 Location() // = default |
| 34 : prefixText(), postfixText() |
| 35 {}; |
| 36 |
| 37 Location(int id, int subId); |
| 38 |
| 39 Location(std::string postfixText) |
| 40 : prefixText(), postfixText(postfixText) |
| 41 {} |
| 42 |
| 43 Location(std::string prefixText, std::string postfixText) |
| 44 : prefixText(prefixText), postfixText(postfixText) |
| 45 {} |
| 46 }; |
| 47 |
| 48 void TextFixed(const std::string& description, Trace::Location location); |
| 49 void ErrorCode(DWORD errorCode, const std::string& description, Trace::Locatio
n location); |
| 50 void SystemException(const std::system_error& ex, const std::string& descripti
on, Trace::Location location); |
| 51 } |
| 52 |
| 53 // Cope with insufficient support on old toolsets |
| 54 #if !defined(__func__) && defined(_MSC_VER) |
| 55 #define __func__ __FUNCTION__ |
| 56 #endif |
| 57 |
| 58 // __LINE__ expands to an integer literal, not a string literal |
| 59 // The stringify operator "#" applies only to arguments, not macro definitions |
| 60 #define STRING_EXPAND(x) STRINGIFY(x) |
| 61 #define STRINGIFY(x) #x |
| 62 #define __LINE_STRING__ STRING_EXPAND(__LINE__) |
| 63 #define HERE Trace::Location(__FILE__ ":" __LINE_STRING__) |
| 64 #define HERE_F Trace::Location(__func__, __FILE__ ":" __LINE_STRING__) |
| 65 |
| 66 #ifdef _DEBUG |
| 67 #define TRACE(description, location) Trace::TextFixed(description, location) |
| 68 #else |
| 69 #define TRACE(a,b) |
| 70 #endif |
| 71 |
21 class CPluginDebug | 72 class CPluginDebug |
22 { | 73 { |
23 | |
24 public: | 74 public: |
25 static void DebugSystemException(const std::system_error& ex, int errorId, int
errorSubid, const std::string& description); | |
26 | |
27 #if (defined ENABLE_DEBUG_INFO) | 75 #if (defined ENABLE_DEBUG_INFO) |
28 static void Debug(const std::string& text); | 76 static void Debug(const std::string& text); |
29 static void Debug(const std::wstring& text); | 77 static void Debug(const std::wstring& text); |
30 static void DebugException(const std::exception& ex); | 78 static void DebugException(const std::exception& ex, Trace::Location location)
; |
31 static void DebugErrorCode(DWORD errorCode, const std::string& error, DWORD pr
ocessId=0, DWORD threadId=0); | 79 static void DebugErrorCode(DWORD errorCode, const std::string& error, Trace::L
ocation location); |
32 #endif | 80 #endif |
33 | 81 |
34 #if (defined ENABLE_DEBUG_RESULT) | 82 #if (defined ENABLE_DEBUG_RESULT) |
35 static void DebugResult(const std::wstring& text); | 83 static void DebugResult(const std::wstring& text); |
36 static void DebugResultDomain(const std::wstring& domain); | 84 static void DebugResultDomain(const std::wstring& domain); |
37 static void DebugResultBlocking(const std::wstring& type, const std::wstring&
src, const std::wstring& domain); | 85 static void DebugResultBlocking(const std::wstring& type, const std::wstring&
src, const std::wstring& domain); |
38 static void DebugResultHiding(const std::wstring& tag, const std::wstring& id,
const std::wstring& filter); | 86 static void DebugResultHiding(const std::wstring& tag, const std::wstring& id,
const std::wstring& filter); |
39 #endif | 87 #endif |
40 | 88 |
41 #if (defined ENABLE_DEBUG_RESULT_IGNORED) | 89 #if (defined ENABLE_DEBUG_RESULT_IGNORED) |
42 static void DebugResultIgnoring(const std::wstring& type, const std::wstring&
src, const std::wstring& domain); | 90 static void DebugResultIgnoring(const std::wstring& type, const std::wstring&
src, const std::wstring& domain); |
43 #endif | 91 #endif |
44 }; | 92 }; |
45 | 93 |
46 /** | 94 /** |
47 * Convert a pointer to a hexadecimal literal (0x...) | 95 * Convert a pointer to a hexadecimal literal (0x...) |
48 * | 96 * |
49 * The length of the literal varies with pointer size: | 97 * The length of the literal varies with pointer size: |
50 * 10 characters for WIN32, and 18 for WIN64. | 98 * 10 characters for WIN32, and 18 for WIN64. |
51 */ | 99 */ |
52 std::wstring ToHexLiteral(const void*); | 100 std::wstring ToHexLiteral(const void*); |
53 | 101 |
| 102 /* |
| 103 * Forward declaration. |
| 104 */ |
| 105 class LogQueue; |
| 106 |
| 107 /** |
| 108 * This class maintains a singleton LogQueue in existence. |
| 109 * |
| 110 * This class exists because it's not possible to use a static declaration for |
| 111 * 'std::thread' or any class that contains one, such as 'ActiveQueue'. |
| 112 * To appreciate of the origin of restriction, see |
| 113 * http://stackoverflow.com/questions/28746016/thread-join-does-not-return-whe
n-called-in-global-var-destructor |
| 114 * In addition, we have no main() function to use for initialization, |
| 115 * since we're packaged as a COM class provider. |
| 116 * |
| 117 * As a substitute, we maintain a reference to the queue in each instance of |
| 118 * 'CPluginClass', the sole visible COM class, the one that implements the |
| 119 * BHO interface. |
| 120 * This class has the responsibility for managing the life cycle of the queue, |
| 121 * ensuring it exists whenever there's a reference to it. |
| 122 * The lifespan of instances of 'CPluginClass' encompasses the lifespans of any |
| 123 * other objects that the BHO uses, so it's sufficient to maintain a LogQueue |
| 124 * in existence with a reference in that class. |
| 125 * |
| 126 * The queue itself is accessible only to the logging functions; |
| 127 * it need not be visible externally. |
| 128 * Hence all this class requires is an opaque, forward declaration. |
| 129 * Mere instantiation of this class is sufficient to ensure we have a log queue. |
| 130 */ |
| 131 class LogQueueReference |
| 132 { |
| 133 friend LogQueue; |
| 134 |
| 135 typedef std::shared_ptr<LogQueue> pointer; |
| 136 |
| 137 /** |
| 138 * The class-wide reference to the debug queue. |
| 139 */ |
| 140 static pointer queueMasterReference; |
| 141 |
| 142 /** |
| 143 * The instance reference to the debug queue. |
| 144 */ |
| 145 pointer queueReference; |
| 146 |
| 147 /** |
| 148 * Mutex to allow joint serialization of constructor/destructor calls. |
| 149 */ |
| 150 static std::mutex mutex; |
| 151 |
| 152 public: |
| 153 LogQueueReference(); |
| 154 ~LogQueueReference(); |
| 155 }; |
| 156 |
54 #endif // _PLUGIN_DEBUG_H_ | 157 #endif // _PLUGIN_DEBUG_H_ |
OLD | NEW |