timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ttail_check.h 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #ifndef _ttail_check_h__
  2. #define _ttail_check_h__
  3. #include <check.h>
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <libgen.h>
  8. #include "ttail.h"
  9. #include "ttail_init.h"
  10. ttail_t *ttail;
  11. #define __Fname_sz 5
  12. #define FNAME_NO_EXIST 0
  13. #define FNAME_EXIST 1
  14. char *fname[__Fname_sz];
  15. /*date formats*/
  16. char *fmt[] = TTAIL_DEFAULT_FORMATS;
  17. /* logfiles samples */
  18. char *samples[6] = { "./samples/1.log", "./samples/2.log", "./samples/3.log",\
  19. "./samples/4.log", "./samples/5.log", "./samples/1.1.log"};
  20. off_t samples_sz[6] = { 442, 0, 893, 2587, 2310, 220 };
  21. FILE *fpl;
  22. void teardown_ttail(void)
  23. {
  24. ttail_free(ttail);
  25. }
  26. void setup_ttail_empty(void)
  27. {
  28. ttail = ttail_init(1, (char**)&"foo");
  29. }
  30. void setup_fname(void)
  31. {
  32. int i;
  33. FILE *fp;
  34. for(i=0;i<__Fname_sz;i++)
  35. {
  36. fname[i] = NULL;
  37. }
  38. for(i=0; i<__Fname_sz; i++)
  39. {
  40. fname[i] = tempnam(NULL, "ttail_check");
  41. if(i%2)
  42. {
  43. if((fp=fopen(fname[i], "w+")) == NULL)
  44. {
  45. perror("Unable to create file for testing");
  46. ck_abort_msg("Unable to create file for testing");
  47. }
  48. if(fclose(fp))
  49. {
  50. perror("Unable to close file for testing");
  51. ck_abort_msg("Unable to close file for testing");
  52. }
  53. }
  54. }
  55. }
  56. void teardown_fname(void)
  57. {
  58. int i;
  59. for(i=0; i<__Fname_sz; i++)
  60. {
  61. unlink(fname[i]);
  62. if(fname[i] != NULL)
  63. {
  64. free(fname[i]);
  65. }
  66. }
  67. }
  68. void setup_file_line(void)
  69. {
  70. fpl = fopen(samples[0], "r");
  71. if(!fpl)
  72. {
  73. perror("Unable to open file for testing :");
  74. ck_abort();
  75. }
  76. }
  77. void teardown_file_line(void)
  78. {
  79. fclose(fpl);
  80. }
  81. void teardown_closest(void)
  82. {
  83. ttail_free(ttail);
  84. }
  85. void setup_closest(void)
  86. {
  87. ttail = ttail_init(1, (char**)&"foo");
  88. }
  89. void setup_closest_fileinit(void)
  90. {
  91. size_t i;
  92. setup_closest();
  93. for(i=0; i<6; i++)
  94. {
  95. ttail_add_logfile(ttail, samples[i]);
  96. }
  97. }
  98. void teardown_closest_fileinit(void)
  99. {
  100. _ttail_search_file_free(ttail);
  101. teardown_closest();
  102. }
  103. /**@brief Start a ttail test
  104. *@param const char* suite_str test suite string
  105. *@param const char* tc_str test case string
  106. */
  107. #define TTAIL_CHECK_START(suite_str, tc_str) \
  108. Suite * ttail_test_suite(void) \
  109. {\
  110. Suite *s;\
  111. TCase *tc;\
  112. s = suite_create(suite_str);\
  113. tc = tcase_create(tc_str);
  114. /**@brief Set the setup & teardown fixture
  115. *@param void (*setup)()
  116. *@param void (*teardown)()
  117. */
  118. #define TTAIL_SET_FIXTURE(setup, teardown) \
  119. tcase_add_checked_fixture(tc, setup, teardown)
  120. /**@brief Add a test function defined using START_TEST(name) { ... } END_TEST
  121. */
  122. #define TTAIL_ADD_TEST(test) tcase_add_test(tc, test)
  123. /**@brief End a ttail test */
  124. #define TTAIL_CHECK_END \
  125. suite_add_tcase(s, tc);\
  126. return s;\
  127. }\
  128. \
  129. int main(int argc, char **argv) {\
  130. int n_fail;\
  131. SRunner *sr;\
  132. chdir(dirname(argv[0])); /* move in ./tests dir */ \
  133. sr = srunner_create(ttail_test_suite());\
  134. srunner_set_fork_status(sr, CK_FORK);\
  135. srunner_run_all(sr, CK_VERBOSE);\
  136. n_fail = srunner_ntests_failed(sr);\
  137. srunner_free(sr);\
  138. return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;\
  139. }
  140. #endif