1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#ifndef _LOGGING_H
18#define _LOGGING_H
19
20#include <stdio.h>
21#include <string.h>
22#include <unistd.h>
23#include <linux/futex.h>
24#include "kselftest.h"
25
26
27
28
29
30#define ESC 0x1B, '['
31#define BRIGHT '1'
32#define GREEN '3', '2'
33#define YELLOW '3', '3'
34#define RED '3', '1'
35#define ESCEND 'm'
36#define BRIGHT_GREEN ESC, BRIGHT, ';', GREEN, ESCEND
37#define BRIGHT_YELLOW ESC, BRIGHT, ';', YELLOW, ESCEND
38#define BRIGHT_RED ESC, BRIGHT, ';', RED, ESCEND
39#define RESET_COLOR ESC, '0', 'm'
40static const char PASS_COLOR[] = {BRIGHT_GREEN, ' ', 'P', 'A', 'S', 'S',
41 RESET_COLOR, 0};
42static const char ERROR_COLOR[] = {BRIGHT_YELLOW, 'E', 'R', 'R', 'O', 'R',
43 RESET_COLOR, 0};
44static const char FAIL_COLOR[] = {BRIGHT_RED, ' ', 'F', 'A', 'I', 'L',
45 RESET_COLOR, 0};
46static const char INFO_NORMAL[] = " INFO";
47static const char PASS_NORMAL[] = " PASS";
48static const char ERROR_NORMAL[] = "ERROR";
49static const char FAIL_NORMAL[] = " FAIL";
50const char *INFO = INFO_NORMAL;
51const char *PASS = PASS_NORMAL;
52const char *ERROR = ERROR_NORMAL;
53const char *FAIL = FAIL_NORMAL;
54
55
56#define VQUIET 0
57#define VCRITICAL 1
58#define VINFO 2
59#define VMAX VINFO
60int _verbose = VCRITICAL;
61
62
63#define RET_PASS 0
64#define RET_ERROR -1
65#define RET_FAIL -2
66
67
68
69
70
71void log_color(int use_color)
72{
73 if (use_color) {
74 PASS = PASS_COLOR;
75 ERROR = ERROR_COLOR;
76 FAIL = FAIL_COLOR;
77 } else {
78 PASS = PASS_NORMAL;
79 ERROR = ERROR_NORMAL;
80 FAIL = FAIL_NORMAL;
81 }
82}
83
84
85
86
87
88
89
90
91void log_verbosity(int level)
92{
93 if (level > VMAX)
94 level = VMAX;
95 else if (level < 0)
96 level = 0;
97 _verbose = level;
98}
99
100
101
102
103
104
105
106void print_result(const char *test_name, int ret)
107{
108 switch (ret) {
109 case RET_PASS:
110 ksft_test_result_pass("%s\n", test_name);
111 ksft_print_cnts();
112 return;
113 case RET_ERROR:
114 ksft_test_result_error("%s\n", test_name);
115 ksft_print_cnts();
116 return;
117 case RET_FAIL:
118 ksft_test_result_fail("%s\n", test_name);
119 ksft_print_cnts();
120 return;
121 }
122}
123
124
125#define info(message, vargs...) \
126do { \
127 if (_verbose >= VINFO) \
128 fprintf(stderr, "\t%s: "message, INFO, ##vargs); \
129} while (0)
130
131#define error(message, err, args...) \
132do { \
133 if (_verbose >= VCRITICAL) {\
134 if (err) \
135 fprintf(stderr, "\t%s: %s: "message, \
136 ERROR, strerror(err), ##args); \
137 else \
138 fprintf(stderr, "\t%s: "message, ERROR, ##args); \
139 } \
140} while (0)
141
142#define fail(message, args...) \
143do { \
144 if (_verbose >= VCRITICAL) \
145 fprintf(stderr, "\t%s: "message, FAIL, ##args); \
146} while (0)
147
148#endif
149