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_init_set_date_relative.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <check.h>
  2. #include <stdio.h>
  3. #include <libgen.h>
  4. #include <time.h>
  5. #include "ttail_check.h"
  6. #include "ttail.h"
  7. #include "ttail_init.h"
  8. #include "ttail_search.h"
  9. struct tm *now;
  10. struct check_relative_date_s
  11. {
  12. char * f;
  13. double d;
  14. };
  15. void setup_date_relative()
  16. {
  17. time_t t;
  18. if(time(&t) < 0)
  19. {
  20. fprintf(stderr,
  21. "CHECK FAIL : Unable to retrieve time using time()\n");
  22. exit(1);
  23. }
  24. if(!(now = localtime(&t)))
  25. {
  26. fprintf(stderr,
  27. "CHECK FAIL : Unable to retreive localtime()\n");
  28. exit(1);
  29. }
  30. setup_ttail_empty();
  31. }
  32. double tm_diff(struct tm *tm1, struct tm *tm2)
  33. {
  34. time_t t1,t2;
  35. t1 = mktime(tm1);
  36. t2 = mktime(tm2);
  37. return difftime(t1,t2);
  38. }
  39. START_TEST (wrong_formats)
  40. {
  41. int r,i;
  42. char *fmts[] = { "foobar", "%d", "#-1f", "#-1dour", "#-h",
  43. "-1h", "#-1h3m",
  44. "#--1hour", "ab1h", " 1h", "123h"
  45. };
  46. for(i=0; i<sizeof(fmts)/sizeof(char*);i++)
  47. {
  48. r = _ttail_set_date_relative(ttail, fmts[i], 0);
  49. ck_assert_printf(r == -1, "Returns %d with '%s' on date 0",
  50. r, fmts[i]);
  51. r = _ttail_set_date_relative(ttail, fmts[i], 1);
  52. ck_assert_printf(r == -1, "Returns %d with '%s' on date 1",
  53. r, fmts[i]);
  54. }
  55. }
  56. END_TEST
  57. /* Test for sec, min, hour, day formats. Comparing with tm_diff */
  58. START_TEST (fmt_sec)
  59. {
  60. struct check_relative_date_s datas[] = {
  61. {"#-10s", 10}, {"#-142sec", 142}, {"#-4096s", 4096},
  62. {"#-1m", 60}, {"#-10min", 600},
  63. {"#-1h", 3600}, {"#-10h", 36000},
  64. {"#-1d", 3600*24}, {"#-2day", 3600*24*2},
  65. };
  66. int r, i, c;
  67. double d;
  68. struct tm* tm;
  69. for(i=0; i<sizeof(datas) / (sizeof(char*)+sizeof(double)); i++)
  70. {
  71. for(c=0;c<2;c++)
  72. {
  73. tm = c?&(ttail->date_max):&(ttail->date_min);
  74. ttail_tm_init(tm);
  75. r = _ttail_set_date_relative(ttail, datas[i].f, c);
  76. ck_assert_int_eq(r, 0);
  77. d = tm_diff(now, tm);
  78. ck_assert_printf( d == datas[i].d,
  79. "Failed to set relative date for %s '%s' : \
  80. expected diff was %0.0f but %0.0f was found", c?"date_max":"date_min",
  81. datas[i].f, datas[i].d, d);
  82. }
  83. }
  84. }
  85. END_TEST
  86. START_TEST (fmt_mon)
  87. {
  88. char *fmts[] = { "#-1M", "#-1Month"};
  89. int r, i, c;
  90. struct tm* tm;
  91. for(i=0; i<sizeof(fmts) / sizeof(char*); i++)
  92. {
  93. for(c=0; c<2; c++)
  94. {
  95. tm = c?&(ttail->date_max):&(ttail->date_min);
  96. ttail_tm_init(tm);
  97. r = _ttail_set_date_relative(ttail, "#-1M", c);
  98. ck_assert_int_eq(r, 0);
  99. ck_assert_int_eq(tm->tm_sec, now->tm_sec);
  100. ck_assert_int_eq(tm->tm_min, now->tm_min);
  101. ck_assert_int_eq(tm->tm_hour, now->tm_hour);
  102. ck_assert_int_eq(tm->tm_mday, now->tm_mday);
  103. if(now->tm_mon != 0)
  104. {
  105. ck_assert_int_eq(tm->tm_mon,
  106. now->tm_mon - 1);
  107. ck_assert_int_eq(tm->tm_year,
  108. now->tm_year);
  109. }
  110. else
  111. {
  112. ck_assert_int_eq(tm->tm_mon, 11);
  113. ck_assert_int_eq(tm->tm_year,
  114. now->tm_year - 1);
  115. }
  116. }
  117. }
  118. }
  119. END_TEST
  120. START_TEST (fmt_year)
  121. {
  122. int r, i, c;
  123. struct tm *tm;
  124. struct check_relative_date_s datas[] = {
  125. {"#-1y", 1} , {"#-10year", 10}};
  126. for(i=0; i<sizeof(datas) / (sizeof(char*)+sizeof(double)); i++)
  127. {
  128. for(c=0;c<2;c++)
  129. {
  130. tm = c?&(ttail->date_max):&(ttail->date_min);
  131. ttail_tm_init(tm);
  132. r = _ttail_set_date_relative(ttail, datas[i].f, c);
  133. ck_assert_int_eq(r, 0);
  134. ck_assert_int_eq(tm->tm_sec, now->tm_sec);
  135. ck_assert_int_eq(tm->tm_min, now->tm_min);
  136. ck_assert_int_eq(tm->tm_hour, now->tm_hour);
  137. ck_assert_int_eq(tm->tm_mday, now->tm_mday);
  138. ck_assert_int_eq(tm->tm_mon, now->tm_mon);
  139. ck_assert_printf(
  140. now->tm_year - tm->tm_year == datas[i].d,
  141. "Failed for %s with format '%s' : found %d \
  142. year of difference instead of %d\n", c?"date_max":"date_min", datas[i].f,
  143. now->tm_year - tm->tm_year, (int)datas[i].d);
  144. }
  145. }
  146. }
  147. END_TEST
  148. TTAIL_CHECK_START("ttail relative date checks", "ttail_set_date_relative() checks")
  149. TTAIL_SET_FIXTURE(setup_date_relative, teardown_ttail);
  150. TTAIL_ADD_TEST(wrong_formats);
  151. TTAIL_ADD_TEST(fmt_sec);
  152. TTAIL_ADD_TEST(fmt_mon);
  153. TTAIL_ADD_TEST(fmt_year);
  154. TTAIL_CHECK_END