logging.h 3.6 KB

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