timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ttail_check.h 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. void teardown_ttail(void)
  22. {
  23. ttail_free(ttail);
  24. }
  25. void setup_ttail_empty(void)
  26. {
  27. ttail = ttail_init(1, (char**)&"foo");
  28. }
  29. void setup_fname(void)
  30. {
  31. int i;
  32. FILE *fp;
  33. for(i=0;i<__Fname_sz;i++)
  34. {
  35. fname[i] = NULL;
  36. }
  37. for(i=0; i<__Fname_sz; i++)
  38. {
  39. fname[i] = tempnam(NULL, "ttail_check");
  40. if(i%2)
  41. {
  42. if((fp=fopen(fname[i], "w+")) == NULL)
  43. {
  44. perror("Unable to create file for testing");
  45. ck_abort_msg("Unable to create file for testing");
  46. }
  47. if(fclose(fp))
  48. {
  49. perror("Unable to close file for testing");
  50. ck_abort_msg("Unable to close file for testing");
  51. }
  52. }
  53. }
  54. }
  55. void teardown_fname(void)
  56. {
  57. int i;
  58. for(i=0; i<__Fname_sz; i++)
  59. {
  60. unlink(fname[i]);
  61. if(fname[i] != NULL)
  62. {
  63. free(fname[i]);
  64. }
  65. }
  66. }
  67. void setup_file_line(void)
  68. {
  69. setup_ttail_empty();
  70. ttail_add_logfile(ttail, samples[0]);
  71. }
  72. void teardown_closest(void)
  73. {
  74. ttail_free(ttail);
  75. }
  76. void setup_closest(void)
  77. {
  78. ttail = ttail_init(1, (char**)&"foo");
  79. }
  80. void setup_closest_fileinit(void)
  81. {
  82. size_t i;
  83. setup_closest();
  84. for(i=0; i<6; i++)
  85. {
  86. ttail_add_logfile(ttail, samples[i]);
  87. }
  88. }
  89. void teardown_closest_fileinit(void)
  90. {
  91. _ttail_search_file_free(ttail);
  92. teardown_closest();
  93. }
  94. /**@brief Start a ttail test
  95. *@param const char* suite_str test suite string
  96. *@param const char* tc_str test case string
  97. */
  98. #define TTAIL_CHECK_START(suite_str, tc_str) \
  99. Suite * ttail_test_suite(void) \
  100. {\
  101. Suite *s;\
  102. TCase *tc;\
  103. s = suite_create(suite_str);\
  104. tc = tcase_create(tc_str);
  105. /**@brief Set the setup & teardown fixture
  106. *@param void (*setup)()
  107. *@param void (*teardown)()
  108. */
  109. #define TTAIL_SET_FIXTURE(setup, teardown) \
  110. tcase_add_checked_fixture(tc, setup, teardown)
  111. /**@brief Add a test function defined using START_TEST(name) { ... } END_TEST
  112. */
  113. #define TTAIL_ADD_TEST(test) tcase_add_test(tc, test)
  114. /**@brief End a ttail test */
  115. #define TTAIL_CHECK_END \
  116. suite_add_tcase(s, tc);\
  117. return s;\
  118. }\
  119. \
  120. int main(int argc, char **argv) {\
  121. int n_fail;\
  122. SRunner *sr;\
  123. chdir(dirname(argv[0])); /* move in ./tests dir */ \
  124. sr = srunner_create(ttail_test_suite());\
  125. srunner_set_fork_status(sr, CK_FORK);\
  126. srunner_run_all(sr, CK_VERBOSE);\
  127. n_fail = srunner_ntests_failed(sr);\
  128. srunner_free(sr);\
  129. return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;\
  130. }
  131. #endif