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_files.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include "ttail_search_files.h"
  2. int _ttail_search_closest_files(ttail_t* t)
  3. {
  4. int ret;
  5. ret = _ttail_search_closest_files_init(t);
  6. if(ret)
  7. {
  8. return ret;
  9. }
  10. /* Checking that files are sorted well */
  11. return 0;
  12. }
  13. int _ttail_search_closest_files_init(ttail_t* t)
  14. {
  15. struct stat stat;
  16. FILE *fp;
  17. size_t i;
  18. off_t *file_sz;
  19. t->session = (ttail_search_t*)malloc(sizeof(ttail_search_file_t));
  20. if(!t->session)
  21. {
  22. perror("Unable to allocate memory for search session");
  23. goto _ttail_search_closest_files_alloc_session_err;
  24. }
  25. memset(t->session, 0, sizeof(ttail_search_file_t));
  26. file_sz = malloc(sizeof(off_t)*t->logfile_sz);
  27. if(!file_sz)
  28. {
  29. perror("Unable to allocate memory for file sizes");
  30. goto _ttail_search_closest_files_alloc_err;
  31. }
  32. t->session->file.file_sz = file_sz;
  33. #ifdef HUGEFILE
  34. t->session->file.sz_div = 0;
  35. #endif
  36. for(i=0;i<t->logfile_sz;i++)
  37. {
  38. fp = t->logfile[i];
  39. if(!fp)
  40. {
  41. if(_ttail_file_reopen(t,i))
  42. {
  43. file_sz[i] = 0;
  44. continue;
  45. }
  46. }
  47. if(fstat(fileno(fp), &stat))
  48. {
  49. perror("Unable to get file size");
  50. goto _ttail_search_closest_files_err;
  51. }
  52. file_sz[i] = stat.st_size;
  53. }
  54. /* we got all real size, determining if we need a divisor */
  55. /*
  56. * not implemented
  57. */
  58. if(_ttail_search_closest_files_set_fsizes(t))
  59. {
  60. goto _ttail_search_closest_files_err;
  61. }
  62. t->session->file.buf_sz = 128;
  63. t->session->file.buf = malloc(t->session->file.buf_sz);
  64. if(!t->session->file.buf)
  65. {
  66. goto _ttail_search_closest_files_err;
  67. }
  68. return 0;
  69. _ttail_search_closest_files_err:
  70. free(file_sz);
  71. t->session->file.file_sz = NULL;
  72. _ttail_search_closest_files_alloc_err:
  73. free(t->session);
  74. _ttail_search_closest_files_alloc_session_err:
  75. return -1;
  76. }
  77. int _ttail_search_closest_files_set_fsizes(ttail_t* t)
  78. {
  79. size_t i;
  80. off_t *vfile, *vsz;
  81. vfile = malloc(sizeof(off_t)*t->logfile_sz);
  82. if(!vfile)
  83. {
  84. perror("Unable to allocate memory for file size sum");
  85. return -1;
  86. }
  87. t->session->file.vfile = vfile;
  88. vsz = &(t->session->file.vsz);
  89. *vsz = 0;
  90. for(i=0; i<t->logfile_sz;i++)
  91. {
  92. vfile[i] = *vsz;
  93. #ifdef HUGEFILE
  94. *vsz += t->session->file.file_sz[i] >> t->session->file.sz_div;
  95. #else
  96. *vsz += t->session->file.file_sz[i];
  97. #endif
  98. }
  99. t->session->file.vpos = 0;
  100. return 0;
  101. }
  102. inline int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
  103. {
  104. FILE *fp;
  105. long cur;
  106. memset(tm, 0, sizeof(struct tm)*2);
  107. fp = t->logfile[id];
  108. if(!fp)
  109. {
  110. return 1;
  111. }
  112. if(fseek(fp, 0, SEEK_SET) < 0)
  113. {
  114. perror("Unable to manipulate fp");
  115. return -1;
  116. }
  117. while(1)
  118. {
  119. if(ttail_getline(t, id) < 0)
  120. {
  121. return 1;
  122. }
  123. if(!ttail_logline2date(t, ttail_getline_buf(t), tm))
  124. {
  125. break;
  126. }
  127. }
  128. if(fseek(fp, -1, SEEK_END) < 0)
  129. {
  130. perror("Unable to manipulate fp");
  131. return -1;
  132. }
  133. while(1)
  134. {
  135. if((cur = _ttail_file_start_line(fp)) < 0)
  136. {
  137. return -1;
  138. }
  139. if(ttail_getline(t, id) < 0)
  140. {
  141. return 1;
  142. }
  143. if(!ttail_logline2date(t, ttail_getline_buf(t), tm+1))
  144. {
  145. break;
  146. }
  147. if(!cur)
  148. {
  149. return 1;
  150. }
  151. if(fseek(fp, cur-1, SEEK_SET) < 0)
  152. {
  153. perror("Unable to manipulate fp");
  154. return -1;
  155. }
  156. }
  157. return 0;
  158. }
  159. int _ttail_file_reopen(ttail_t* t, size_t id)
  160. {
  161. if(t->logfile[id])
  162. {
  163. fclose(t->logfile[id]);
  164. }
  165. t->logfile[id] = fopen(t->logfile_name[id], "r");
  166. if(!t->logfile[id] && t->verbose > 2)
  167. {
  168. fprintf(stderr, "Unable to reopen '%s'\n",
  169. t->logfile_name[id]);
  170. }
  171. return t->logfile[id]?0:-1;
  172. }
  173. inline long _ttail_file_next_line(FILE* f)
  174. {
  175. ssize_t s;
  176. size_t r;
  177. char *buff;
  178. long res;
  179. int c;
  180. r=0;
  181. buff = NULL;
  182. s = getline(&buff, &r, f);
  183. if(s == -1)
  184. {
  185. goto _ttail_file_next_line_err;
  186. }
  187. while(1)
  188. {
  189. c = getc(f);
  190. if(c == EOF)
  191. {
  192. return 0;
  193. }
  194. else if(c!='\n')
  195. {
  196. if(fseek(f, -1, SEEK_CUR)<0)
  197. {
  198. goto _ttail_file_next_line_err;
  199. }
  200. break;
  201. }
  202. }
  203. res = ftell(f);
  204. free(buff);
  205. return res;
  206. _ttail_file_next_line_err:
  207. free(buff);
  208. return -1;
  209. }
  210. inline long _ttail_file_start_line(FILE* f)
  211. {
  212. #define _STARTLN_BUFFLEN 32
  213. long res; /* function result */
  214. long read_beg, cur, last, start;
  215. int read_sz;
  216. int c;
  217. if((start = ftell(f)) < 0)
  218. {
  219. return -1;
  220. }
  221. res = 0;
  222. read_beg = start;
  223. while(!res && start)
  224. {
  225. if(fseek(f, read_beg, SEEK_SET) < 0)
  226. {
  227. return -1;
  228. }
  229. start = read_beg;
  230. read_sz = start <= _STARTLN_BUFFLEN?start:_STARTLN_BUFFLEN;
  231. read_beg = start - read_sz;
  232. if(fseek(f, read_beg, SEEK_SET) < 0)
  233. {
  234. return -1;
  235. }
  236. last = -1; /* last pos we saw a '\n' */
  237. cur = read_beg;
  238. while(cur <= start)
  239. {
  240. c = getc(f);
  241. if(c == EOF)
  242. {
  243. if(!res)
  244. {
  245. return 0;
  246. }
  247. break;
  248. }
  249. else if (c =='\n')
  250. {
  251. last = cur;
  252. }
  253. else if(last >= 0)
  254. {
  255. res = cur;
  256. last = -1;
  257. }
  258. cur++;
  259. }
  260. if(!read_beg)
  261. {
  262. break;
  263. }
  264. }
  265. if(fseek(f, res, SEEK_SET) < 0)
  266. {
  267. return -1;
  268. }
  269. return res;
  270. }
  271. inline off_t _ttail_from_search_from_end(ttail_t* t , size_t id ,
  272. const struct tm* tm)
  273. {
  274. FILE *f;
  275. struct tm curtm;
  276. off_t last;
  277. int ret;
  278. f = t->logfile[id];
  279. if(fseek(f, -1, SEEK_END) < 0)
  280. {
  281. return -1;
  282. }
  283. while(1)
  284. {
  285. last = _ttail_file_start_line(f);
  286. if(last < 0)
  287. {
  288. goto _ttail_from_search_from_end_err;
  289. }
  290. if(ttail_getline(t, id) < 0)
  291. {
  292. goto _ttail_from_search_from_end_err;
  293. }
  294. ret = ttail_logline2date(t, ttail_getline_buf(t), &curtm);
  295. if(ret < 0)
  296. {
  297. goto _ttail_from_search_from_end_err;
  298. }
  299. if(!ret && !ttail_tm_cmp(&curtm, tm))
  300. {
  301. /* found */
  302. break;
  303. }
  304. if(last == 0)
  305. {
  306. /* considere the begining of the file as the answer */
  307. return 0;
  308. }
  309. if(fseek(f, last-1, SEEK_CUR))
  310. {
  311. goto _ttail_from_search_from_end_err;
  312. }
  313. }
  314. return last;
  315. _ttail_from_search_from_end_err:
  316. return -1;
  317. }
  318. void _ttail_search_file_free(ttail_t* t)
  319. {
  320. if(!t->session)
  321. {
  322. return;
  323. }
  324. if(t->session->file.buf)
  325. {
  326. free(t->session->file.buf);
  327. }
  328. if(t->session->file.file_sz)
  329. {
  330. free(t->session->file.file_sz);
  331. }
  332. if(t->session->file.vfile)
  333. {
  334. free(t->session->file.vfile);
  335. }
  336. free(t->session);
  337. }