blob: ffa127f0389fb4cb686401734e471780bf18e9c6 [file] [log] [blame]
Jon A. Cruz5a75a412015-07-02 23:36:44 -07001/*
2 * Copyright © 2015 Samsung Electronics Co., Ltd
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26#include "config.h"
27
28#include "zuc_base_logger.h"
29
Emmanuel Gil Peyrot1764d222016-05-18 17:18:18 +010030#include <inttypes.h>
Jon A. Cruz5a75a412015-07-02 23:36:44 -070031#include <memory.h>
32#include <stdarg.h>
33#include <stdbool.h>
34#include <stdio.h>
35#include <unistd.h>
36
37#include "zuc_event_listener.h"
38#include "zuc_types.h"
39
40#include "shared/zalloc.h"
41
42/* a few sequences for rudimentary ANSI graphics. */
43#define CSI_GRN "\x1b[0;32m"
44#define CSI_RED "\x1b[0;31m"
45#define CSI_YLW "\x1b[0;33m"
46#define CSI_RST "\x1b[m"
47
48/**
49 * Logical mappings of style levels.
50 */
51enum style_level {
52 STYLE_GOOD,
53 STYLE_WARN,
54 STYLE_BAD
55};
56
57/**
58 * Structure for internal context.
59 */
60struct base_data {
61 bool use_color;
62};
63
64/**
65 * Prints a formatted string with optional ANSI coloring.
66 *
67 * @param use_color true to colorize the output, false to output normally.
68 * @param slevel the logical type to color for.
69 * @param fmt the format string to print with.
70 */
71static void
72styled_printf(bool use_color, enum style_level slevel, const char *fmt, ...);
73
74static void
75destroy(void *data);
76
77static void
78pre_run(void *data, int pass_count, int pass_num, int seed, const char *filter);
79
80static void
81run_started(void *data, int live_case_count,
82 int live_test_count, int disabled_count);
83
84static void
85run_ended(void *data, int case_count, struct zuc_case **cases,
86 int live_case_count, int live_test_count, int total_passed,
87 int total_failed, int total_disabled, long total_elapsed);
88
89static void
90case_started(void *data, struct zuc_case *test_case, int live_test_count,
91 int disabled_count);
92
93static void
94case_ended(void *data, struct zuc_case *test_case);
95
96static void
97test_started(void *data, struct zuc_test *test);
98
99static void
100test_ended(void *data, struct zuc_test *test);
101
102static void
103check_triggered(void *data, char const *file, int line,
104 enum zuc_fail_state state, enum zuc_check_op op,
105 enum zuc_check_valtype valtype,
106 intptr_t val1, intptr_t val2,
107 const char *expr1, const char *expr2);
108
109struct zuc_event_listener *
110zuc_base_logger_create(void)
111{
112 struct zuc_event_listener *listener =
113 zalloc(sizeof(struct zuc_event_listener));
114
115 listener->data = zalloc(sizeof(struct base_data));
116 listener->destroy = destroy;
117 listener->pre_run = pre_run;
118 listener->run_started = run_started;
119 listener->run_ended = run_ended;
120 listener->case_started = case_started;
121 listener->case_ended = case_ended;
122 listener->test_started = test_started;
123 listener->test_ended = test_ended;
124 listener->check_triggered = check_triggered;
125
126 return listener;
127}
128
129void
130styled_printf(bool use_color, enum style_level slevel, const char *fmt, ...)
131{
132 va_list argp;
133
134 if (use_color)
135 switch (slevel) {
136 case STYLE_GOOD:
137 printf(CSI_GRN);
138 break;
139 case STYLE_WARN:
140 printf(CSI_YLW);
141 break;
142 case STYLE_BAD:
143 printf(CSI_RED);
144 break;
145 default:
146 break;
147 }
148
149 va_start(argp, fmt);
150 vprintf(fmt, argp);
151 va_end(argp);
152
153 if (use_color)
154 printf(CSI_RST);
155}
156
157void
158destroy(void *data)
159{
160 free(data);
161}
162
163void
164pre_run(void *data, int pass_count, int pass_num, int seed, const char *filter)
165{
166 struct base_data *bdata = data;
167
168 bdata->use_color = isatty(fileno(stdout))
169 && getenv("TERM") && strcmp(getenv("TERM"), "dumb");
170
171 if (pass_count > 1)
172 printf("\nRepeating all tests (iteration %d) . . .\n\n",
173 pass_num);
174
175 if (filter && filter[0])
176 styled_printf(bdata->use_color, STYLE_WARN,
177 "Note: test filter = %s\n",
178 filter);
179
180 if (seed > 0)
181 styled_printf(bdata->use_color, STYLE_WARN,
182 "Note: Randomizing tests' orders"
183 " with a seed of %u .\n",
184 seed);
185}
186
187void
188run_started(void *data, int live_case_count,
189 int live_test_count, int disabled_count)
190{
191 struct base_data *bdata = data;
192
193 styled_printf(bdata->use_color, STYLE_GOOD, "[==========]");
194 printf(" Running %d %s from %d test %s.\n",
195 live_test_count,
196 (live_test_count == 1) ? "test" : "tests",
197 live_case_count,
198 (live_case_count == 1) ? "case" : "cases");
199}
200
201void
202run_ended(void *data, int case_count, struct zuc_case **cases,
203 int live_case_count, int live_test_count, int total_passed,
204 int total_failed, int total_disabled, long total_elapsed)
205{
206 struct base_data *bdata = data;
207 styled_printf(bdata->use_color, STYLE_GOOD, "[==========]");
208 printf(" %d %s from %d test %s ran. (%ld ms)\n",
209 live_test_count,
210 (live_test_count == 1) ? "test" : "tests",
211 live_case_count,
212 (live_case_count == 1) ? "case" : "cases",
213 total_elapsed);
214
215 if (total_passed) {
216 styled_printf(bdata->use_color, STYLE_GOOD, "[ PASSED ]");
217 printf(" %d %s.\n", total_passed,
218 (total_passed == 1) ? "test" : "tests");
219 }
220
221 if (total_failed) {
222 int case_num;
223 styled_printf(bdata->use_color, STYLE_BAD, "[ FAILED ]");
224 printf(" %d %s, listed below:\n",
225 total_failed, (total_failed == 1) ? "test" : "tests");
226
227 for (case_num = 0; case_num < case_count; ++case_num) {
228 int i;
229 for (i = 0; i < cases[case_num]->test_count; ++i) {
230 struct zuc_test *curr =
231 cases[case_num]->tests[i];
232 if (curr->failed || curr->fatal) {
233 styled_printf(bdata->use_color,
234 STYLE_BAD,
235 "[ FAILED ]");
236 printf(" %s.%s\n",
237 cases[case_num]->name,
238 curr->name);
239 }
240 }
241 }
242 }
243
244 if (total_failed || total_disabled)
245 printf("\n");
246
247 if (total_failed)
248 printf(" %d FAILED %s\n",
249 total_failed,
250 (total_failed == 1) ? "TEST" : "TESTS");
251
252 if (total_disabled)
253 styled_printf(bdata->use_color, STYLE_WARN,
254 " YOU HAVE %d DISABLED %s\n",
255 total_disabled,
256 (total_disabled == 1) ? "TEST" : "TESTS");
257}
258
259void
260case_started(void *data, struct zuc_case *test_case, int live_test_count,
261 int disabled_count)
262{
263 struct base_data *bdata = data;
264 styled_printf(bdata->use_color, STYLE_GOOD, "[----------]");
265 printf(" %d %s from %s.\n",
266 live_test_count,
267 (live_test_count == 1) ? "test" : "tests",
268 test_case->name);
269
270}
271
272void
273case_ended(void *data, struct zuc_case *test_case)
274{
275 struct base_data *bdata = data;
276 styled_printf(bdata->use_color, STYLE_GOOD, "[----------]");
277 printf(" %d %s from %s (%ld ms)\n",
278 test_case->test_count,
279 (test_case->test_count == 1) ? "test" : "tests",
280 test_case->name,
281 test_case->elapsed);
282 printf("\n");
283}
284
285void
286test_started(void *data, struct zuc_test *test)
287{
288 struct base_data *bdata = data;
289 styled_printf(bdata->use_color, STYLE_GOOD, "[ RUN ]");
290 printf(" %s.%s\n", test->test_case->name, test->name);
291}
292
293void
294test_ended(void *data, struct zuc_test *test)
295{
296 struct base_data *bdata = data;
297 if (test->failed || test->fatal) {
298 styled_printf(bdata->use_color, STYLE_BAD, "[ FAILED ]");
299 printf(" %s.%s (%ld ms)\n",
300 test->test_case->name, test->name, test->elapsed);
301 } else {
302 styled_printf(bdata->use_color, STYLE_GOOD, "[ OK ]");
303 printf(" %s.%s (%ld ms)\n",
304 test->test_case->name, test->name, test->elapsed);
305 }
306}
307
308const char *
309zuc_get_opstr(enum zuc_check_op op)
310{
311 switch (op) {
312 case ZUC_OP_EQ:
313 return "=";
Jon A. Cruz5a75a412015-07-02 23:36:44 -0700314 case ZUC_OP_NE:
315 return "!=";
Jon A. Cruz5a75a412015-07-02 23:36:44 -0700316 case ZUC_OP_GE:
317 return ">=";
Jon A. Cruz5a75a412015-07-02 23:36:44 -0700318 case ZUC_OP_GT:
319 return ">";
Jon A. Cruz5a75a412015-07-02 23:36:44 -0700320 case ZUC_OP_LE:
321 return "<=";
Jon A. Cruz5a75a412015-07-02 23:36:44 -0700322 case ZUC_OP_LT:
323 return "<";
Jon A. Cruz5a75a412015-07-02 23:36:44 -0700324 default:
325 return "???";
326 }
327}
328
329void
330check_triggered(void *data, char const *file, int line,
331 enum zuc_fail_state state, enum zuc_check_op op,
332 enum zuc_check_valtype valtype,
333 intptr_t val1, intptr_t val2,
334 const char *expr1, const char *expr2)
335{
336 switch (op) {
337 case ZUC_OP_TRUE:
338 printf("%s:%d: error: Value of: %s\n", file, line, expr1);
339 printf(" Actual: false\n");
340 printf("Expected: true\n");
341 break;
342 case ZUC_OP_FALSE:
343 printf("%s:%d: error: Value of: %s\n", file, line, expr1);
344 printf(" Actual: true\n");
345 printf("Expected: false\n");
346 break;
347 case ZUC_OP_NULL:
348 printf("%s:%d: error: Value of: %s\n", file, line, expr1);
349 printf(" Actual: %p\n", (void *)val1);
350 printf("Expected: %p\n", NULL);
351 break;
352 case ZUC_OP_NOT_NULL:
353 printf("%s:%d: error: Value of: %s\n", file, line, expr1);
354 printf(" Actual: %p\n", (void *)val1);
355 printf("Expected: not %p\n", NULL);
356 break;
357 case ZUC_OP_EQ:
358 if (valtype == ZUC_VAL_CSTR) {
359 printf("%s:%d: error: Value of: %s\n", file, line,
360 expr2);
361 printf(" Actual: %s\n", (const char *)val2);
362 printf("Expected: %s\n", expr1);
363 printf("Which is: %s\n", (const char *)val1);
364 } else {
365 printf("%s:%d: error: Value of: %s\n", file, line,
366 expr2);
Emmanuel Gil Peyrot1764d222016-05-18 17:18:18 +0100367 printf(" Actual: %"PRIdPTR"\n", val2);
Jon A. Cruz5a75a412015-07-02 23:36:44 -0700368 printf("Expected: %s\n", expr1);
Emmanuel Gil Peyrot1764d222016-05-18 17:18:18 +0100369 printf("Which is: %"PRIdPTR"\n", val1);
Jon A. Cruz5a75a412015-07-02 23:36:44 -0700370 }
371 break;
372 case ZUC_OP_NE:
373 if (valtype == ZUC_VAL_CSTR) {
374 printf("%s:%d: error: ", file, line);
375 printf("Expected: (%s) %s (%s), actual: %s == %s\n",
376 expr1, zuc_get_opstr(op), expr2,
377 (char *)val1, (char *)val2);
378 } else {
379 printf("%s:%d: error: ", file, line);
Emmanuel Gil Peyrot1764d222016-05-18 17:18:18 +0100380 printf("Expected: (%s) %s (%s), actual: %"PRIdPTR" vs "
381 "%"PRIdPTR"\n", expr1, zuc_get_opstr(op), expr2,
382 val1, val2);
Jon A. Cruz5a75a412015-07-02 23:36:44 -0700383 }
384 break;
385 case ZUC_OP_TERMINATE: {
386 char const *level = (val1 == 0) ? "error"
387 : (val1 == 1) ? "warning"
388 : "note";
389 printf("%s:%d: %s: %s\n", file, line, level, expr1);
390 break;
391 }
392 case ZUC_OP_TRACEPOINT:
393 printf("%s:%d: note: %s\n", file, line, expr1);
394 break;
395 default:
396 printf("%s:%d: error: ", file, line);
Emmanuel Gil Peyrot1764d222016-05-18 17:18:18 +0100397 printf("Expected: (%s) %s (%s), actual: %"PRIdPTR" vs "
398 "%"PRIdPTR"\n", expr1, zuc_get_opstr(op), expr2, val1,
399 val2);
Jon A. Cruz5a75a412015-07-02 23:36:44 -0700400 }
401}