]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/testing/selftests/futex/include/logging.h
selftests: Add futex functional tests
[mirror_ubuntu-bionic-kernel.git] / tools / testing / selftests / futex / include / logging.h
1 /******************************************************************************
2 *
3 * Copyright © International Business Machines Corp., 2009
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * DESCRIPTION
11 * Glibc independent futex library for testing kernel functionality.
12 *
13 * AUTHOR
14 * Darren Hart <dvhart@linux.intel.com>
15 *
16 * HISTORY
17 * 2009-Nov-6: Initial version by Darren Hart <dvhart@linux.intel.com>
18 *
19 *****************************************************************************/
20
21 #ifndef _LOGGING_H
22 #define _LOGGING_H
23
24 #include <string.h>
25 #include <unistd.h>
26 #include <linux/futex.h>
27
28 /*
29 * Define PASS, ERROR, and FAIL strings with and without color escape
30 * sequences, default to no color.
31 */
32 #define ESC 0x1B, '['
33 #define BRIGHT '1'
34 #define GREEN '3', '2'
35 #define YELLOW '3', '3'
36 #define RED '3', '1'
37 #define ESCEND 'm'
38 #define BRIGHT_GREEN ESC, BRIGHT, ';', GREEN, ESCEND
39 #define BRIGHT_YELLOW ESC, BRIGHT, ';', YELLOW, ESCEND
40 #define BRIGHT_RED ESC, BRIGHT, ';', RED, ESCEND
41 #define RESET_COLOR ESC, '0', 'm'
42 static const char PASS_COLOR[] = {BRIGHT_GREEN, ' ', 'P', 'A', 'S', 'S',
43 RESET_COLOR, 0};
44 static const char ERROR_COLOR[] = {BRIGHT_YELLOW, 'E', 'R', 'R', 'O', 'R',
45 RESET_COLOR, 0};
46 static const char FAIL_COLOR[] = {BRIGHT_RED, ' ', 'F', 'A', 'I', 'L',
47 RESET_COLOR, 0};
48 static const char INFO_NORMAL[] = " INFO";
49 static const char PASS_NORMAL[] = " PASS";
50 static const char ERROR_NORMAL[] = "ERROR";
51 static const char FAIL_NORMAL[] = " FAIL";
52 const char *INFO = INFO_NORMAL;
53 const char *PASS = PASS_NORMAL;
54 const char *ERROR = ERROR_NORMAL;
55 const char *FAIL = FAIL_NORMAL;
56
57 /* Verbosity setting for INFO messages */
58 #define VQUIET 0
59 #define VCRITICAL 1
60 #define VINFO 2
61 #define VMAX VINFO
62 int _verbose = VCRITICAL;
63
64 /* Functional test return codes */
65 #define RET_PASS 0
66 #define RET_ERROR -1
67 #define RET_FAIL -2
68
69 /**
70 * log_color() - Use colored output for PASS, ERROR, and FAIL strings
71 * @use_color: use color (1) or not (0)
72 */
73 void log_color(int use_color)
74 {
75 if (use_color) {
76 PASS = PASS_COLOR;
77 ERROR = ERROR_COLOR;
78 FAIL = FAIL_COLOR;
79 } else {
80 PASS = PASS_NORMAL;
81 ERROR = ERROR_NORMAL;
82 FAIL = FAIL_NORMAL;
83 }
84 }
85
86 /**
87 * log_verbosity() - Set verbosity of test output
88 * @verbose: Enable (1) verbose output or not (0)
89 *
90 * Currently setting verbose=1 will enable INFO messages and 0 will disable
91 * them. FAIL and ERROR messages are always displayed.
92 */
93 void log_verbosity(int level)
94 {
95 if (level > VMAX)
96 level = VMAX;
97 else if (level < 0)
98 level = 0;
99 _verbose = level;
100 }
101
102 /**
103 * print_result() - Print standard PASS | ERROR | FAIL results
104 * @ret: the return value to be considered: 0 | RET_ERROR | RET_FAIL
105 *
106 * print_result() is primarily intended for functional tests.
107 */
108 void print_result(int ret)
109 {
110 const char *result = "Unknown return code";
111
112 switch (ret) {
113 case RET_PASS:
114 result = PASS;
115 break;
116 case RET_ERROR:
117 result = ERROR;
118 break;
119 case RET_FAIL:
120 result = FAIL;
121 break;
122 }
123 printf("Result: %s\n", result);
124 }
125
126 /* log level macros */
127 #define info(message, vargs...) \
128 do { \
129 if (_verbose >= VINFO) \
130 fprintf(stderr, "\t%s: "message, INFO, ##vargs); \
131 } while (0)
132
133 #define error(message, err, args...) \
134 do { \
135 if (_verbose >= VCRITICAL) {\
136 if (err) \
137 fprintf(stderr, "\t%s: %s: "message, \
138 ERROR, strerror(err), ##args); \
139 else \
140 fprintf(stderr, "\t%s: "message, ERROR, ##args); \
141 } \
142 } while (0)
143
144 #define fail(message, args...) \
145 do { \
146 if (_verbose >= VCRITICAL) \
147 fprintf(stderr, "\t%s: "message, FAIL, ##args); \
148 } while (0)
149
150 #endif