timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ttail_check.h 3.0KB

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