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 const char *result = "Unknown return code";
113
114 switch (ret) {
115 case RET_PASS:
116 ksft_inc_pass_cnt();
117 result = PASS;
118 break;
119 case RET_ERROR:
120 result = ERROR;
121 break;
122 case RET_FAIL:
123 ksft_inc_fail_cnt();
124 result = FAIL;
125 break;
126 }
127 printf("selftests: %s [%s]\n", test_name, result);
128}
129
130
131#define info(message, vargs...) \
132do { \
133 if (_verbose >= VINFO) \
134 fprintf(stderr, "\t%s: "message, INFO, ##vargs); \
135} while (0)
136
137#define error(message, err, args...) \
138do { \
139 if (_verbose >= VCRITICAL) {\
140 if (err) \
141 fprintf(stderr, "\t%s: %s: "message, \
142 ERROR, strerror(err), ##args); \
143 else \
144 fprintf(stderr, "\t%s: "message, ERROR, ##args); \
145 } \
146} while (0)
147
148#define fail(message, args...) \
149do { \
150 if (_verbose >= VCRITICAL) \
151 fprintf(stderr, "\t%s: "message, FAIL, ##args); \
152} while (0)
153
154#endif
155