test_log_level_switches.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #ifndef ZF_LOG_LEVEL
  2. #error ZF_LOG_LEVEL must be defined for this test
  3. #endif
  4. #include <zf_log.c>
  5. #include <zf_test.h>
  6. #include <string.h>
  7. #include <stdbool.h>
  8. static int g_output_lvl_used;
  9. static unsigned g_output_called;
  10. static unsigned g_arg_called;
  11. static char g_msg[1024];
  12. static unsigned g_msg_len;
  13. static const int c_levels[] =
  14. {
  15. ZF_LOG_VERBOSE,
  16. ZF_LOG_DEBUG,
  17. ZF_LOG_INFO,
  18. ZF_LOG_WARN,
  19. ZF_LOG_ERROR,
  20. ZF_LOG_FATAL,
  21. ZF_LOG_NONE,
  22. };
  23. static void reset()
  24. {
  25. g_output_called = 0;
  26. g_arg_called = 0;
  27. zf_log_set_output_level(0);
  28. }
  29. static void mock_output_callback(const zf_log_message *msg, void *arg)
  30. {
  31. (void)arg;
  32. g_output_lvl_used = msg->lvl;
  33. g_msg_len = (unsigned)(msg->p - msg->buf);
  34. memcpy(g_msg, msg->buf, g_msg_len);
  35. ++g_output_called;
  36. }
  37. static int get_arg()
  38. {
  39. ++g_arg_called;
  40. return 0;
  41. }
  42. static void test_current_level()
  43. {
  44. reset();
  45. ZF_LOGV("verbose log");
  46. TEST_VERIFY_EQUAL(1 == g_output_called, ZF_LOG_LEVEL <= ZF_LOG_VERBOSE);
  47. TEST_VERIFY_TRUE(0 == g_output_called || ZF_LOG_VERBOSE == g_output_lvl_used);
  48. reset();
  49. ZF_LOGD("debug log");
  50. TEST_VERIFY_EQUAL(1 == g_output_called, ZF_LOG_LEVEL <= ZF_LOG_DEBUG);
  51. TEST_VERIFY_TRUE(0 == g_output_called || ZF_LOG_DEBUG == g_output_lvl_used);
  52. reset();
  53. ZF_LOGI("info log");
  54. TEST_VERIFY_EQUAL(1 == g_output_called, ZF_LOG_LEVEL <= ZF_LOG_INFO);
  55. TEST_VERIFY_TRUE(0 == g_output_called || ZF_LOG_INFO == g_output_lvl_used);
  56. reset();
  57. ZF_LOGW("warning log");
  58. TEST_VERIFY_EQUAL(1 == g_output_called, ZF_LOG_LEVEL <= ZF_LOG_WARN);
  59. TEST_VERIFY_TRUE(0 == g_output_called || ZF_LOG_WARN == g_output_lvl_used);
  60. reset();
  61. ZF_LOGE("error log");
  62. TEST_VERIFY_EQUAL(1 == g_output_called, ZF_LOG_LEVEL <= ZF_LOG_ERROR);
  63. TEST_VERIFY_TRUE(0 == g_output_called || ZF_LOG_ERROR == g_output_lvl_used);
  64. reset();
  65. ZF_LOGF("fatal log");
  66. TEST_VERIFY_EQUAL(1 == g_output_called, ZF_LOG_LEVEL <= ZF_LOG_FATAL);
  67. TEST_VERIFY_TRUE(0 == g_output_called || ZF_LOG_FATAL == g_output_lvl_used);
  68. }
  69. static void test_output_level()
  70. {
  71. for (unsigned i = 0; _countof(c_levels) > i; ++i)
  72. {
  73. const int lvl = c_levels[i];
  74. reset();
  75. zf_log_set_output_level(lvl);
  76. ZF_LOGV("verbose log");
  77. TEST_VERIFY_EQUAL(1 == g_output_called,
  78. ZF_LOG_LEVEL <= ZF_LOG_VERBOSE && lvl <= ZF_LOG_VERBOSE);
  79. TEST_VERIFY_TRUE(0 == g_output_called || ZF_LOG_VERBOSE == g_output_lvl_used);
  80. reset();
  81. zf_log_set_output_level(lvl);
  82. ZF_LOGD("debug log");
  83. TEST_VERIFY_EQUAL(1 == g_output_called,
  84. ZF_LOG_LEVEL <= ZF_LOG_DEBUG && lvl <= ZF_LOG_DEBUG);
  85. TEST_VERIFY_TRUE(0 == g_output_called || ZF_LOG_DEBUG == g_output_lvl_used);
  86. reset();
  87. zf_log_set_output_level(lvl);
  88. ZF_LOGI("info log");
  89. TEST_VERIFY_EQUAL(1 == g_output_called,
  90. ZF_LOG_LEVEL <= ZF_LOG_INFO && lvl <= ZF_LOG_INFO);
  91. TEST_VERIFY_TRUE(0 == g_output_called || ZF_LOG_INFO == g_output_lvl_used);
  92. reset();
  93. zf_log_set_output_level(lvl);
  94. ZF_LOGW("warn log");
  95. TEST_VERIFY_EQUAL(1 == g_output_called,
  96. ZF_LOG_LEVEL <= ZF_LOG_WARN && lvl <= ZF_LOG_WARN);
  97. TEST_VERIFY_TRUE(0 == g_output_called || ZF_LOG_WARN == g_output_lvl_used);
  98. reset();
  99. zf_log_set_output_level(lvl);
  100. ZF_LOGE("error log");
  101. TEST_VERIFY_EQUAL(1 == g_output_called,
  102. ZF_LOG_LEVEL <= ZF_LOG_ERROR && lvl <= ZF_LOG_ERROR);
  103. TEST_VERIFY_TRUE(0 == g_output_called || ZF_LOG_ERROR == g_output_lvl_used);
  104. reset();
  105. zf_log_set_output_level(lvl);
  106. ZF_LOGF("fatal log");
  107. TEST_VERIFY_EQUAL(1 == g_output_called,
  108. ZF_LOG_LEVEL <= ZF_LOG_FATAL && lvl <= ZF_LOG_FATAL);
  109. TEST_VERIFY_TRUE(0 == g_output_called || ZF_LOG_FATAL == g_output_lvl_used);
  110. }
  111. }
  112. static void test_args_evaluation()
  113. {
  114. reset();
  115. ZF_LOGV("verbose log: %i", get_arg());
  116. TEST_VERIFY_EQUAL(1 == g_arg_called, ZF_LOG_LEVEL <= ZF_LOG_VERBOSE);
  117. reset();
  118. ZF_LOGD("debug log: %i", get_arg());
  119. TEST_VERIFY_EQUAL(1 == g_arg_called, ZF_LOG_LEVEL <= ZF_LOG_DEBUG);
  120. reset();
  121. ZF_LOGI("info log: %i", get_arg());
  122. TEST_VERIFY_EQUAL(1 == g_arg_called, ZF_LOG_LEVEL <= ZF_LOG_INFO);
  123. reset();
  124. ZF_LOGW("warning log: %i", get_arg());
  125. TEST_VERIFY_EQUAL(1 == g_arg_called, ZF_LOG_LEVEL <= ZF_LOG_WARN);
  126. reset();
  127. ZF_LOGE("error log: %i", get_arg());
  128. TEST_VERIFY_EQUAL(1 == g_arg_called, ZF_LOG_LEVEL <= ZF_LOG_ERROR);
  129. reset();
  130. ZF_LOGF("fatal log: %i", get_arg());
  131. TEST_VERIFY_EQUAL(1 == g_arg_called, ZF_LOG_LEVEL <= ZF_LOG_FATAL);
  132. for (unsigned i = 0; _countof(c_levels) > i; ++i)
  133. {
  134. const int lvl = c_levels[i];
  135. reset();
  136. zf_log_set_output_level(lvl);
  137. ZF_LOGV("verbose log: %i", get_arg());
  138. TEST_VERIFY_EQUAL(1 == g_arg_called,
  139. ZF_LOG_LEVEL <= ZF_LOG_VERBOSE && lvl <= ZF_LOG_VERBOSE);
  140. reset();
  141. zf_log_set_output_level(lvl);
  142. ZF_LOGD("debug log: %i", get_arg());
  143. TEST_VERIFY_EQUAL(1 == g_arg_called,
  144. ZF_LOG_LEVEL <= ZF_LOG_DEBUG && lvl <= ZF_LOG_DEBUG);
  145. reset();
  146. zf_log_set_output_level(lvl);
  147. ZF_LOGI("info log: %i", get_arg());
  148. TEST_VERIFY_EQUAL(1 == g_arg_called,
  149. ZF_LOG_LEVEL <= ZF_LOG_INFO && lvl <= ZF_LOG_INFO);
  150. reset();
  151. zf_log_set_output_level(lvl);
  152. ZF_LOGW("warn log: %i", get_arg());
  153. TEST_VERIFY_EQUAL(1 == g_arg_called,
  154. ZF_LOG_LEVEL <= ZF_LOG_WARN && lvl <= ZF_LOG_WARN);
  155. reset();
  156. zf_log_set_output_level(lvl);
  157. ZF_LOGE("error log: %i", get_arg());
  158. TEST_VERIFY_EQUAL(1 == g_arg_called,
  159. ZF_LOG_LEVEL <= ZF_LOG_ERROR && lvl <= ZF_LOG_ERROR);
  160. reset();
  161. zf_log_set_output_level(lvl);
  162. ZF_LOGF("fatal log: %i", get_arg());
  163. TEST_VERIFY_EQUAL(1 == g_arg_called,
  164. ZF_LOG_LEVEL <= ZF_LOG_FATAL && lvl <= ZF_LOG_FATAL);
  165. }
  166. }
  167. static void test_level_checks()
  168. {
  169. reset();
  170. TEST_VERIFY_EQUAL(!!ZF_LOG_ENABLED_VERBOSE, ZF_LOG_LEVEL <= ZF_LOG_VERBOSE);
  171. TEST_VERIFY_EQUAL(!!ZF_LOG_ENABLED_DEBUG, ZF_LOG_LEVEL <= ZF_LOG_DEBUG);
  172. TEST_VERIFY_EQUAL(!!ZF_LOG_ENABLED_INFO, ZF_LOG_LEVEL <= ZF_LOG_INFO);
  173. TEST_VERIFY_EQUAL(!!ZF_LOG_ENABLED_WARN, ZF_LOG_LEVEL <= ZF_LOG_WARN);
  174. TEST_VERIFY_EQUAL(!!ZF_LOG_ENABLED_ERROR, ZF_LOG_LEVEL <= ZF_LOG_ERROR);
  175. TEST_VERIFY_EQUAL(!!ZF_LOG_ENABLED_FATAL, ZF_LOG_LEVEL <= ZF_LOG_FATAL);
  176. for (unsigned i = 0; _countof(c_levels) > i; ++i)
  177. {
  178. const int lvl = c_levels[i];
  179. reset();
  180. zf_log_set_output_level(lvl);
  181. TEST_VERIFY_EQUAL(!!ZF_LOG_ON_VERBOSE,
  182. ZF_LOG_LEVEL <= ZF_LOG_VERBOSE && lvl <= ZF_LOG_VERBOSE);
  183. TEST_VERIFY_EQUAL(!!ZF_LOG_ON_DEBUG,
  184. ZF_LOG_LEVEL <= ZF_LOG_DEBUG && lvl <= ZF_LOG_DEBUG);
  185. TEST_VERIFY_EQUAL(!!ZF_LOG_ON_INFO,
  186. ZF_LOG_LEVEL <= ZF_LOG_INFO && lvl <= ZF_LOG_INFO);
  187. TEST_VERIFY_EQUAL(!!ZF_LOG_ON_WARN,
  188. ZF_LOG_LEVEL <= ZF_LOG_WARN && lvl <= ZF_LOG_WARN);
  189. TEST_VERIFY_EQUAL(!!ZF_LOG_ON_ERROR,
  190. ZF_LOG_LEVEL <= ZF_LOG_ERROR && lvl <= ZF_LOG_ERROR);
  191. TEST_VERIFY_EQUAL(!!ZF_LOG_ON_FATAL,
  192. ZF_LOG_LEVEL <= ZF_LOG_FATAL && lvl <= ZF_LOG_FATAL);
  193. }
  194. }
  195. int main(int argc, char *argv[])
  196. {
  197. zf_log_set_output_v(ZF_LOG_PUT_STD, 0, mock_output_callback);
  198. TEST_RUNNER_CREATE(argc, argv);
  199. TEST_EXECUTE(test_current_level());
  200. TEST_EXECUTE(test_output_level());
  201. TEST_EXECUTE(test_args_evaluation());
  202. TEST_EXECUTE(test_level_checks());
  203. return TEST_RUNNER_EXIT_CODE();
  204. }