master
h 347 lines 9.36 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 lxtlog.h
8
9 Abstract:
10
11 This file contains lx test logging routines.
12
13 --*/
14
15 #ifndef _LXT_LOG
16 #define _LXT_LOG
17
18 #include <unistd.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <stdbool.h>
22
23 typedef enum _LxtLogType
24 {
25 LxtLogTypeFile = 0x1,
26 LxtLogTypePrintf = 0x2,
27 LxtLogTypeStress = 0x4,
28 LxtLogTypeMax
29 } LxtLogType,
30 *PLxtLogType;
31
32 typedef enum _LxtLogLevel
33 {
34 LxtLogLevelInfo = 0,
35 LxtLogLevelError,
36 LxtLogLevelResourceError,
37 LxtLogLevelPass,
38 LxtLogLevelStart,
39 LxtLogLevelMax
40 } LxtLogLevel,
41 *PLxtLogLevel;
42
43 #define LXT_LOG_TYPE_DEFAULT_MASK (LxtLogTypeFile | LxtLogTypePrintf)
44
45 void LxtLog(LxtLogLevel LogLevel, const char* Message, ...);
46
47 int LxtLogInitialize(const char* TestName, LxtLogType LogTypeMask, bool LogAppend);
48
49 void LxtLogUninitialize(void);
50
51 #define LXT_RESULT_SUCCESS (0)
52 #define LXT_RESULT_FAILURE (-1)
53 #define LXT_SUCCESS(Result) ((Result) != LXT_RESULT_FAILURE)
54
55 #define LxtLogStart(str, ...) \
56 { \
57 LxtLog(LxtLogLevelStart, "START: %s:%u: " str "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); \
58 }
59
60 #define LxtLogInfo(str, ...) \
61 { \
62 LxtLog(LxtLogLevelInfo, "INFO: %s:%u: " str "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); \
63 }
64
65 #define LxtLogResourceError(str, ...) \
66 { \
67 LxtLog(LxtLogLevelResourceError, "RESOURCE_ERROR: %s:%u: " str "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); \
68 }
69
70 #define LxtLogError(str, ...) \
71 { \
72 LxtLog(LxtLogLevelError, "ERROR: %s:%u: " str "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); \
73 }
74
75 #define LxtLogPassed(str, ...) \
76 { \
77 LxtLog(LxtLogLevelPass, "PASS: %s:%u: " str "\n", __FUNCTION__, __LINE__, ##__VA_ARGS__); \
78 }
79
80 #define LxtCheckErrno(fn) \
81 { \
82 Result = (fn); \
83 if (LXT_SUCCESS(Result) == 0) \
84 { \
85 LxtLogError("%s failed: %d (%s)", #fn, errno, strerror(errno)); \
86 goto ErrorExit; \
87 } \
88 }
89
90 #define LxtCheckMapErrno(fn) \
91 { \
92 MapResult = (fn); \
93 Result = LXT_RESULT_SUCCESS; \
94 if (MapResult == MAP_FAILED) \
95 { \
96 Result = LXT_RESULT_FAILURE; \
97 LxtLogError("%s failed: %s", #fn, strerror(errno)); \
98 goto ErrorExit; \
99 } \
100 }
101
102 #define LxtCheckMapErrnoFailure(_Fn, _ExpectedErrno) \
103 { \
104 MapResult = (_Fn); \
105 Result = LXT_RESULT_SUCCESS; \
106 if (MapResult != MAP_FAILED) \
107 { \
108 Result = LXT_RESULT_FAILURE; \
109 LxtLogError("%s succeeded, expected errno %d", #_Fn, (_ExpectedErrno)); \
110 goto ErrorExit; \
111 } \
112 \
113 if (errno != (_ExpectedErrno)) \
114 { \
115 LxtLogError("%s unexpected failure status: %d != %d (%s)", #_Fn, _ExpectedErrno, errno, strerror(errno)); \
116 \
117 Result = LXT_RESULT_FAILURE; \
118 goto ErrorExit; \
119 } \
120 }
121
122 #define LxtCheckNullErrno(_Fn) \
123 { \
124 PointerResult = (_Fn); \
125 Result = LXT_RESULT_SUCCESS; \
126 if (PointerResult == NULL) \
127 { \
128 Result = LXT_RESULT_FAILURE; \
129 LxtLogError("%s failed: %s", #_Fn, strerror(errno)); \
130 goto ErrorExit; \
131 } \
132 }
133
134 #define LxtCheckNullErrnoFailure(_Fn, _ExpectedErrno) \
135 { \
136 PointerResult = (_Fn); \
137 Result = LXT_RESULT_SUCCESS; \
138 if (PointerResult != NULL) \
139 { \
140 LxtLogError("%s succeeded, expected errno %d", #_Fn, (_ExpectedErrno)); \
141 Result = LXT_RESULT_FAILURE; \
142 goto ErrorExit; \
143 } \
144 \
145 if (errno != (_ExpectedErrno)) \
146 { \
147 LxtLogError("%s unexpected failure status: %d != %d (%s)", #_Fn, _ExpectedErrno, errno, strerror(errno)); \
148 \
149 Result = LXT_RESULT_FAILURE; \
150 goto ErrorExit; \
151 } \
152 }
153
154 //
155 // The following macro is for functions where 0 is the only valid success value
156 // (they don't return a pid or fd or anything), and where errno is set if an
157 // error occurs.
158 //
159
160 #define LxtCheckErrnoZeroSuccess(_Fn) \
161 { \
162 Result = (_Fn); \
163 if (Result != LXT_RESULT_SUCCESS) \
164 { \
165 if (LXT_SUCCESS(Result) != 0) \
166 { \
167 LxtLogError("%s succeeded with %d, expected 0.", #_Fn, Result); \
168 } \
169 else \
170 { \
171 LxtLogError("%s failed: %d, errno %d (%s)", #_Fn, Result, errno, strerror(errno)); \
172 } \
173 \
174 Result = LXT_RESULT_FAILURE; \
175 goto ErrorExit; \
176 } \
177 }
178
179 #define LxtCheckErrnoFailure(_Fn, _ExpectedErrno) \
180 { \
181 Result = (_Fn); \
182 if ((LXT_SUCCESS(Result) != 0)) \
183 { \
184 LxtLogError("%s succeeded with %d, expected errno %d", #_Fn, Result, _ExpectedErrno); \
185 \
186 Result = LXT_RESULT_FAILURE; \
187 goto ErrorExit; \
188 } \
189 \
190 if ((_ExpectedErrno) != errno) \
191 { \
192 LxtLogError("%s unexpected failure status: %d, %d != %d (%s)", #_Fn, Result, _ExpectedErrno, errno, strerror(errno)); \
193 \
194 Result = LXT_RESULT_FAILURE; \
195 goto ErrorExit; \
196 } \
197 \
198 Result = LXT_RESULT_SUCCESS; \
199 }
200
201 #define LxtCheckEqual(_Val1, _Val2, _Format) \
202 { \
203 if ((_Val1) != (_Val2)) \
204 { \
205 LxtLogError("{:%s (" _Format ") != %s (" _Format "):}", #_Val1, (_Val1), #_Val2, (_Val2)); \
206 Result = LXT_RESULT_FAILURE; \
207 goto ErrorExit; \
208 } \
209 \
210 Result = LXT_RESULT_SUCCESS; \
211 }
212
213 #define LxtCheckGreaterOrEqual(_Val1, _Val2, _Format) \
214 { \
215 if ((_Val1) < (_Val2)) \
216 { \
217 LxtLogError("{:%s (" _Format ") < %s (" _Format "):}", #_Val1, (_Val1), #_Val2, (_Val2)); \
218 Result = LXT_RESULT_FAILURE; \
219 goto ErrorExit; \
220 } \
221 \
222 Result = LXT_RESULT_SUCCESS; \
223 }
224
225 #define LxtCheckNotEqual(_Val1, _Val2, _Format) \
226 { \
227 if ((_Val1) == (_Val2)) \
228 { \
229 LxtLogError("{:%s (" _Format ") == %s (" _Format "):}", #_Val1, (_Val1), #_Val2, (_Val2)); \
230 Result = LXT_RESULT_FAILURE; \
231 goto ErrorExit; \
232 } \
233 \
234 Result = LXT_RESULT_SUCCESS; \
235 }
236
237 #define LxtCheckStringEqual(_Val1, _Val2) \
238 { \
239 if ((((_Val1) == NULL) && ((_Val2) != NULL)) || (((_Val1) != NULL) && ((_Val2) == NULL)) || \
240 (((_Val1) != (_Val2)) && (strcmp((_Val1), (_Val2)) != 0))) \
241 { \
242 \
243 LxtLogError("%s (\"%s\") != %s (\"%s\")", #_Val1, (_Val1), #_Val2, (_Val2)); \
244 \
245 Result = LXT_RESULT_FAILURE; \
246 goto ErrorExit; \
247 } \
248 \
249 Result = LXT_RESULT_SUCCESS; \
250 }
251
252 #define LxtCheckStringNotEqual(_Val1, _Val2) \
253 { \
254 if (strcmp((_Val1), (_Val2)) == 0) \
255 { \
256 LxtLogError("%s (\"%s\") == %s (\"%s\")", #_Val1, (_Val1), #_Val2, (_Val2)); \
257 \
258 Result = LXT_RESULT_FAILURE; \
259 goto ErrorExit; \
260 } \
261 \
262 Result = LXT_RESULT_SUCCESS; \
263 }
264
265 #define LxtCheckGreater(_Val1, _Val2, _Format) \
266 { \
267 if ((_Val1) <= (_Val2)) \
268 { \
269 LxtLogError("{:%s (" _Format ") <= %s (" _Format "):}", #_Val1, (_Val1), #_Val2, (_Val2)); \
270 Result = LXT_RESULT_FAILURE; \
271 goto ErrorExit; \
272 } \
273 \
274 Result = LXT_RESULT_SUCCESS; \
275 }
276
277 #define LxtCheckMemoryEqual(_Ptr1, _Ptr2, _Size) \
278 { \
279 Result = LxtCompareMemory((_Ptr1), (_Ptr2), (_Size), #_Ptr1, #_Ptr2); \
280 if (Result < 0) \
281 { \
282 LxtLogError("Memory contents were not equal"); \
283 goto ErrorExit; \
284 } \
285 }
286
287 #define LxtCheckTrue(_Val) \
288 { \
289 if ((_Val) == FALSE) \
290 { \
291 LxtLogError("The expression (" #_Val ") does not equal true"); \
292 Result = LXT_RESULT_FAILURE; \
293 goto ErrorExit; \
294 } \
295 \
296 Result = LXT_RESULT_SUCCESS; \
297 }
298
299 #define LxtCheckResult(fn) \
300 { \
301 Result = (fn); \
302 if (LXT_SUCCESS(Result) == 0) \
303 { \
304 LxtLogError("%s failed", #fn); \
305 goto ErrorExit; \
306 } \
307 }
308
309 //
310 // Macro for functions that return a positive error value on failure, like
311 // pthread functions.
312 //
313
314 #define LxtCheckResultError(_Fn) \
315 { \
316 Result = (_Fn); \
317 if (Result != 0) \
318 { \
319 LxtLogError("%s failed: %d (%s)", #_Fn, Result, strerror(Result)); \
320 Result = LXT_RESULT_FAILURE; \
321 goto ErrorExit; \
322 } \
323 }
324
325 #define LxtCheckResultErrorFailure(_Fn, _ExpectedErrno) \
326 { \
327 Result = (_Fn); \
328 if (Result == 0) \
329 { \
330 LxtLogError("%s succeeded, expected errno %d", #_Fn, _ExpectedErrno); \
331 \
332 Result = LXT_RESULT_FAILURE; \
333 goto ErrorExit; \
334 } \
335 \
336 if ((_ExpectedErrno) != Result) \
337 { \
338 LxtLogError("%s unexpected failure status: %d != %d (%s)", #_Fn, (_ExpectedErrno), Result, strerror(Result)); \
339 \
340 Result = LXT_RESULT_FAILURE; \
341 goto ErrorExit; \
342 } \
343 \
344 Result = LXT_RESULT_SUCCESS; \
345 }
346
347 #endif // _LXT_LOG