timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ttail_search_files.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. int _ttail_search_file_sorted(ttail_t* t)
  103. {
  104. /* files start & stop log tm, file start id = i*2 file stop = i*2+1 */
  105. struct tm *ftm;
  106. ftm = malloc(sizeof(struct tm)*t->logfile_sz*2);
  107. if(!ftm)
  108. {
  109. perror("Unable to allocate memory");
  110. return -1;
  111. }
  112. free(ftm);
  113. return -1;
  114. }
  115. int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
  116. {
  117. FILE *fp;
  118. long cur;
  119. memset(tm, 0, sizeof(struct tm)*2);
  120. fp = t->logfile[id];
  121. if(!fp)
  122. {
  123. return 1;
  124. }
  125. if(fseek(fp, 0, SEEK_SET) < 0)
  126. {
  127. perror("Unable to manipulate fp");
  128. return -1;
  129. }
  130. while(1)
  131. {
  132. if(ttail_getline(t, id) < 0)
  133. {
  134. return 1;
  135. }
  136. if(!ttail_logline2date(t, ttail_getline_buf(t), tm))
  137. {
  138. break;
  139. }
  140. }
  141. if(fseek(fp, -1, SEEK_END) < 0)
  142. {
  143. perror("Unable to manipulate fp");
  144. return -1;
  145. }
  146. while(1)
  147. {
  148. if((cur = _ttail_file_start_line(fp)) < 0)
  149. {
  150. return -1;
  151. }
  152. if(ttail_getline(t, id) < 0)
  153. {
  154. return 1;
  155. }
  156. if(!ttail_logline2date(t, ttail_getline_buf(t), tm+1))
  157. {
  158. break;
  159. }
  160. if(!cur)
  161. {
  162. return 1;
  163. }
  164. if(fseek(fp, cur-1, SEEK_SET) < 0)
  165. {
  166. perror("Unable to manipulate fp");
  167. return -1;
  168. }
  169. }
  170. return 0;
  171. }
  172. int _ttail_file_reopen(ttail_t* t, size_t id)
  173. {
  174. if(t->logfile[id])
  175. {
  176. fclose(t->logfile[id]);
  177. }
  178. t->logfile[id] = fopen(t->logfile_name[id], "r");
  179. if(!t->logfile[id] && t->verbose > 2)
  180. {
  181. fprintf(stderr, "Unable to reopen '%s'\n",
  182. t->logfile_name[id]);
  183. }
  184. return t->logfile[id]?0:-1;
  185. }
  186. long _ttail_file_next_line(FILE* f)
  187. {
  188. ssize_t s;
  189. size_t r;
  190. char *buff;
  191. long res;
  192. int c;
  193. r=0;
  194. buff = NULL;
  195. s = getline(&buff, &r, f);
  196. if(s == -1)
  197. {
  198. goto _ttail_file_next_line_err;
  199. }
  200. while(1)
  201. {
  202. c = getc(f);
  203. if(c == EOF)
  204. {
  205. return 0;
  206. }
  207. else if(c!='\n')
  208. {
  209. if(fseek(f, -1, SEEK_CUR)<0)
  210. {
  211. goto _ttail_file_next_line_err;
  212. }
  213. break;
  214. }
  215. }
  216. res = ftell(f);
  217. free(buff);
  218. return res;
  219. _ttail_file_next_line_err:
  220. free(buff);
  221. return -1;
  222. }
  223. long _ttail_file_start_line(FILE* f)
  224. {
  225. #define _STARTLN_BUFFLEN 32
  226. long res; /* function result */
  227. long read_beg, cur, last, start;
  228. int read_sz;
  229. int c;
  230. if((start = ftell(f)) < 0)
  231. {
  232. return -1;
  233. }
  234. res = 0;
  235. read_beg = start;
  236. while(!res && start)
  237. {
  238. if(fseek(f, read_beg, SEEK_SET) < 0)
  239. {
  240. return -1;
  241. }
  242. start = read_beg;
  243. read_sz = start <= _STARTLN_BUFFLEN?start:_STARTLN_BUFFLEN;
  244. read_beg = start - read_sz;
  245. if(fseek(f, read_beg, SEEK_SET) < 0)
  246. {
  247. return -1;
  248. }
  249. last = -1; /* last pos we saw a '\n' */
  250. cur = read_beg;
  251. while(cur <= start)
  252. {
  253. c = getc(f);
  254. if(c == EOF)
  255. {
  256. if(!res)
  257. {
  258. return 0;
  259. }
  260. break;
  261. }
  262. else if (c =='\n')
  263. {
  264. last = cur;
  265. }
  266. else if(last >= 0)
  267. {
  268. res = cur;
  269. last = -1;
  270. }
  271. cur++;
  272. }
  273. if(!read_beg)
  274. {
  275. break;
  276. }
  277. }
  278. if(fseek(f, res, SEEK_SET) < 0)
  279. {
  280. return -1;
  281. }
  282. return res;
  283. }
  284. void _ttail_search_file_free(ttail_t* t)
  285. {
  286. if(!t->session)
  287. {
  288. return;
  289. }
  290. if(t->session->file.buf)
  291. {
  292. free(t->session->file.buf);
  293. }
  294. if(t->session->file.file_sz)
  295. {
  296. free(t->session->file.file_sz);
  297. }
  298. if(t->session->file.vfile)
  299. {
  300. free(t->session->file.vfile);
  301. }
  302. free(t->session);
  303. }