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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. if(_ttail_search_std_fmt_init(t) < 0)
  31. {
  32. return -1;
  33. }
  34. return 0;
  35. }
  36. int _ttail_search_std_fmt_init(ttail_t* t)
  37. {
  38. ssize_t rd_sz;
  39. const char *buff;
  40. int fmt_id, max_lines, i;
  41. const char *fmt[] = TTAIL_DEFAULT_FORMATS;
  42. if(t->flag & TTAIL_FLAG_FORMAT)
  43. {
  44. return 1;
  45. }
  46. max_lines = 10;
  47. for(i=0; i<max_lines; i++)
  48. {
  49. if((rd_sz = ttail_std_getline(t)) < 0)
  50. {
  51. fprintf(stderr, "Unable to detect date from stdin\n");
  52. return -1;
  53. }
  54. buff = ttail_logline_subst(t, ttail_std_getline_buff(t));
  55. fmt_id = ttail_format_guess(buff, NULL);
  56. if(fmt_id >= 0)
  57. {
  58. break;
  59. }
  60. }
  61. if(fmt_id < 0)
  62. {
  63. fprintf(stderr, "Unable to detect date format from stdin\
  64. after %d lines were readed\n", max_lines);
  65. return -1;
  66. }
  67. buff = fmt[fmt_id];
  68. t->fmt = malloc(sizeof(char) * (strlen(buff)+1));
  69. if(!t->fmt)
  70. {
  71. perror("Unable to allocate memory for date format");
  72. return -1;
  73. }
  74. strcpy(t->fmt, buff);
  75. t->flag |= TTAIL_FLAG_FORMAT;
  76. return 0;
  77. }
  78. int _ttail_search_closest_stdin(ttail_t* t)
  79. {
  80. struct tm tm;
  81. ssize_t rd_sz;
  82. int ret, tmp;
  83. if(!(t->flag & TTAIL_FLAG_DATE_MIN))
  84. {
  85. return 0;
  86. }
  87. if(! t->session->std.buff)
  88. {
  89. if((rd_sz = ttail_std_getline(t)) < 0)
  90. {
  91. perror("Unable to read stdin");
  92. return -1;
  93. }
  94. }
  95. else
  96. {
  97. /*first line allready in the buffer thank's to format
  98. *detection*/
  99. rd_sz = strlen(ttail_std_getline_buff(t)) + 1;
  100. }
  101. while(rd_sz > 0)
  102. {
  103. ttail_tm_init(&tm);
  104. ret = ttail_logline2date(t, ttail_std_getline_buff(t), &tm);
  105. if(ret < 0)
  106. {
  107. return -1;
  108. }
  109. else if(ret > 0)
  110. {
  111. continue;
  112. }
  113. ret = ttail_tm_cmp(&tm, &(t->date_min));
  114. if(ret >= 0)
  115. {
  116. //buffer contains the first line to print
  117. return 0;
  118. }
  119. rd_sz = ttail_std_getline(t);
  120. }
  121. if(rd_sz < 0)
  122. {
  123. tmp = errno;
  124. ret = feof(stdin);
  125. if(ret <= 0)
  126. {
  127. errno = tmp;
  128. perror("Error while reading loglines from stdin");
  129. return -1;
  130. }
  131. }
  132. return 1;
  133. }
  134. void _ttail_search_print_stdin(ttail_t* t, int fd)
  135. {
  136. struct tm tm;
  137. ssize_t rd_sz;
  138. int ret, tmp;
  139. rd_sz = strlen(ttail_std_getline_buff(t));
  140. if(!(t->flag & TTAIL_FLAG_DATE_MAX))
  141. {
  142. do
  143. {
  144. ret = write(1, ttail_std_getline_buff(t),
  145. sizeof(char)*rd_sz);
  146. if(ret < 0)
  147. {
  148. perror("Unable to write to stdout");
  149. return;
  150. }
  151. }
  152. while((rd_sz = ttail_std_getline(t)) > 0);
  153. }
  154. else
  155. {
  156. do
  157. {
  158. ttail_tm_init(&tm);
  159. ret = ttail_logline2date(t, ttail_std_getline_buff(t),
  160. &tm);
  161. //dropping errors
  162. if(ret == 0)
  163. {
  164. ret = ttail_tm_cmp(&tm, &(t->date_max));
  165. if(ret > 0)
  166. {
  167. return;
  168. }
  169. }
  170. ret = write(1, ttail_std_getline_buff(t),
  171. sizeof(char)*rd_sz);
  172. if(ret < 0)
  173. {
  174. perror("Unable to write to stdout");
  175. return;
  176. }
  177. }
  178. while((rd_sz = ttail_std_getline(t)) > 0);
  179. }
  180. if(rd_sz < 0)
  181. {
  182. tmp = errno;
  183. ret = feof(stdin);
  184. if(ret <= 0)
  185. {
  186. errno = tmp;
  187. perror("Error while reading loglines from stdin");
  188. }
  189. }
  190. return;
  191. }
  192. void _ttail_search_stdin_free(ttail_t* t)
  193. {
  194. if(t->session->std.buff)
  195. {
  196. free(t->session->std.buff);
  197. }
  198. }