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

Side by Side Diff: src/plugin/Console.h

Issue 5187613258416128: Issue #1234 - Rewrite internals of debug facility (Closed)
Patch Set: Created March 12, 2015, 10:52 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
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 /*
19 Macros CONSOLE, CONSOLE_WARN, CONSOLE_ERROR are used for Console output
20 CONSOLE_WAIT waits for user to hit Enter
21 In order to see Console output macro USE_CONSOLE should be defined before Cons ole.h is included.
22 Since Console.h is added in PluginStdAfx.h, then 2 ways to log in Console oupu t:
23 1. Uncomment "#define USE_CONSOLE" in PluginStdAfx.h - Console ouput will be a vailable in all files.
24 2. Add "#define USE_CONSOLE" as very first line of a cpp file - Console ouput will be available in this file
25 */
26
27 #pragma once
28
29 #if defined(USE_CONSOLE)
30
31 #include <Strsafe.h>
32
33 enum {eLog = 0, eWarn = 1, eError = 2};
34
35 static long g_consoleCount = -1;
36 static bool s_hasError = false;
37
38 void CONSOLE(const char* format, ...);
39
40 inline void WritelnToConsole(int code, int count, const char* format, va_list ar gs)
41 {
42 // In an exe application, if there is an error and exe exists, Console disap pears and error or warning are not visible
43 // On exit destructor will be called and Console waits for keyboard input.
44 struct ConsoleWaitReturn
45 {
46 ConsoleWaitReturn(){}
47 ~ConsoleWaitReturn()
48 {
49 if (s_hasError)
50 {
51 CONSOLE("Hit 'ENTER' to exit");
52
53 HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
54
55 wchar_t buf[128];
56 DWORD nRead = 0;
57 ReadConsole(hInput, buf, countof(buf), &nRead, 0);
58 }
59 }
60 } static s_ConsoleWaitReturn;
61
62 DWORD lastError = GetLastError();
63
64 if (eError == code)
65 {
66 s_hasError = true;
67 }
68
69 static bool s_log = false;
70 if (!s_log)
71 {
72 s_log = true;
73
74 char log[] = "Log \"";
75
76 const int lenLog = sizeof(log) - 1;
77
78 char title[lenLog + MAX_PATH + 2];
79 strcpy_s(title, sizeof(title), log);
80
81 char* pFileName = title + lenLog;
82
83 if (GetModuleFileNameA(0, pFileName, MAX_PATH))
84 {
85 char* pDelim = strrchr(pFileName, '\\') + 1;
86 if (pDelim)
87 {
88 strcpy_s(pFileName, sizeof(title) - lenLog, pDelim);
89 }
90 //Explicit conversion here, to disable compilation wr
91 size_t lenFileName = strlen(pFileName);
92 *(pFileName + lenFileName) = '"';
93 *(pFileName + lenFileName + 1) = 0;
94 }
95
96 AllocConsole();
97 SetConsoleTitleA(title);
98 }
99
100 SYSTEMTIME systemTime;
101 GetLocalTime(&systemTime);
102
103 char buf[1024];
104 sprintf_s(buf, countof(buf), "%d [%.2d:%.2d:%.2d] {%d} %s\n", count, systemT ime.wMinute, systemTime.wSecond, systemTime.wMilliseconds, GetCurrentThreadId(), format);
105
106 HANDLE hError = GetStdHandle(STD_ERROR_HANDLE);
107
108 WORD color;
109 if (eLog == code)
110 {
111 color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
112 }
113 else if (eWarn == code)
114 {
115 color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY ;
116 } else if (eError == code)
117 {
118 color = FOREGROUND_RED | FOREGROUND_INTENSITY;
119 }
120
121 SetConsoleTextAttribute(hError, color);
122
123 char out[4096];
124 StringCbVPrintfA(out, sizeof(out), buf, args);
125
126 DWORD dwOutput;
127 WriteConsoleA(hError, out, (DWORD)strlen(out), &dwOutput, 0);
128 FlushConsoleInputBuffer(hError);
129
130 SetLastError(lastError);
131 }
132
133
134 inline void CONSOLE(const char* format, ...)
135 {
136 InterlockedIncrement(&g_consoleCount);
137
138 va_list args;
139 va_start(args, format);
140 WritelnToConsole(eLog, g_consoleCount, format, args);
141 va_end(args);
142 }
143
144 inline void CONSOLE_WARN(const char* format, ...)
145 {
146 va_list args;
147 va_start(args, format);
148 WritelnToConsole(eWarn, g_consoleCount, format, args);
149 va_end(args);
150 }
151
152 inline void CONSOLE_ERROR(const char* format, ...)
153 {
154 va_list args;
155 va_start(args, format);
156 WritelnToConsole(eError, g_consoleCount, format, args);
157 va_end(args);
158 }
159
160 inline void CONSOLE_WAIT(const char* format = "", ...)
161 {
162 if (format && 0 != format[0])
163 {
164 InterlockedIncrement(&g_consoleCount);
165
166 va_list args;
167 va_start(args, format);
168 WritelnToConsole(eLog, g_consoleCount, format, args);
169 va_end(args);
170 }
171
172 CONSOLE("Hit 'ENTER' to continue");
173 wchar_t buf[128];
174 DWORD nRead = 0;
175 ReadConsole(GetStdHandle(STD_INPUT_HANDLE), buf, countof(buf), &nRead, 0 );
176 }
177
178
179 #else
180 int RemoveConsole(...);
181
182 #define CONSOLE sizeof RemoveConsole
183 #define CONSOLE_WARN sizeof RemoveConsole
184 #define CONSOLE_ERROR sizeof RemoveConsole
185 #define CONSOLE_WAIT sizeof RemoveConsole
186 #endif
187
OLDNEW
« no previous file with comments | « adblockplus.gyp ('k') | src/plugin/PluginClass.cpp » ('j') | src/plugin/PluginClass.cpp » ('J')

Powered by Google App Engine
This is Rietveld