logging.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifndef _LOGGING_H
  21. #define _LOGGING_H
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <linux/futex.h>
  25. #include "kselftest.h"
  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'
  40. static const char PASS_COLOR[] = {BRIGHT_GREEN, ' ', 'P', 'A', 'S', 'S',
  41. RESET_COLOR, 0};
  42. static const char ERROR_COLOR[] = {BRIGHT_YELLOW, 'E', 'R', 'R', 'O', 'R',
  43. RESET_COLOR, 0};
  44. static const char FAIL_COLOR[] = {BRIGHT_RED, ' ', 'F', 'A', 'I', 'L',
  45. RESET_COLOR, 0};
  46. static const char INFO_NORMAL[] = " INFO";
  47. static const char PASS_NORMAL[] = " PASS";
  48. static const char ERROR_NORMAL[] = "ERROR";
  49. static const char FAIL_NORMAL[] = " FAIL";
  50. const char *INFO = INFO_NORMAL;
  51. const char *PASS = PASS_NORMAL;
  52. const char *ERROR = ERROR_NORMAL;
  53. const char *FAIL = FAIL_NORMAL;
  54. /* Verbosity setting for INFO messages */
  55. #define VQUIET 0
  56. #define VCRITICAL 1
  57. #define VINFO 2
  58. #define VMAX VINFO
  59. int _verbose = VCRITICAL;
  60. /* Functional test return codes */
  61. #define RET_PASS 0
  62. #define RET_ERROR -1
  63. #define RET_FAIL -2
  64. /**
  65. * log_color() - Use colored output for PASS, ERROR, and FAIL strings
  66. * @use_color: use color (1) or not (0)
  67. */
  68. void log_color(int use_color)
  69. {
  70. if (use_color) {
  71. PASS = PASS_COLOR;
  72. ERROR = ERROR_COLOR;
  73. FAIL = FAIL_COLOR;
  74. } else {
  75. PASS = PASS_NORMAL;
  76. ERROR = ERROR_NORMAL;
  77. FAIL = FAIL_NORMAL;
  78. }
  79. }
  80. /**
  81. * log_verbosity() - Set verbosity of test output
  82. * @verbose: Enable (1) verbose output or not (0)
  83. *
  84. * Currently setting verbose=1 will enable INFO messages and 0 will disable
  85. * them. FAIL and ERROR messages are always displayed.
  86. */
  87. void log_verbosity(int level)
  88. {
  89. if (level > VMAX)
  90. level = VMAX;
  91. else if (level < 0)
  92. level = 0;
  93. _verbose = level;
  94. }
  95. /**
  96. * print_result() - Print standard PASS | ERROR | FAIL results
  97. * @ret: the return value to be considered: 0 | RET_ERROR | RET_FAIL
  98. *
  99. * print_result() is primarily intended for functional tests.
  100. */
  101. void print_result(int ret)
  102. {
  103. const char *result = "Unknown return code";
  104. switch (ret) {
  105. case RET_PASS:
  106. ksft_inc_pass_cnt();
  107. result = PASS;
  108. break;
  109. case RET_ERROR:
  110. result = ERROR;
  111. break;
  112. case RET_FAIL:
  113. ksft_inc_fail_cnt();
  114. result = FAIL;
  115. break;
  116. }
  117. printf("Result: %s\n", result);
  118. }
  119. /* log level macros */
  120. #define info(message, vargs...) \
  121. do { \
  122. if (_verbose >= VINFO) \
  123. fprintf(stderr, "\t%s: "message, INFO, ##vargs); \
  124. } while (0)
  125. #define error(message, err, args...) \
  126. do { \
  127. if (_verbose >= VCRITICAL) {\
  128. if (err) \
  129. fprintf(stderr, "\t%s: %s: "message, \
  130. ERROR, strerror(err), ##args); \
  131. else \
  132. fprintf(stderr, "\t%s: "message, ERROR, ##args); \
  133. } \
  134. } while (0)
  135. #define fail(message, args...) \
  136. do { \
  137. if (_verbose >= VCRITICAL) \
  138. fprintf(stderr, "\t%s: "message, FAIL, ##args); \
  139. } while (0)
  140. #endif