|
@@ -110,36 +110,7 @@ int _ttail_search_closest_files_set_fsizes(ttail_t* t)
|
110
|
110
|
return 0;
|
111
|
111
|
}
|
112
|
112
|
|
113
|
|
-int _ttail_search_file_sorted(ttail_t* t)
|
114
|
|
-{
|
115
|
|
- /* files start & stop log tm, file start id = i*2 file stop = i*2+1 */
|
116
|
|
- struct tm *ftm;
|
117
|
|
- size_t i;
|
118
|
|
-
|
119
|
|
- ftm = malloc(sizeof(struct tm)*t->logfile_sz*2);
|
120
|
|
- if(!ftm)
|
121
|
|
- {
|
122
|
|
- perror("Unable to allocate memory");
|
123
|
|
- return -3;
|
124
|
|
- }
|
125
|
|
- for(i=0; i<t->logfile_sz; i++)
|
126
|
|
- {
|
127
|
|
- _ttail_file_minmax(t, 0, ftm+(i*2));
|
128
|
|
- if(i)
|
129
|
|
- {
|
130
|
|
- if(ttail_tm_cmp(ftm+((i-1)*2)+1, ftm+i*2) > 0)
|
131
|
|
- {
|
132
|
|
- /* not sorted */
|
133
|
|
- free(ftm);
|
134
|
|
- return -1;
|
135
|
|
- }
|
136
|
|
- }
|
137
|
|
- }
|
138
|
|
- free(ftm);
|
139
|
|
- return 0;
|
140
|
|
-}
|
141
|
|
-
|
142
|
|
-int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
|
|
113
|
+inline int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
|
143
|
114
|
{
|
144
|
115
|
FILE *fp;
|
145
|
116
|
long cur;
|
|
@@ -212,7 +183,7 @@ int _ttail_file_reopen(ttail_t* t, size_t id)
|
212
|
183
|
return t->logfile[id]?0:-1;
|
213
|
184
|
}
|
214
|
185
|
|
215
|
|
-long _ttail_file_next_line(FILE* f)
|
|
186
|
+inline long _ttail_file_next_line(FILE* f)
|
216
|
187
|
{
|
217
|
188
|
ssize_t s;
|
218
|
189
|
size_t r;
|
|
@@ -253,7 +224,7 @@ long _ttail_file_next_line(FILE* f)
|
253
|
224
|
return -1;
|
254
|
225
|
}
|
255
|
226
|
|
256
|
|
-long _ttail_file_start_line(FILE* f)
|
|
227
|
+inline long _ttail_file_start_line(FILE* f)
|
257
|
228
|
{
|
258
|
229
|
#define _STARTLN_BUFFLEN 32
|
259
|
230
|
long res; /* function result */
|
|
@@ -316,6 +287,56 @@ long _ttail_file_start_line(FILE* f)
|
316
|
287
|
return res;
|
317
|
288
|
}
|
318
|
289
|
|
|
290
|
+inline off_t _ttail_from_search_from_end(ttail_t* t , size_t id ,
|
|
291
|
+ const struct tm* tm)
|
|
292
|
+{
|
|
293
|
+ FILE *f;
|
|
294
|
+ struct tm curtm;
|
|
295
|
+ off_t last;
|
|
296
|
+ int ret;
|
|
297
|
+
|
|
298
|
+ f = t->logfile[id];
|
|
299
|
+ if(fseek(f, -1, SEEK_END) < 0)
|
|
300
|
+ {
|
|
301
|
+ return -1;
|
|
302
|
+ }
|
|
303
|
+ while(1)
|
|
304
|
+ {
|
|
305
|
+ last = _ttail_file_start_line(f);
|
|
306
|
+ if(last < 0)
|
|
307
|
+ {
|
|
308
|
+ goto _ttail_from_search_from_end_err;
|
|
309
|
+ }
|
|
310
|
+ if(ttail_getline(t, id) < 0)
|
|
311
|
+ {
|
|
312
|
+ goto _ttail_from_search_from_end_err;
|
|
313
|
+ }
|
|
314
|
+ ret = ttail_logline2date(t, ttail_getline_buf(t), &curtm);
|
|
315
|
+ if(ret < 0)
|
|
316
|
+ {
|
|
317
|
+ goto _ttail_from_search_from_end_err;
|
|
318
|
+ }
|
|
319
|
+ if(!ret && !ttail_tm_cmp(&curtm, tm))
|
|
320
|
+ {
|
|
321
|
+ /* found */
|
|
322
|
+ break;
|
|
323
|
+ }
|
|
324
|
+ if(last == 0)
|
|
325
|
+ {
|
|
326
|
+ /* considere the begining of the file as the answer */
|
|
327
|
+ return 0;
|
|
328
|
+ }
|
|
329
|
+ if(fseek(f, last-1, SEEK_CUR))
|
|
330
|
+ {
|
|
331
|
+ goto _ttail_from_search_from_end_err;
|
|
332
|
+ }
|
|
333
|
+ }
|
|
334
|
+ return last;
|
|
335
|
+
|
|
336
|
+ _ttail_from_search_from_end_err:
|
|
337
|
+ return -1;
|
|
338
|
+}
|
|
339
|
+
|
319
|
340
|
void _ttail_search_file_free(ttail_t* t)
|
320
|
341
|
{
|
321
|
342
|
if(!t->session)
|