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

ttail_init_logfile.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <check.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include "ttail_check.h"
  6. #include "ttail.h"
  7. #include "ttail_init.h"
  8. /*
  9. * ttail_add_logfile() tests
  10. */
  11. START_TEST (test_init_empty_logfilename)
  12. {
  13. ck_assert_msg(ttail->logfile_name == NULL,
  14. "ttail_t.logfile_name should be NULL");
  15. }
  16. END_TEST
  17. START_TEST (test_init_nonexist_logfilename)
  18. {
  19. char *fname;
  20. int ret;
  21. /** @todo replace by mkstemp */
  22. fname = tempnam(NULL, "ttail_check");
  23. ret = ttail_add_logfile(ttail, fname);
  24. ck_assert_int_eq(ret, 1);
  25. ck_assert(ttail->logfile_name != NULL);
  26. ck_assert_str_eq(ttail->logfile_name[0], fname);
  27. ck_assert(ttail->logfile[0] == NULL);
  28. free(fname);
  29. }
  30. END_TEST
  31. START_TEST (test_init_exist_logfilename)
  32. {
  33. char *fname;
  34. int ret;
  35. FILE *fp;
  36. fname = tempnam(NULL, "ttail_check");
  37. if(!(fp = fopen(fname, "w+")))
  38. {
  39. perror("Unable to create file for testing");
  40. ck_abort_msg("Unable to create file for testing");
  41. }
  42. if(fclose(fp))
  43. {
  44. perror("Unable to close file for testing");
  45. ck_abort_msg("Unabe to close file for testing");
  46. }
  47. ret = ttail_add_logfile(ttail, fname);
  48. ck_assert_int_eq(ret, 0);
  49. ck_assert(ttail->logfile_name != NULL);
  50. ck_assert_str_eq(ttail->logfile_name[0], fname);
  51. ck_assert(ttail->logfile[0] != NULL);
  52. free(fname);
  53. }
  54. END_TEST
  55. START_TEST (test_init_same_exist_logfilename)
  56. {
  57. char *fname;
  58. int ret;
  59. FILE *fp;
  60. fname = tempnam(NULL, "ttail_check");
  61. if(!(fp = fopen(fname, "w+")))
  62. {
  63. perror("Unable to create file for testing");
  64. ck_abort_msg("Unable to create file for testing");
  65. }
  66. if(fclose(fp))
  67. {
  68. perror("Unable to close file for testing");
  69. ck_abort_msg("Unabe to close file for testing");
  70. }
  71. ret = ttail_add_logfile(ttail, fname);
  72. ck_assert_int_eq(ret, 0);
  73. ret = ttail_add_logfile(ttail, fname);
  74. ck_assert_int_eq(ret, -1);
  75. ck_assert_int_eq(ttail->logfile_sz, 1);
  76. ck_assert(ttail->logfile_name != NULL);
  77. ck_assert_str_eq(ttail->logfile_name[0], fname);
  78. ck_assert(ttail->logfile[0] != NULL);
  79. free(fname);
  80. }
  81. END_TEST
  82. START_TEST (test_init_same_nonexist_logfilename)
  83. {
  84. char *fname;
  85. int ret;
  86. fname = tempnam(NULL, "ttail_check");
  87. ret = ttail_add_logfile(ttail, fname);
  88. ck_assert_int_eq(ret, 1);
  89. ret = ttail_add_logfile(ttail, fname);
  90. ck_assert_int_eq(ret, -1);
  91. ck_assert_int_eq(ttail->logfile_sz, 1);
  92. ck_assert(ttail->logfile_name != NULL);
  93. ck_assert_str_eq(ttail->logfile_name[0], fname);
  94. ck_assert(ttail->logfile[0] == NULL);
  95. free(fname);
  96. }
  97. END_TEST
  98. START_TEST (test_init_multiple_logfilename)
  99. {
  100. char *fname[20];
  101. int ret;
  102. int i, j;
  103. for(i=0;i<20;i++)
  104. {
  105. fname[i] = tempnam(NULL, "ttail_check");
  106. }
  107. for(i=0; i<20; i++)
  108. {
  109. for(j=0; j<i; j++)
  110. {
  111. ret = ttail_add_logfile(ttail, fname[j]);
  112. ck_assert_int_eq(ret, -1);
  113. ck_assert_int_eq(ttail->logfile_sz, i);
  114. }
  115. ret = ttail_add_logfile(ttail, fname[i]);
  116. ck_assert_int_eq(ret, 1);
  117. ck_assert_int_eq(ttail->logfile_sz, i+1);
  118. for(j=0; j<i+1; j++)
  119. {
  120. ck_assert_str_eq(ttail->logfile_name[j], fname[j]);
  121. ck_assert(ttail->logfile[j] == NULL);
  122. }
  123. }
  124. for(i=0; i<20; i++)
  125. {
  126. free(fname[i]);
  127. }
  128. }
  129. END_TEST
  130. TTAIL_CHECK_START("ttail init checks", "logfile init checks")
  131. TTAIL_SET_FIXTURE(setup_ttail_empty, teardown_ttail);
  132. TTAIL_ADD_TEST(test_init_empty_logfilename);
  133. TTAIL_ADD_TEST(test_init_nonexist_logfilename);
  134. TTAIL_ADD_TEST(test_init_exist_logfilename);
  135. TTAIL_ADD_TEST(test_init_same_exist_logfilename);
  136. TTAIL_ADD_TEST(test_init_same_nonexist_logfilename);
  137. TTAIL_ADD_TEST(test_init_multiple_logfilename);
  138. TTAIL_CHECK_END