Home | History | Annotate | Download | only in include
      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 (at) linux.intel.com>
     15  *
     16  * HISTORY
     17  *      2009-Nov-6: Initial version by Darren Hart <dvhart (at) 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 #include "kselftest.h"
     28 
     29 /*
     30  * Define PASS, ERROR, and FAIL strings with and without color escape
     31  * sequences, default to no color.
     32  */
     33 #define ESC 0x1B, '['
     34 #define BRIGHT '1'
     35 #define GREEN '3', '2'
     36 #define YELLOW '3', '3'
     37 #define RED '3', '1'
     38 #define ESCEND 'm'
     39 #define BRIGHT_GREEN ESC, BRIGHT, ';', GREEN, ESCEND
     40 #define BRIGHT_YELLOW ESC, BRIGHT, ';', YELLOW, ESCEND
     41 #define BRIGHT_RED ESC, BRIGHT, ';', RED, ESCEND
     42 #define RESET_COLOR ESC, '0', 'm'
     43 static const char PASS_COLOR[] = {BRIGHT_GREEN, ' ', 'P', 'A', 'S', 'S',
     44 				  RESET_COLOR, 0};
     45 static const char ERROR_COLOR[] = {BRIGHT_YELLOW, 'E', 'R', 'R', 'O', 'R',
     46 				   RESET_COLOR, 0};
     47 static const char FAIL_COLOR[] = {BRIGHT_RED, ' ', 'F', 'A', 'I', 'L',
     48 				  RESET_COLOR, 0};
     49 static const char INFO_NORMAL[] = " INFO";
     50 static const char PASS_NORMAL[] = " PASS";
     51 static const char ERROR_NORMAL[] = "ERROR";
     52 static const char FAIL_NORMAL[] = " FAIL";
     53 const char *INFO = INFO_NORMAL;
     54 const char *PASS = PASS_NORMAL;
     55 const char *ERROR = ERROR_NORMAL;
     56 const char *FAIL = FAIL_NORMAL;
     57 
     58 /* Verbosity setting for INFO messages */
     59 #define VQUIET    0
     60 #define VCRITICAL 1
     61 #define VINFO     2
     62 #define VMAX      VINFO
     63 int _verbose = VCRITICAL;
     64 
     65 /* Functional test return codes */
     66 #define RET_PASS   0
     67 #define RET_ERROR -1
     68 #define RET_FAIL  -2
     69 
     70 /**
     71  * log_color() - Use colored output for PASS, ERROR, and FAIL strings
     72  * @use_color:	use color (1) or not (0)
     73  */
     74 void log_color(int use_color)
     75 {
     76 	if (use_color) {
     77 		PASS = PASS_COLOR;
     78 		ERROR = ERROR_COLOR;
     79 		FAIL = FAIL_COLOR;
     80 	} else {
     81 		PASS = PASS_NORMAL;
     82 		ERROR = ERROR_NORMAL;
     83 		FAIL = FAIL_NORMAL;
     84 	}
     85 }
     86 
     87 /**
     88  * log_verbosity() - Set verbosity of test output
     89  * @verbose:	Enable (1) verbose output or not (0)
     90  *
     91  * Currently setting verbose=1 will enable INFO messages and 0 will disable
     92  * them. FAIL and ERROR messages are always displayed.
     93  */
     94 void log_verbosity(int level)
     95 {
     96 	if (level > VMAX)
     97 		level = VMAX;
     98 	else if (level < 0)
     99 		level = 0;
    100 	_verbose = level;
    101 }
    102 
    103 /**
    104  * print_result() - Print standard PASS | ERROR | FAIL results
    105  * @ret:	the return value to be considered: 0 | RET_ERROR | RET_FAIL
    106  *
    107  * print_result() is primarily intended for functional tests.
    108  */
    109 void print_result(int ret)
    110 {
    111 	const char *result = "Unknown return code";
    112 
    113 	switch (ret) {
    114 	case RET_PASS:
    115 		ksft_inc_pass_cnt();
    116 		result = PASS;
    117 		break;
    118 	case RET_ERROR:
    119 		result = ERROR;
    120 		break;
    121 	case RET_FAIL:
    122 		ksft_inc_fail_cnt();
    123 		result = FAIL;
    124 		break;
    125 	}
    126 	printf("Result: %s\n", result);
    127 }
    128 
    129 /* log level macros */
    130 #define info(message, vargs...) \
    131 do { \
    132 	if (_verbose >= VINFO) \
    133 		fprintf(stderr, "\t%s: "message, INFO, ##vargs); \
    134 } while (0)
    135 
    136 #define error(message, err, args...) \
    137 do { \
    138 	if (_verbose >= VCRITICAL) {\
    139 		if (err) \
    140 			fprintf(stderr, "\t%s: %s: "message, \
    141 				ERROR, strerror(err), ##args); \
    142 		else \
    143 			fprintf(stderr, "\t%s: "message, ERROR, ##args); \
    144 	} \
    145 } while (0)
    146 
    147 #define fail(message, args...) \
    148 do { \
    149 	if (_verbose >= VCRITICAL) \
    150 		fprintf(stderr, "\t%s: "message, FAIL, ##args); \
    151 } while (0)
    152 
    153 #endif
    154