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_search_std.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2017 Yann Weber
  3. *
  4. * This file is part of Ttail.
  5. *
  6. * Ttail is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * Ttail is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Ttail. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "ttail_search_std.h"
  20. int ttail_search_std_init(ttail_t* t)
  21. {
  22. t->session = (ttail_search_t*)malloc(sizeof(ttail_search_stdin_t));
  23. if(!t->session)
  24. {
  25. perror("Unable to allocate memory for search session");
  26. return -1;
  27. }
  28. t->session->std.buff = NULL;
  29. t->session->std.buff_sz = 0;
  30. return 0;
  31. }
  32. int _ttail_search_fmtdetect_provider_std(ttail_t *t, int *try_left,
  33. size_t* line_sz, char*** lines_ptr)
  34. {
  35. return 1;
  36. }
  37. int _ttail_search_closest_stdin(ttail_t* t)
  38. {
  39. struct tm tm;
  40. ssize_t rd_sz;
  41. int ret, tmp;
  42. if(!(t->flag & TTAIL_FLAG_DATE_MIN))
  43. {
  44. return 0;
  45. }
  46. if(! t->session->std.buff)
  47. {
  48. rd_sz = ttail_std_getline(t);
  49. }
  50. else
  51. {
  52. rd_sz = strlen(ttail_std_getline_buff(t)) + 1;
  53. }
  54. while(rd_sz > 0)
  55. {
  56. /*first line allready in the buffer thank's to format
  57. *detection*/
  58. ttail_tm_init(&tm);
  59. ret = ttail_logline2date(t, ttail_std_getline_buff(t), &tm);
  60. if(ret < 0)
  61. {
  62. return -1;
  63. }
  64. else if(ret > 0)
  65. {
  66. continue;
  67. }
  68. ret = ttail_tm_cmp(&tm, &(t->date_min));
  69. if(ret >= 0)
  70. {
  71. //buffer contains the first line to print
  72. return 0;
  73. }
  74. rd_sz = ttail_std_getline(t);
  75. }
  76. if(rd_sz < 0)
  77. {
  78. tmp = errno;
  79. ret = feof(stdin);
  80. if(ret <= 0)
  81. {
  82. errno = tmp;
  83. perror("Error while reading loglines from stdin");
  84. return -1;
  85. }
  86. }
  87. return 0;
  88. }
  89. void _ttail_search_print_stdin(ttail_t* t, int fd)
  90. {
  91. struct tm tm;
  92. ssize_t rd_sz;
  93. int ret, tmp;
  94. rd_sz = strlen(ttail_std_getline_buff(t));
  95. if(!(t->flag & TTAIL_FLAG_DATE_MAX))
  96. {
  97. do
  98. {
  99. ret = write(1, ttail_std_getline_buff(t),
  100. sizeof(char)*rd_sz);
  101. if(ret < 0)
  102. {
  103. perror("Unable to write to stdout");
  104. return;
  105. }
  106. }
  107. while((rd_sz = ttail_std_getline(t)) > 0);
  108. }
  109. else
  110. {
  111. do
  112. {
  113. ttail_tm_init(&tm);
  114. ret = ttail_logline2date(t, ttail_std_getline_buff(t),
  115. &tm);
  116. //dropping errors
  117. if(ret == 0)
  118. {
  119. ret = ttail_tm_cmp(&tm, &(t->date_max));
  120. if(ret > 0)
  121. {
  122. return;
  123. }
  124. }
  125. ret = write(1, ttail_std_getline_buff(t),
  126. sizeof(char)*rd_sz);
  127. if(ret < 0)
  128. {
  129. perror("Unable to write to stdout");
  130. return;
  131. }
  132. }
  133. while((rd_sz = ttail_std_getline(t)) > 0);
  134. }
  135. if(rd_sz < 0)
  136. {
  137. tmp = errno;
  138. ret = feof(stdin);
  139. if(ret <= 0)
  140. {
  141. errno = tmp;
  142. perror("Error while reading loglines from stdin");
  143. }
  144. }
  145. return;
  146. }
  147. void _ttail_search_stdin_free(ttail_t* t)
  148. {
  149. if(t->session->std.buff)
  150. {
  151. free(t->session->std.buff);
  152. }
  153. }