Update Dutil_TraceErrorSource to filter based on the report level.
Sean Hall committed
Jul 12, 2020 at 17:07 UTC
dc558da002794cc07013e8376f3c55c73391aa0e
15 files changed
+142
-24
src/dutil/dutil.cpp
+6
@@ -408,6 +408,12 @@ DAPIV_(void) Dutil_TraceErrorSource(
408
...
409
)
410
{
411
+ // if this is NOT an error report and we're not logging at this level, bail
412
+ if (REPORT_ERROR != rl && Dutil_rlCurrentTrace < rl)
413
+ {
414
+ return;
415
+ }
416
+
417
if (DUTIL_SOURCE_UNKNOWN != source && vpfnTraceErrorCallback)
418
{
419
va_list args;
src/dutil/inc/dutil.h
+1
-12
@@ -10,17 +10,6 @@
10
#define DAPIV_(type) EXTERN_C type DAPIV
11
12
13
-// enums
14
-typedef enum REPORT_LEVEL
15
-{
16
- REPORT_NONE, // turns off report (only valid for XXXSetLevel())
17
- REPORT_WARNING, // written if want only warnings or reporting is on in general
18
- REPORT_STANDARD, // written if reporting is on
19
- REPORT_VERBOSE, // written only if verbose reporting is on
20
- REPORT_DEBUG, // reporting useful when debugging code
21
- REPORT_ERROR, // always gets reported, but can never be specified
22
-} REPORT_LEVEL;
23
-
13
// asserts and traces
14
typedef BOOL (DAPI *DUTIL_ASSERTDISPLAYFUNCTION)(__in_z LPCSTR sz);
15
@@ -29,7 +18,7 @@ typedef void (CALLBACK *DUTIL_CALLBACK_TRACEERROR)(
18
__in int iLine,
19
__in REPORT_LEVEL rl,
20
__in UINT source,
32
- __in HRESULT hr,
21
+ __in HRESULT hrError,
22
__in_z __format_string LPCSTR szFormat,
23
__in va_list args
24
);
src/dutil/inc/dutilsources.h
+10
@@ -63,3 +63,13 @@ typedef enum DUTIL_SOURCE
63
64
DUTIL_SOURCE_EXTERNAL = 256,
65
} DUTIL_SOURCE;
66
+
67
+typedef enum REPORT_LEVEL
68
+{
69
+ REPORT_NONE, // turns off report (only valid for XXXSetLevel())
70
+ REPORT_WARNING, // written if want only warnings or reporting is on in general
71
+ REPORT_STANDARD, // written if reporting is on
72
+ REPORT_VERBOSE, // written only if verbose reporting is on
73
+ REPORT_DEBUG, // reporting useful when debugging code
74
+ REPORT_ERROR, // always gets reported, but can never be specified
75
+} REPORT_LEVEL;
src/test/DUtilUnitTest/DUtilTests.cpp
new
+35
@@ -0,0 +1,35 @@
1
+// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
+
3
+#include "precomp.h"
4
+
5
+using namespace System;
6
+using namespace Xunit;
7
+using namespace WixBuildTools::TestSupport;
8
+
9
+namespace DutilTests
10
+{
11
+ public ref class DUtil
12
+ {
13
+ public:
14
+ [Fact]
15
+ void DUtilTraceErrorSourceFiltersOnTraceLevel()
16
+ {
17
+ DutilInitialize(&DutilTestTraceError);
18
+
19
+ CallDutilTraceErrorSource();
20
+
21
+ Dutil_TraceSetLevel(REPORT_DEBUG, FALSE);
22
+
23
+ Action^ action = gcnew Action(this, &DUtil::CallDutilTraceErrorSource);
24
+ Assert::Throws<Exception^>(action);
25
+
26
+ DutilUninitialize();
27
+ }
28
+
29
+ private:
30
+ void CallDutilTraceErrorSource()
31
+ {
32
+ Dutil_TraceErrorSource(__FILE__, __LINE__, REPORT_DEBUG, DUTIL_SOURCE_EXTERNAL, E_FAIL, "Error message");
33
+ }
34
+ };
35
+}
src/test/DUtilUnitTest/DUtilUnitTest.vcxproj
+3
-1
@@ -36,6 +36,8 @@
36
<ClCompile Include="AssemblyInfo.cpp" />
37
<ClCompile Include="DictUtilTest.cpp" />
38
<ClCompile Include="DirUtilTests.cpp" />
39
+ <ClCompile Include="DUtilTests.cpp" />
40
+ <ClCompile Include="error.cpp" />
41
<ClCompile Include="FileUtilTest.cpp" />
42
<ClCompile Include="GuidUtilTest.cpp" />
43
<ClCompile Include="IniUtilTest.cpp" />
@@ -92,4 +94,4 @@
94
<Error Condition="!Exists('..\..\..\packages\xunit.core.2.4.1\build\xunit.core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\xunit.core.2.4.1\build\xunit.core.targets'))" />
95
<Error Condition="!Exists('..\..\..\packages\xunit.runner.visualstudio.2.4.1\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\xunit.runner.visualstudio.2.4.1\build\net20\xunit.runner.visualstudio.props'))" />
96
</Target>
95
-</Project>
97
+</Project>
\ No newline at end of file
src/test/DUtilUnitTest/DUtilUnitTest.vcxproj.filters
+9
@@ -24,6 +24,12 @@
24
<ClCompile Include="DirUtilTests.cpp">
25
<Filter>Source Files</Filter>
26
</ClCompile>
27
+ <ClCompile Include="DUtilTests.cpp">
28
+ <Filter>Source Files</Filter>
29
+ </ClCompile>
30
+ <ClCompile Include="error.cpp">
31
+ <Filter>Source Files</Filter>
32
+ </ClCompile>
33
<ClCompile Include="FileUtilTest.cpp">
34
<Filter>Source Files</Filter>
35
</ClCompile>
@@ -42,6 +48,9 @@
48
<ClCompile Include="PathUtilTest.cpp">
49
<Filter>Source Files</Filter>
50
</ClCompile>
51
+ <ClCompile Include="precomp.cpp">
52
+ <Filter>Source Files</Filter>
53
+ </ClCompile>
54
<ClCompile Include="StrUtilTest.cpp">
55
<Filter>Source Files</Filter>
56
</ClCompile>
src/test/DUtilUnitTest/DictUtilTest.cpp
+4
@@ -22,6 +22,8 @@ namespace DutilTests
22
[Fact]
23
void DictUtilTest()
24
{
25
+ DutilInitialize(&DutilTestTraceError);
26
+
27
EmbeddedKeyTestHelper(DICT_FLAG_NONE, numIterations);
28
29
EmbeddedKeyTestHelper(DICT_FLAG_CASEINSENSITIVE, numIterations);
@@ -29,6 +31,8 @@ namespace DutilTests
31
StringListTestHelper(DICT_FLAG_NONE, numIterations);
32
33
StringListTestHelper(DICT_FLAG_CASEINSENSITIVE, numIterations);
34
+
35
+ DutilUninitialize();
36
}
37
38
private:
src/test/DUtilUnitTest/FileUtilTest.cpp
+3
@@ -18,6 +18,8 @@ namespace DutilTests
18
LPWSTR sczTempDir = NULL;
19
LPWSTR sczFileDir = NULL;
20
21
+ DutilInitialize(&DutilTestTraceError);
22
+
23
try
24
{
25
hr = PathExpand(&sczTempDir, L"%TEMP%\\FileUtilTest\\", PATH_EXPAND_ENVIRONMENT);
@@ -43,6 +45,7 @@ namespace DutilTests
45
{
46
ReleaseStr(sczTempDir);
47
ReleaseStr(sczFileDir);
48
+ DutilUninitialize();
49
}
50
}
51
src/test/DUtilUnitTest/IniUtilTest.cpp
+3
@@ -24,6 +24,8 @@ namespace DutilTests
24
LPWSTR wzIniContents = L" PlainValue = \t Blah \r\n;CommentHere\r\n[Section1]\r\n ;Another Comment With = Equal Sign\r\nSection1ValueA=Foo\r\n\r\nSection1ValueB=Bar\r\n[Section2]\r\nSection2ValueA=Cha\r\nArray[0]=Arr\r\n";
25
LPWSTR wzScriptContents = L"setf ~PlainValue Blah\r\n;CommentHere\r\n\r\nsetf ~Section1\\Section1ValueA Foo\r\n\r\nsetf ~Section1\\Section1ValueB Bar\r\nsetf ~Section2\\Section2ValueA Cha\r\nsetf ~Section2\\Array[0] Arr\r\n";
26
27
+ DutilInitialize(&DutilTestTraceError);
28
+
29
try
30
{
31
hr = PathExpand(&sczTempIniFilePath, L"%TEMP%\\IniUtilTest\\Test.ini", PATH_EXPAND_ENVIRONMENT);
@@ -58,6 +60,7 @@ namespace DutilTests
60
{
61
ReleaseStr(sczTempIniFilePath);
62
ReleaseStr(sczTempIniFileDir);
63
+ DutilUninitialize();
64
}
65
}
66
src/test/DUtilUnitTest/MemUtilTest.cpp
+15
-1
@@ -27,6 +27,8 @@ namespace DutilTests
27
ArrayValue *rgValues = NULL;
28
DWORD cValues = 0;
29
30
+ DutilInitialize(&DutilTestTraceError);
31
+
32
try
33
{
34
hr = MemEnsureArraySize(reinterpret_cast<LPVOID*>(&rgValues), cValues + 1, sizeof(ArrayValue), 5);
@@ -101,7 +103,7 @@ namespace DutilTests
103
}
104
105
LExit:
104
- return;
106
+ DutilUninitialize();
107
}
108
109
[Fact]
@@ -111,6 +113,8 @@ namespace DutilTests
113
ArrayValue *rgValues = NULL;
114
DWORD cValues = 0;
115
116
+ DutilInitialize(&DutilTestTraceError);
117
+
118
try
119
{
120
hr = MemInsertIntoArray(reinterpret_cast<LPVOID*>(&rgValues), 0, 1, cValues + 1, sizeof(ArrayValue), 5);
@@ -180,6 +184,7 @@ namespace DutilTests
184
finally
185
{
186
ReleaseMem(rgValues);
187
+ DutilUninitialize();
188
}
189
}
190
@@ -190,6 +195,8 @@ namespace DutilTests
195
ArrayValue *rgValues = NULL;
196
DWORD cValues = 0;
197
198
+ DutilInitialize(&DutilTestTraceError);
199
+
200
try
201
{
202
hr = MemEnsureArraySize(reinterpret_cast<LPVOID*>(&rgValues), 10, sizeof(ArrayValue), 10);
@@ -252,6 +259,7 @@ namespace DutilTests
259
finally
260
{
261
ReleaseMem(rgValues);
262
+ DutilUninitialize();
263
}
264
}
265
@@ -262,6 +270,8 @@ namespace DutilTests
270
ArrayValue *rgValues = NULL;
271
DWORD cValues = 0;
272
273
+ DutilInitialize(&DutilTestTraceError);
274
+
275
try
276
{
277
hr = MemEnsureArraySize(reinterpret_cast<LPVOID*>(&rgValues), 10, sizeof(ArrayValue), 10);
@@ -326,6 +336,7 @@ namespace DutilTests
336
finally
337
{
338
ReleaseMem(rgValues);
339
+ DutilUninitialize();
340
}
341
}
342
@@ -336,6 +347,8 @@ namespace DutilTests
347
ArrayValue *rgValues = NULL;
348
DWORD cValues = 0;
349
350
+ DutilInitialize(&DutilTestTraceError);
351
+
352
try
353
{
354
hr = MemEnsureArraySize(reinterpret_cast<LPVOID*>(&rgValues), 10, sizeof(ArrayValue), 10);
@@ -424,6 +437,7 @@ namespace DutilTests
437
finally
438
{
439
ReleaseMem(rgValues);
440
+ DutilUninitialize();
441
}
442
}
443
src/test/DUtilUnitTest/StrUtilTest.cpp
+12
-4
@@ -86,6 +86,8 @@ namespace DutilTests
86
HRESULT hr = S_OK;
87
LPWSTR sczOutput = NULL;
88
89
+ DutilInitialize(&DutilTestTraceError);
90
+
91
try
92
{
93
hr = StrTrimWhitespace(&sczOutput, wzInput);
@@ -103,7 +105,7 @@ namespace DutilTests
105
}
106
107
LExit:
106
- return;
108
+ DutilUninitialize();
109
}
110
111
void TestTrimAnsi(LPCSTR szInput, LPCSTR szExpectedResult)
@@ -111,6 +113,8 @@ namespace DutilTests
113
HRESULT hr = S_OK;
114
LPSTR sczOutput = NULL;
115
116
+ DutilInitialize(&DutilTestTraceError);
117
+
118
try
119
{
120
hr = StrAnsiTrimWhitespace(&sczOutput, szInput);
@@ -128,7 +132,7 @@ namespace DutilTests
132
}
133
134
LExit:
131
- return;
135
+ DutilUninitialize();
136
}
137
138
void TestStrAllocStringAnsi(LPCSTR szSource, DWORD cchSource, LPCWSTR wzExpectedResult)
@@ -136,6 +140,8 @@ namespace DutilTests
140
HRESULT hr = S_OK;
141
LPWSTR sczOutput = NULL;
142
143
+ DutilInitialize(&DutilTestTraceError);
144
+
145
try
146
{
147
hr = StrAllocStringAnsi(&sczOutput, szSource, cchSource, CP_UTF8);
@@ -153,7 +159,7 @@ namespace DutilTests
159
}
160
161
LExit:
156
- return;
162
+ DutilUninitialize();
163
}
164
165
void TestStrAnsiAllocString(LPWSTR wzSource, DWORD cchSource, LPCSTR szExpectedResult)
@@ -161,6 +167,8 @@ namespace DutilTests
167
HRESULT hr = S_OK;
168
LPSTR sczOutput = NULL;
169
170
+ DutilInitialize(&DutilTestTraceError);
171
+
172
try
173
{
174
hr = StrAnsiAllocString(&sczOutput, wzSource, cchSource, CP_UTF8);
@@ -178,7 +186,7 @@ namespace DutilTests
186
}
187
188
LExit:
181
- return;
189
+ DutilUninitialize();
190
}
191
};
192
}
src/test/DUtilUnitTest/UriUtilTest.cpp
+3
-1
@@ -17,6 +17,8 @@ namespace CfgTests
17
{
18
HRESULT hr = S_OK;
19
20
+ DutilInitialize(&DutilTestTraceError);
21
+
22
LPCWSTR uri = L"https://localhost/";
23
URI_PROTOCOL uriProtocol = URI_PROTOCOL::URI_PROTOCOL_UNKNOWN;
24
hr = UriProtocol(uri, &uriProtocol);
@@ -90,7 +92,7 @@ namespace CfgTests
92
Assert::Equal((int)URI_PROTOCOL::URI_PROTOCOL_FTP, (int)uriProtocol);
93
94
LExit:
93
- ;
95
+ DutilUninitialize();
96
}
97
};
98
}
src/test/DUtilUnitTest/error.cpp
new
+26
@@ -0,0 +1,26 @@
1
+// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
+
3
+#include "precomp.h"
4
+
5
+const int ERROR_STRING_BUFFER = 1024;
6
+
7
+static char szMsg[ERROR_STRING_BUFFER];
8
+static WCHAR wzMsg[ERROR_STRING_BUFFER];
9
+
10
+void CALLBACK DutilTestTraceError(
11
+ __in_z LPCSTR /*szFile*/,
12
+ __in int /*iLine*/,
13
+ __in REPORT_LEVEL /*rl*/,
14
+ __in UINT source,
15
+ __in HRESULT hrError,
16
+ __in_z __format_string LPCSTR szFormat,
17
+ __in va_list args
18
+ )
19
+{
20
+ if (DUTIL_SOURCE_EXTERNAL == source)
21
+ {
22
+ ::StringCchPrintfA(szMsg, countof(szMsg), szFormat, args);
23
+ MultiByteToWideChar(CP_ACP, 0, szMsg, -1, wzMsg, countof(wzMsg));
24
+ throw gcnew System::Exception(System::String::Format("hr = 0x{0:X8}, message = {1}", hrError, gcnew System::String(wzMsg)));
25
+ }
26
+}
src/test/DUtilUnitTest/error.h
+11
-5
@@ -1,8 +1,14 @@
1
+#pragma once
2
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
3
3
-const int ERROR_STRING_BUFFER = 1024;
4
+#define DUTIL_SOURCE_DEFAULT DUTIL_SOURCE_EXTERNAL
5
5
-static char szMsg[ERROR_STRING_BUFFER];
6
-static WCHAR wzMsg[ERROR_STRING_BUFFER];
7
-
8
-#define ExitTrace(x, f, ...) { HRESULT hrTemp = x; hr = ::StringCchPrintfA(szMsg, countof(szMsg), f, __VA_ARGS__); MultiByteToWideChar(CP_ACP, 0, szMsg, -1, wzMsg, countof(wzMsg)); throw gcnew System::Exception(System::String::Format("hr = 0x{0:X8}, message = {1}", hrTemp, gcnew System::String(wzMsg))); }
6
+void CALLBACK DutilTestTraceError(
7
+ __in_z LPCSTR szFile,
8
+ __in int iLine,
9
+ __in REPORT_LEVEL rl,
10
+ __in UINT source,
11
+ __in HRESULT hrError,
12
+ __in_z __format_string LPCSTR szFormat,
13
+ __in va_list args
14
+ );
src/test/DUtilUnitTest/precomp.h
+1
@@ -7,6 +7,7 @@
7
#include <ShlObj.h>
8
9
// Include error.h before dutil.h
10
+#include <dutilsources.h>
11
#include "error.h"
12
#include <dutil.h>
13