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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. {"#-2h", 7200}, {"#-12h", 43200},
  65. {"#-1d", 3600*24}, {"#-2day", 3600*24*2},
  66. };
  67. int r, i, c;
  68. double d;
  69. struct tm* tm;
  70. for(i=0; i<sizeof(datas) / (sizeof(char*)+sizeof(double)); i++)
  71. {
  72. for(c=0;c<2;c++)
  73. {
  74. tm = c?&(ttail->date_max):&(ttail->date_min);
  75. ttail_tm_init(tm);
  76. r = _ttail_set_date_relative(ttail, datas[i].f, c);
  77. ck_assert_int_eq(r, 0);
  78. d = tm_diff(now, tm);
  79. ck_assert_printf( d == datas[i].d,
  80. "Failed to set relative date for %s '%s' : \
  81. expected diff was %0.0f but %0.0f was found", c?"date_max":"date_min",
  82. datas[i].f, datas[i].d, d);
  83. }
  84. }
  85. }
  86. END_TEST
  87. START_TEST (fmt_mon)
  88. {
  89. char *fmts[] = { "#-1M", "#-1Month"};
  90. int r, i, c;
  91. struct tm* tm;
  92. for(i=0; i<sizeof(fmts) / sizeof(char*); i++)
  93. {
  94. for(c=0; c<2; c++)
  95. {
  96. tm = c?&(ttail->date_max):&(ttail->date_min);
  97. ttail_tm_init(tm);
  98. r = _ttail_set_date_relative(ttail, "#-1M", c);
  99. ck_assert_int_eq(r, 0);
  100. ck_assert_int_eq(tm->tm_sec, now->tm_sec);
  101. ck_assert_int_eq(tm->tm_min, now->tm_min);
  102. ck_assert_int_eq(tm->tm_hour, now->tm_hour);
  103. ck_assert_int_eq(tm->tm_mday, now->tm_mday);
  104. if(now->tm_mon != 0)
  105. {
  106. ck_assert_int_eq(tm->tm_mon,
  107. now->tm_mon - 1);
  108. ck_assert_int_eq(tm->tm_year,
  109. now->tm_year);
  110. }
  111. else
  112. {
  113. ck_assert_int_eq(tm->tm_mon, 11);
  114. ck_assert_int_eq(tm->tm_year,
  115. now->tm_year - 1);
  116. }
  117. }
  118. }
  119. }
  120. END_TEST
  121. START_TEST (fmt_year)
  122. {
  123. int r, i, c;
  124. struct tm *tm;
  125. struct check_relative_date_s datas[] = {
  126. {"#-1y", 1} , {"#-10year", 10}};
  127. for(i=0; i<sizeof(datas) / (sizeof(char*)+sizeof(double)); i++)
  128. {
  129. for(c=0;c<2;c++)
  130. {
  131. tm = c?&(ttail->date_max):&(ttail->date_min);
  132. ttail_tm_init(tm);
  133. r = _ttail_set_date_relative(ttail, datas[i].f, c);
  134. ck_assert_int_eq(r, 0);
  135. ck_assert_int_eq(tm->tm_sec, now->tm_sec);
  136. ck_assert_int_eq(tm->tm_min, now->tm_min);
  137. ck_assert_int_eq(tm->tm_hour, now->tm_hour);
  138. ck_assert_int_eq(tm->tm_mday, now->tm_mday);
  139. ck_assert_int_eq(tm->tm_mon, now->tm_mon);
  140. ck_assert_printf(
  141. now->tm_year - tm->tm_year == datas[i].d,
  142. "Failed for %s with format '%s' : found %d \
  143. year of difference instead of %d\n", c?"date_max":"date_min", datas[i].f,
  144. now->tm_year - tm->tm_year, (int)datas[i].d);
  145. }
  146. }
  147. }
  148. END_TEST
  149. TTAIL_CHECK_START("ttail relative date checks", "ttail_set_date_relative() checks")
  150. TTAIL_SET_FIXTURE(setup_date_relative, teardown_ttail);
  151. TTAIL_ADD_TEST(wrong_formats);
  152. TTAIL_ADD_TEST(fmt_sec);
  153. TTAIL_ADD_TEST(fmt_mon);
  154. TTAIL_ADD_TEST(fmt_year);
  155. TTAIL_CHECK_END