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 <stdio.h> 25 #include <string.h> 26 #include <unistd.h> 27 #include <linux/futex.h> 28 #include "kselftest.h" 29 30 /* 31 * Define PASS, ERROR, and FAIL strings with and without color escape 32 * sequences, default to no color. 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' 44 static const char PASS_COLOR[] = {BRIGHT_GREEN, ' ', 'P', 'A', 'S', 'S', 45 RESET_COLOR, 0}; 46 static const char ERROR_COLOR[] = {BRIGHT_YELLOW, 'E', 'R', 'R', 'O', 'R', 47 RESET_COLOR, 0}; 48 static const char FAIL_COLOR[] = {BRIGHT_RED, ' ', 'F', 'A', 'I', 'L', 49 RESET_COLOR, 0}; 50 static const char INFO_NORMAL[] = " INFO"; 51 static const char PASS_NORMAL[] = " PASS"; 52 static const char ERROR_NORMAL[] = "ERROR"; 53 static const char FAIL_NORMAL[] = " FAIL"; 54 const char *INFO = INFO_NORMAL; 55 const char *PASS = PASS_NORMAL; 56 const char *ERROR = ERROR_NORMAL; 57 const char *FAIL = FAIL_NORMAL; 58 59 /* Verbosity setting for INFO messages */ 60 #define VQUIET 0 61 #define VCRITICAL 1 62 #define VINFO 2 63 #define VMAX VINFO 64 int _verbose = VCRITICAL; 65 66 /* Functional test return codes */ 67 #define RET_PASS 0 68 #define RET_ERROR -1 69 #define RET_FAIL -2 70 71 /** 72 * log_color() - Use colored output for PASS, ERROR, and FAIL strings 73 * @use_color: use color (1) or not (0) 74 */ 75 void 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 * log_verbosity() - Set verbosity of test output 90 * @verbose: Enable (1) verbose output or not (0) 91 * 92 * Currently setting verbose=1 will enable INFO messages and 0 will disable 93 * them. FAIL and ERROR messages are always displayed. 94 */ 95 void 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 * print_result() - Print standard PASS | ERROR | FAIL results 106 * @ret: the return value to be considered: 0 | RET_ERROR | RET_FAIL 107 * 108 * print_result() is primarily intended for functional tests. 109 */ 110 void 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 /* log level macros */ 129 #define info(message, vargs...) \ 130 do { \ 131 if (_verbose >= VINFO) \ 132 fprintf(stderr, "\t%s: "message, INFO, ##vargs); \ 133 } while (0) 134 135 #define error(message, err, args...) \ 136 do { \ 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...) \ 147 do { \ 148 if (_verbose >= VCRITICAL) \ 149 fprintf(stderr, "\t%s: "message, FAIL, ##args); \ 150 } while (0) 151 152 #endif 153