]> git.proxmox.com Git - mirror_edk2.git/blame - UnitTestFrameworkPkg/Library/UnitTestLib/Log.c
UnitTestFrameworkPkg: Apply uncrustify changes
[mirror_edk2.git] / UnitTestFrameworkPkg / Library / UnitTestLib / Log.c
CommitLineData
0eb52298
MK
1/**\r
2 Implemnet UnitTestLib log services\r
3\r
4 Copyright (c) Microsoft Corporation.<BR>\r
5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
6**/\r
7\r
8#include <PiDxe.h>\r
9#include <UnitTestFrameworkTypes.h>\r
10#include <Library/UnitTestLib.h>\r
11#include <Library/BaseLib.h>\r
12#include <Library/BaseMemoryLib.h>\r
13#include <Library/MemoryAllocationLib.h>\r
14#include <Library/DebugLib.h>\r
15#include <Library/PrintLib.h>\r
16#include <Library/PcdLib.h>\r
17\r
18#define UNIT_TEST_MAX_SINGLE_LOG_STRING_LENGTH (512)\r
19#define UNIT_TEST_MAX_LOG_BUFFER SIZE_16KB\r
20\r
21struct _UNIT_TEST_LOG_PREFIX_STRING {\r
7c0ad2c3
MK
22 UNIT_TEST_STATUS LogLevel;\r
23 CHAR8 *String;\r
0eb52298
MK
24};\r
25\r
26struct _UNIT_TEST_LOG_PREFIX_STRING mLogPrefixStrings[] = {\r
27 { UNIT_TEST_LOG_LEVEL_ERROR, "[ERROR] " },\r
28 { UNIT_TEST_LOG_LEVEL_WARN, "[WARNING] " },\r
29 { UNIT_TEST_LOG_LEVEL_INFO, "[INFO] " },\r
30 { UNIT_TEST_LOG_LEVEL_VERBOSE, "[VERBOSE] " }\r
31};\r
32\r
33//\r
34// Unit-Test Log helper functions\r
35//\r
36\r
37STATIC\r
7c0ad2c3 38CONST CHAR8 *\r
0eb52298
MK
39GetStringForStatusLogPrefix (\r
40 IN UINTN LogLevel\r
41 )\r
42{\r
43 UINTN Index;\r
44 CHAR8 *Result;\r
45\r
46 Result = NULL;\r
47 for (Index = 0; Index < ARRAY_SIZE (mLogPrefixStrings); Index++) {\r
48 if (mLogPrefixStrings[Index].LogLevel == LogLevel) {\r
49 Result = mLogPrefixStrings[Index].String;\r
50 break;\r
51 }\r
52 }\r
7c0ad2c3 53\r
0eb52298
MK
54 return Result;\r
55}\r
56\r
57STATIC\r
58EFI_STATUS\r
59AddStringToUnitTestLog (\r
60 IN OUT UNIT_TEST *UnitTest,\r
61 IN CONST CHAR8 *String\r
62 )\r
63{\r
64 EFI_STATUS Status;\r
65\r
66 //\r
67 // Make sure that you're cooking with gas.\r
68 //\r
7c0ad2c3 69 if ((UnitTest == NULL) || (String == NULL)) {\r
0eb52298
MK
70 return EFI_INVALID_PARAMETER;\r
71 }\r
72\r
73 // If this is the first log for the test allocate log space\r
74 if (UnitTest->Log == NULL) {\r
75 UnitTestLogInit (UnitTest, NULL, 0);\r
76 }\r
77\r
78 if (UnitTest->Log == NULL) {\r
79 DEBUG ((DEBUG_ERROR, "Failed to allocate space for unit test log\n"));\r
80 ASSERT (UnitTest->Log != NULL);\r
81 return EFI_OUT_OF_RESOURCES;\r
82 }\r
83\r
84 Status = AsciiStrnCatS (\r
85 UnitTest->Log,\r
86 UNIT_TEST_MAX_LOG_BUFFER / sizeof (CHAR8),\r
87 String,\r
88 UNIT_TEST_MAX_SINGLE_LOG_STRING_LENGTH\r
89 );\r
7c0ad2c3 90 if (EFI_ERROR (Status)) {\r
0eb52298
MK
91 DEBUG ((DEBUG_ERROR, "Failed to add unit test log string. Status = %r\n", Status));\r
92 return Status;\r
93 }\r
94\r
95 return EFI_SUCCESS;\r
96}\r
97\r
98/**\r
99 This function is responsible for initializing the log buffer for a single test. It can\r
100 be used internally, but may also be consumed by the test framework to add pre-existing\r
101 data to a log before it's used.\r
102\r
103 @param[in,out] TestHandle A handle to the test being initialized.\r
104 @param[in] Buffer [Optional] A pointer to pre-existing log data that should\r
105 be used to initialize the log. Should include a NULL terminator.\r
106 @param[in] BufferSize [Optional] The size of the pre-existing log data.\r
107\r
108**/\r
109VOID\r
110EFIAPI\r
111UnitTestLogInit (\r
112 IN OUT UNIT_TEST *Test,\r
78bc3bdd 113 IN UINT8 *Buffer OPTIONAL,\r
0eb52298
MK
114 IN UINTN BufferSize OPTIONAL\r
115 )\r
116{\r
117 //\r
118 // Make sure that you're cooking with gas.\r
119 //\r
120 if (Test == NULL) {\r
121 DEBUG ((DEBUG_ERROR, "%a called with invalid Test parameter\n", __FUNCTION__));\r
122 return;\r
123 }\r
124\r
125 //\r
126 // If this is the first log for the test allocate log space\r
127 //\r
128 if (Test->Log == NULL) {\r
129 Test->Log = AllocateZeroPool (UNIT_TEST_MAX_LOG_BUFFER);\r
130 }\r
131\r
132 //\r
7c0ad2c3 133 // check again to make sure allocate worked\r
0eb52298 134 //\r
7c0ad2c3 135 if (Test->Log == NULL) {\r
0eb52298
MK
136 DEBUG ((DEBUG_ERROR, "Failed to allocate memory for the log\n"));\r
137 return;\r
138 }\r
139\r
7c0ad2c3 140 if ((Buffer != NULL) && (BufferSize > 0) && (BufferSize <= UNIT_TEST_MAX_LOG_BUFFER)) {\r
0eb52298
MK
141 CopyMem (Test->Log, Buffer, BufferSize);\r
142 }\r
143}\r
144\r
145/**\r
146 Test logging function that records a messages in the test framework log.\r
147 Record is associated with the currently executing test case.\r
148\r
149 @param[in] ErrorLevel The error level of the unit test log message.\r
150 @param[in] Format Formatting string following the format defined in the\r
151 MdePkg/Include/Library/PrintLib.h.\r
152 @param[in] ... Print args.\r
153**/\r
154VOID\r
155EFIAPI\r
156UnitTestLog (\r
157 IN UINTN ErrorLevel,\r
158 IN CONST CHAR8 *Format,\r
159 ...\r
160 )\r
161{\r
162 UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle;\r
163 CHAR8 NewFormatString[UNIT_TEST_MAX_SINGLE_LOG_STRING_LENGTH];\r
164 CHAR8 LogString[UNIT_TEST_MAX_SINGLE_LOG_STRING_LENGTH];\r
165 CONST CHAR8 *LogTypePrefix;\r
166 VA_LIST Marker;\r
167\r
168 FrameworkHandle = GetActiveFrameworkHandle ();\r
169\r
170 LogTypePrefix = NULL;\r
171\r
172 //\r
173 // Make sure that this unit test log level is enabled.\r
174 //\r
175 if ((ErrorLevel & (UINTN)PcdGet32 (PcdUnitTestLogLevel)) == 0) {\r
176 return;\r
177 }\r
178\r
179 //\r
180 // If we need to define a new format string...\r
181 // well... get to it.\r
182 //\r
183 LogTypePrefix = GetStringForStatusLogPrefix (ErrorLevel);\r
184 if (LogTypePrefix != NULL) {\r
185 AsciiSPrint (NewFormatString, sizeof (NewFormatString), "%a%a", LogTypePrefix, Format);\r
186 } else {\r
187 AsciiStrCpyS (NewFormatString, sizeof (NewFormatString), Format);\r
188 }\r
189\r
190 //\r
191 // Convert the message to an ASCII String\r
192 //\r
193 VA_START (Marker, Format);\r
194 AsciiVSPrint (LogString, sizeof (LogString), NewFormatString, Marker);\r
195 VA_END (Marker);\r
196\r
197 //\r
198 // Finally, add the string to the log.\r
199 //\r
200 AddStringToUnitTestLog (((UNIT_TEST_FRAMEWORK *)FrameworkHandle)->CurrentTest, LogString);\r
201}\r