]>
Commit | Line | Data |
---|---|---|
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 | |
21 | struct _UNIT_TEST_LOG_PREFIX_STRING {\r | |
22 | UNIT_TEST_STATUS LogLevel;\r | |
23 | CHAR8 *String;\r | |
24 | };\r | |
25 | \r | |
26 | struct _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 | |
37 | STATIC\r | |
38 | CONST CHAR8*\r | |
39 | GetStringForStatusLogPrefix (\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 | |
53 | return Result;\r | |
54 | }\r | |
55 | \r | |
56 | STATIC\r | |
57 | EFI_STATUS\r | |
58 | AddStringToUnitTestLog (\r | |
59 | IN OUT UNIT_TEST *UnitTest,\r | |
60 | IN CONST CHAR8 *String\r | |
61 | )\r | |
62 | {\r | |
63 | EFI_STATUS Status;\r | |
64 | \r | |
65 | //\r | |
66 | // Make sure that you're cooking with gas.\r | |
67 | //\r | |
68 | if (UnitTest == NULL || String == NULL) {\r | |
69 | return EFI_INVALID_PARAMETER;\r | |
70 | }\r | |
71 | \r | |
72 | // If this is the first log for the test allocate log space\r | |
73 | if (UnitTest->Log == NULL) {\r | |
74 | UnitTestLogInit (UnitTest, NULL, 0);\r | |
75 | }\r | |
76 | \r | |
77 | if (UnitTest->Log == NULL) {\r | |
78 | DEBUG ((DEBUG_ERROR, "Failed to allocate space for unit test log\n"));\r | |
79 | ASSERT (UnitTest->Log != NULL);\r | |
80 | return EFI_OUT_OF_RESOURCES;\r | |
81 | }\r | |
82 | \r | |
83 | Status = AsciiStrnCatS (\r | |
84 | UnitTest->Log,\r | |
85 | UNIT_TEST_MAX_LOG_BUFFER / sizeof (CHAR8),\r | |
86 | String,\r | |
87 | UNIT_TEST_MAX_SINGLE_LOG_STRING_LENGTH\r | |
88 | );\r | |
89 | if(EFI_ERROR (Status)) {\r | |
90 | DEBUG ((DEBUG_ERROR, "Failed to add unit test log string. Status = %r\n", Status));\r | |
91 | return Status;\r | |
92 | }\r | |
93 | \r | |
94 | return EFI_SUCCESS;\r | |
95 | }\r | |
96 | \r | |
97 | /**\r | |
98 | This function is responsible for initializing the log buffer for a single test. It can\r | |
99 | be used internally, but may also be consumed by the test framework to add pre-existing\r | |
100 | data to a log before it's used.\r | |
101 | \r | |
102 | @param[in,out] TestHandle A handle to the test being initialized.\r | |
103 | @param[in] Buffer [Optional] A pointer to pre-existing log data that should\r | |
104 | be used to initialize the log. Should include a NULL terminator.\r | |
105 | @param[in] BufferSize [Optional] The size of the pre-existing log data.\r | |
106 | \r | |
107 | **/\r | |
108 | VOID\r | |
109 | EFIAPI\r | |
110 | UnitTestLogInit (\r | |
111 | IN OUT UNIT_TEST *Test,\r | |
78bc3bdd | 112 | IN UINT8 *Buffer OPTIONAL,\r |
0eb52298 MK |
113 | IN UINTN BufferSize OPTIONAL\r |
114 | )\r | |
115 | {\r | |
116 | //\r | |
117 | // Make sure that you're cooking with gas.\r | |
118 | //\r | |
119 | if (Test == NULL) {\r | |
120 | DEBUG ((DEBUG_ERROR, "%a called with invalid Test parameter\n", __FUNCTION__));\r | |
121 | return;\r | |
122 | }\r | |
123 | \r | |
124 | //\r | |
125 | // If this is the first log for the test allocate log space\r | |
126 | //\r | |
127 | if (Test->Log == NULL) {\r | |
128 | Test->Log = AllocateZeroPool (UNIT_TEST_MAX_LOG_BUFFER);\r | |
129 | }\r | |
130 | \r | |
131 | //\r | |
132 | //check again to make sure allocate worked\r | |
133 | //\r | |
134 | if(Test->Log == NULL) {\r | |
135 | DEBUG ((DEBUG_ERROR, "Failed to allocate memory for the log\n"));\r | |
136 | return;\r | |
137 | }\r | |
138 | \r | |
f76d5016 | 139 | if((Buffer != NULL) && (BufferSize > 0) && (BufferSize <= UNIT_TEST_MAX_LOG_BUFFER)) {\r |
0eb52298 MK |
140 | CopyMem (Test->Log, Buffer, BufferSize);\r |
141 | }\r | |
142 | }\r | |
143 | \r | |
144 | /**\r | |
145 | Test logging function that records a messages in the test framework log.\r | |
146 | Record is associated with the currently executing test case.\r | |
147 | \r | |
148 | @param[in] ErrorLevel The error level of the unit test log message.\r | |
149 | @param[in] Format Formatting string following the format defined in the\r | |
150 | MdePkg/Include/Library/PrintLib.h.\r | |
151 | @param[in] ... Print args.\r | |
152 | **/\r | |
153 | VOID\r | |
154 | EFIAPI\r | |
155 | UnitTestLog (\r | |
156 | IN UINTN ErrorLevel,\r | |
157 | IN CONST CHAR8 *Format,\r | |
158 | ...\r | |
159 | )\r | |
160 | {\r | |
161 | UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle;\r | |
162 | CHAR8 NewFormatString[UNIT_TEST_MAX_SINGLE_LOG_STRING_LENGTH];\r | |
163 | CHAR8 LogString[UNIT_TEST_MAX_SINGLE_LOG_STRING_LENGTH];\r | |
164 | CONST CHAR8 *LogTypePrefix;\r | |
165 | VA_LIST Marker;\r | |
166 | \r | |
167 | FrameworkHandle = GetActiveFrameworkHandle ();\r | |
168 | \r | |
169 | LogTypePrefix = NULL;\r | |
170 | \r | |
171 | //\r | |
172 | // Make sure that this unit test log level is enabled.\r | |
173 | //\r | |
174 | if ((ErrorLevel & (UINTN)PcdGet32 (PcdUnitTestLogLevel)) == 0) {\r | |
175 | return;\r | |
176 | }\r | |
177 | \r | |
178 | //\r | |
179 | // If we need to define a new format string...\r | |
180 | // well... get to it.\r | |
181 | //\r | |
182 | LogTypePrefix = GetStringForStatusLogPrefix (ErrorLevel);\r | |
183 | if (LogTypePrefix != NULL) {\r | |
184 | AsciiSPrint (NewFormatString, sizeof (NewFormatString), "%a%a", LogTypePrefix, Format);\r | |
185 | } else {\r | |
186 | AsciiStrCpyS (NewFormatString, sizeof (NewFormatString), Format);\r | |
187 | }\r | |
188 | \r | |
189 | //\r | |
190 | // Convert the message to an ASCII String\r | |
191 | //\r | |
192 | VA_START (Marker, Format);\r | |
193 | AsciiVSPrint (LogString, sizeof (LogString), NewFormatString, Marker);\r | |
194 | VA_END (Marker);\r | |
195 | \r | |
196 | //\r | |
197 | // Finally, add the string to the log.\r | |
198 | //\r | |
199 | AddStringToUnitTestLog (((UNIT_TEST_FRAMEWORK *)FrameworkHandle)->CurrentTest, LogString);\r | |
200 | }\r |