logging.h (3725B)
1/* SPDX-License-Identifier: GPL-2.0-or-later */ 2/****************************************************************************** 3 * 4 * Copyright © International Business Machines Corp., 2009 5 * 6 * DESCRIPTION 7 * Glibc independent futex library for testing kernel functionality. 8 * 9 * AUTHOR 10 * Darren Hart <dvhart@linux.intel.com> 11 * 12 * HISTORY 13 * 2009-Nov-6: Initial version by Darren Hart <dvhart@linux.intel.com> 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 * Define PASS, ERROR, and FAIL strings with and without color escape 28 * sequences, default to no color. 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/* Verbosity setting for INFO messages */ 56#define VQUIET 0 57#define VCRITICAL 1 58#define VINFO 2 59#define VMAX VINFO 60int _verbose = VCRITICAL; 61 62/* Functional test return codes */ 63#define RET_PASS 0 64#define RET_ERROR -1 65#define RET_FAIL -2 66 67/** 68 * log_color() - Use colored output for PASS, ERROR, and FAIL strings 69 * @use_color: use color (1) or not (0) 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 * log_verbosity() - Set verbosity of test output 86 * @verbose: Enable (1) verbose output or not (0) 87 * 88 * Currently setting verbose=1 will enable INFO messages and 0 will disable 89 * them. FAIL and ERROR messages are always displayed. 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 * print_result() - Print standard PASS | ERROR | FAIL results 102 * @ret: the return value to be considered: 0 | RET_ERROR | RET_FAIL 103 * 104 * print_result() is primarily intended for functional tests. 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/* log level macros */ 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