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