linux/tools/testing/selftests/futex/include/logging.h
<<
>>
Prefs
   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'
  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/* Verbosity setting for INFO messages */
  60#define VQUIET    0
  61#define VCRITICAL 1
  62#define VINFO     2
  63#define VMAX      VINFO
  64int _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 */
  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 * 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 */
  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 * 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 */
 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/* log level macros */
 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