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

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