TianoCore EDK2 master
Loading...
Searching...
No Matches
Log.c
1
8#include <PiDxe.h>
10#include <Library/UnitTestLib.h>
11#include <Library/BaseLib.h>
14#include <Library/DebugLib.h>
15#include <Library/PrintLib.h>
16#include <Library/PcdLib.h>
17
18#define UNIT_TEST_MAX_LOG_BUFFER SIZE_16KB
19
21 UNIT_TEST_STATUS LogLevel;
22 CHAR8 *String;
23};
24
25struct _UNIT_TEST_LOG_PREFIX_STRING mLogPrefixStrings[] = {
26 { UNIT_TEST_LOG_LEVEL_ERROR, "[ERROR] " },
27 { UNIT_TEST_LOG_LEVEL_WARN, "[WARNING] " },
28 { UNIT_TEST_LOG_LEVEL_INFO, "[INFO] " },
29 { UNIT_TEST_LOG_LEVEL_VERBOSE, "[VERBOSE] " }
30};
31
32//
33// Unit-Test Log helper functions
34//
35
37CONST CHAR8 *
38GetStringForStatusLogPrefix (
39 IN UINTN LogLevel
40 )
41{
42 UINTN Index;
43 CHAR8 *Result;
44
45 Result = NULL;
46 for (Index = 0; Index < ARRAY_SIZE (mLogPrefixStrings); Index++) {
47 if (mLogPrefixStrings[Index].LogLevel == LogLevel) {
48 Result = mLogPrefixStrings[Index].String;
49 break;
50 }
51 }
52
53 return Result;
54}
55
58AddStringToUnitTestLog (
59 IN OUT UNIT_TEST *UnitTest,
60 IN CONST CHAR8 *String
61 )
62{
63 EFI_STATUS Status;
64
65 //
66 // Make sure that you're cooking with gas.
67 //
68 if ((UnitTest == NULL) || (String == NULL)) {
69 return EFI_INVALID_PARAMETER;
70 }
71
72 // If this is the first log for the test allocate log space
73 if (UnitTest->Log == NULL) {
74 UnitTestLogInit (UnitTest, NULL, 0);
75 }
76
77 if (UnitTest->Log == NULL) {
78 DEBUG ((DEBUG_ERROR, "Failed to allocate space for unit test log\n"));
79 ASSERT (UnitTest->Log != NULL);
80 return EFI_OUT_OF_RESOURCES;
81 }
82
83 Status = AsciiStrnCatS (
84 UnitTest->Log,
85 UNIT_TEST_MAX_LOG_BUFFER / sizeof (CHAR8),
86 String,
88 );
89 if (EFI_ERROR (Status)) {
90 DEBUG ((DEBUG_ERROR, "Failed to add unit test log string. Status = %r\n", Status));
91 return Status;
92 }
93
94 return EFI_SUCCESS;
95}
96
108VOID
109EFIAPI
110UnitTestLogInit (
112 IN UINT8 *Buffer OPTIONAL,
113 IN UINTN BufferSize OPTIONAL
114 )
115{
116 //
117 // Make sure that you're cooking with gas.
118 //
119 if (Test == NULL) {
120 DEBUG ((DEBUG_ERROR, "%a called with invalid Test parameter\n", __func__));
121 return;
122 }
123
124 //
125 // If this is the first log for the test allocate log space
126 //
127 if (Test->Log == NULL) {
128 Test->Log = AllocateZeroPool (UNIT_TEST_MAX_LOG_BUFFER);
129 }
130
131 //
132 // check again to make sure allocate worked
133 //
134 if (Test->Log == NULL) {
135 DEBUG ((DEBUG_ERROR, "Failed to allocate memory for the log\n"));
136 return;
137 }
138
139 if ((Buffer != NULL) && (BufferSize > 0) && (BufferSize <= UNIT_TEST_MAX_LOG_BUFFER)) {
140 CopyMem (Test->Log, Buffer, BufferSize);
141 }
142}
143
153VOID
154EFIAPI
156 IN UINTN ErrorLevel,
157 IN CONST CHAR8 *Format,
158 ...
159 )
160{
161 UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle;
162 CHAR8 NewFormatString[UNIT_TEST_MAX_STRING_LENGTH];
163 CHAR8 LogString[UNIT_TEST_MAX_STRING_LENGTH];
164 CONST CHAR8 *LogTypePrefix;
165 VA_LIST Marker;
166
167 FrameworkHandle = GetActiveFrameworkHandle ();
168 if (FrameworkHandle == NULL) {
169 DEBUG ((DEBUG_ERROR, "%a - FrameworkHandle not initialized\n", __func__));
170 return;
171 }
172
173 LogTypePrefix = NULL;
174
175 //
176 // Make sure that this unit test log level is enabled.
177 //
178 if ((ErrorLevel & (UINTN)PcdGet32 (PcdUnitTestLogLevel)) == 0) {
179 return;
180 }
181
182 //
183 // If we need to define a new format string...
184 // well... get to it.
185 //
186 LogTypePrefix = GetStringForStatusLogPrefix (ErrorLevel);
187 if (LogTypePrefix != NULL) {
188 AsciiSPrint (NewFormatString, sizeof (NewFormatString), "%a%a", LogTypePrefix, Format);
189 } else {
190 AsciiStrCpyS (NewFormatString, sizeof (NewFormatString), Format);
191 }
192
193 //
194 // Convert the message to an ASCII String
195 //
196 VA_START (Marker, Format);
197 AsciiVSPrint (LogString, sizeof (LogString), NewFormatString, Marker);
198 VA_END (Marker);
199
200 //
201 // Finally, add the string to the log.
202 //
203 AddStringToUnitTestLog (((UNIT_TEST_FRAMEWORK *)FrameworkHandle)->CurrentTest, LogString);
204}
UINT64 UINTN
RETURN_STATUS EFIAPI AsciiStrnCatS(IN OUT CHAR8 *Destination, IN UINTN DestMax, IN CONST CHAR8 *Source, IN UINTN Length)
Definition: SafeString.c:2060
RETURN_STATUS EFIAPI AsciiStrCpyS(OUT CHAR8 *Destination, IN UINTN DestMax, IN CONST CHAR8 *Source)
Definition: SafeString.c:1797
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
UINTN EFIAPI AsciiVSPrint(OUT CHAR8 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString, IN VA_LIST Marker)
Definition: PrintLib.c:702
UINTN EFIAPI AsciiSPrint(OUT CHAR8 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString,...)
Definition: PrintLib.c:813
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define VA_START(Marker, Parameter)
Definition: Base.h:661
#define ARRAY_SIZE(Array)
Definition: Base.h:1393
CHAR8 * VA_LIST
Definition: Base.h:643
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define VA_END(Marker)
Definition: Base.h:691
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define PcdGet32(TokenName)
Definition: PcdLib.h:362
INTN EFIAPI Test(CONST VOID *b1, CONST VOID *b2)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
#define UNIT_TEST_MAX_STRING_LENGTH
VOID EFIAPI UnitTestLog(IN UINTN ErrorLevel, IN CONST CHAR8 *Format,...)
Definition: Log.c:155
#define UNIT_TEST_LOG_LEVEL_ERROR
Definition: UnitTestLib.h:28
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16