timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ttail_init_logfile.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. unlink(fname);
  53. free(fname);
  54. }
  55. END_TEST
  56. START_TEST (test_init_same_exist_logfilename)
  57. {
  58. char *fname;
  59. int ret;
  60. FILE *fp;
  61. fname = tempnam(NULL, "ttail_check");
  62. if(!(fp = fopen(fname, "w+")))
  63. {
  64. perror("Unable to create file for testing");
  65. ck_abort_msg("Unable to create file for testing");
  66. }
  67. if(fclose(fp))
  68. {
  69. perror("Unable to close file for testing");
  70. ck_abort_msg("Unabe to close file for testing");
  71. }
  72. ret = ttail_add_logfile(ttail, fname);
  73. ck_assert_int_eq(ret, 0);
  74. ret = ttail_add_logfile(ttail, fname);
  75. ck_assert_int_eq(ret, -1);
  76. ck_assert_int_eq(ttail->logfile_sz, 1);
  77. ck_assert(ttail->logfile_name != NULL);
  78. ck_assert_str_eq(ttail->logfile_name[0], fname);
  79. ck_assert(ttail->logfile[0] != NULL);
  80. unlink(fname);
  81. free(fname);
  82. }
  83. END_TEST
  84. START_TEST (test_init_same_nonexist_logfilename)
  85. {
  86. char *fname;
  87. int ret;
  88. fname = tempnam(NULL, "ttail_check");
  89. ret = ttail_add_logfile(ttail, fname);
  90. ck_assert_int_eq(ret, 1);
  91. ret = ttail_add_logfile(ttail, fname);
  92. ck_assert_int_eq(ret, -1);
  93. ck_assert_int_eq(ttail->logfile_sz, 1);
  94. ck_assert(ttail->logfile_name != NULL);
  95. ck_assert_str_eq(ttail->logfile_name[0], fname);
  96. ck_assert(ttail->logfile[0] == NULL);
  97. free(fname);
  98. }
  99. END_TEST
  100. START_TEST (test_init_multiple_logfilename)
  101. {
  102. char *fname[20];
  103. int ret;
  104. int i, j;
  105. for(i=0;i<20;i++)
  106. {
  107. fname[i] = tempnam(NULL, "ttail_check");
  108. }
  109. for(i=0; i<20; i++)
  110. {
  111. for(j=0; j<i; j++)
  112. {
  113. ret = ttail_add_logfile(ttail, fname[j]);
  114. ck_assert_int_eq(ret, -1);
  115. ck_assert_int_eq(ttail->logfile_sz, i);
  116. }
  117. ret = ttail_add_logfile(ttail, fname[i]);
  118. ck_assert_int_eq(ret, 1);
  119. ck_assert_int_eq(ttail->logfile_sz, i+1);
  120. for(j=0; j<i+1; j++)
  121. {
  122. ck_assert_str_eq(ttail->logfile_name[j], fname[j]);
  123. ck_assert(ttail->logfile[j] == NULL);
  124. }
  125. }
  126. for(i=0; i<20; i++)
  127. {
  128. unlink(fname[i]);
  129. free(fname[i]);
  130. }
  131. }
  132. END_TEST
  133. TTAIL_CHECK_START("ttail init checks", "logfile init checks")
  134. TTAIL_SET_FIXTURE(setup_ttail_empty, teardown_ttail);
  135. TTAIL_ADD_TEST(test_init_empty_logfilename);
  136. TTAIL_ADD_TEST(test_init_nonexist_logfilename);
  137. TTAIL_ADD_TEST(test_init_exist_logfilename);
  138. TTAIL_ADD_TEST(test_init_same_exist_logfilename);
  139. TTAIL_ADD_TEST(test_init_same_nonexist_logfilename);
  140. TTAIL_ADD_TEST(test_init_multiple_logfilename);
  141. TTAIL_CHECK_END