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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. if(!ttail_permissive(t))
  61. {
  62. ttail_strict_msg();
  63. break;
  64. }
  65. }
  66. if(fmt_id < 0)
  67. {
  68. fprintf(stderr, "Unable to detect date format from stdin\
  69. after %d lines were readed\n", max_lines);
  70. return -1;
  71. }
  72. buff = fmt[fmt_id];
  73. t->fmt = malloc(sizeof(char) * (strlen(buff)+1));
  74. if(!t->fmt)
  75. {
  76. perror("Unable to allocate memory for date format");
  77. return -1;
  78. }
  79. strcpy(t->fmt, buff);
  80. t->flag |= TTAIL_FLAG_FORMAT;
  81. return 0;
  82. }
  83. int _ttail_search_closest_stdin(ttail_t* t)
  84. {
  85. struct tm tm;
  86. ssize_t rd_sz;
  87. int ret, tmp;
  88. if(!(t->flag & TTAIL_FLAG_DATE_MIN))
  89. {
  90. return 0;
  91. }
  92. if(! t->session->std.buff)
  93. {
  94. if((rd_sz = ttail_std_getline(t)) < 0)
  95. {
  96. perror("Unable to read stdin");
  97. return -1;
  98. }
  99. }
  100. else
  101. {
  102. /*first line allready in the buffer thank's to format
  103. *detection*/
  104. rd_sz = strlen(ttail_std_getline_buff(t)) + 1;
  105. }
  106. while(rd_sz > 0)
  107. {
  108. ttail_tm_init(&tm);
  109. ret = ttail_logline2date(t, ttail_std_getline_buff(t), &tm);
  110. if(ret < 0)
  111. {
  112. return -1;
  113. }
  114. else if(ret > 0)
  115. {
  116. if(!ttail_permissive(t))
  117. {
  118. fprintf(stderr,
  119. "Unable to find the %s in logline",
  120. ret==1?"prefix":"date");
  121. if(t->verbose > 0)
  122. {
  123. fprintf(stderr, " : '%s'\n",
  124. ttail_file_getline_buf(t));
  125. }
  126. else
  127. {
  128. fprintf(stderr, "\n");
  129. }
  130. ttail_strict_msg();
  131. }
  132. continue;
  133. }
  134. ret = ttail_tm_cmp(&tm, &(t->date_min));
  135. if(ret >= 0)
  136. {
  137. //buffer contains the first line to print
  138. return 0;
  139. }
  140. rd_sz = ttail_std_getline(t);
  141. }
  142. if(rd_sz < 0)
  143. {
  144. tmp = errno;
  145. ret = feof(stdin);
  146. if(ret <= 0)
  147. {
  148. errno = tmp;
  149. perror("Error while reading loglines from stdin");
  150. return -1;
  151. }
  152. }
  153. return 1;
  154. }
  155. void _ttail_search_print_stdin(ttail_t* t, int fd)
  156. {
  157. struct tm tm;
  158. ssize_t rd_sz;
  159. int ret, tmp;
  160. rd_sz = strlen(ttail_std_getline_buff(t));
  161. if(!(t->flag & TTAIL_FLAG_DATE_MAX))
  162. {
  163. do
  164. {
  165. ret = write(1, ttail_std_getline_buff(t),
  166. sizeof(char)*rd_sz);
  167. if(ret < 0)
  168. {
  169. perror("Unable to write to stdout");
  170. return;
  171. }
  172. }
  173. while((rd_sz = ttail_std_getline(t)) > 0);
  174. }
  175. else
  176. {
  177. do
  178. {
  179. ttail_tm_init(&tm);
  180. ret = ttail_logline2date(t, ttail_std_getline_buff(t),
  181. &tm);
  182. //dropping errors
  183. if(ret == 0)
  184. {
  185. ret = ttail_tm_cmp(&tm, &(t->date_max));
  186. if(ret > 0)
  187. {
  188. return;
  189. }
  190. }
  191. ret = write(1, ttail_std_getline_buff(t),
  192. sizeof(char)*rd_sz);
  193. if(ret < 0)
  194. {
  195. perror("Unable to write to stdout");
  196. return;
  197. }
  198. }
  199. while((rd_sz = ttail_std_getline(t)) > 0);
  200. }
  201. if(rd_sz < 0)
  202. {
  203. tmp = errno;
  204. ret = feof(stdin);
  205. if(ret <= 0)
  206. {
  207. errno = tmp;
  208. perror("Error while reading loglines from stdin");
  209. }
  210. }
  211. return;
  212. }
  213. void _ttail_search_stdin_free(ttail_t* t)
  214. {
  215. if(t->session->std.buff)
  216. {
  217. free(t->session->std.buff);
  218. }
  219. }