Browse Source

Bugfix & function implementation

Now we have a running minimal ttail :
src/ttail -d "Mar 6 00:29" -f '%B%n%d %H:%M' -l tests/samples/1.log -l tests/samples/1.1.log
Yann Weber 7 years ago
parent
commit
fd5d6cad18
6 changed files with 110 additions and 17 deletions
  1. 5
    0
      src/include/ttail_search.h
  2. 7
    0
      src/include/ttail_search_files.h
  3. 10
    2
      src/main.c
  4. 0
    1
      src/ttail_init.c
  5. 44
    8
      src/ttail_search.c
  6. 44
    6
      src/ttail_search_files.c

+ 5
- 0
src/include/ttail_search.h View File

@@ -55,6 +55,11 @@ int ttail_search_closest(ttail_t*, const struct tm*);
55 55
  *@return 0 if ok -1 if fatal error 1 if not found
56 56
  */
57 57
 int _ttail_search_closest_stdin(ttail_t*, const struct tm*);
58
+/**@brief Output result loglines to stdout
59
+ *@param ttail_t*
60
+ *@param int fd
61
+ */
62
+void _ttail_search_print_stdin(ttail_t*, int);
58 63
 
59 64
 /**@brief Loglines comparison function
60 65
  *@param ttail_t* ttail

+ 7
- 0
src/include/ttail_search_files.h View File

@@ -68,6 +68,13 @@ struct _ttail_search_file_s
68 68
  *@return 0 if ok -1 if fatal error 1 if not found
69 69
  */
70 70
 int _ttail_search_closest_files(ttail_t*, const struct tm*);
71
+
72
+/**@brief Output result loglines to stdout
73
+ *@param ttail_t*
74
+ *@param int fd
75
+ */
76
+void _ttail_search_print_files(ttail_t*, int);
77
+
71 78
 int _ttail_search_closest_files_init(ttail_t*);
72 79
 int _ttail_search_closest_files_set_fsizes(ttail_t*);
73 80
 

+ 10
- 2
src/main.c View File

@@ -1,14 +1,22 @@
1 1
 #include "include/ttail.h"
2 2
 #include "include/ttail_init.h"
3 3
 
4
+/**@mainpage
5
+ *
6
+ *Super options : -d -r -p -v -m
7
+ */
8
+
4 9
 int main(int argc, char **argv)
5 10
 {
6 11
 	ttail_t *ttail;
7
-	ttail = ttail_init(argc, argv);
12
+	if(!(ttail = ttail_init(argc, argv)))
13
+	{
14
+		return 1;
15
+	}
8 16
 	if(ttail_init_check(ttail) < 0)
9 17
 	{
10 18
 		ttail_free(ttail);
11 19
 		return 1;
12 20
 	}
13
-	return 0;
21
+	return ttail_search_closest(ttail, &(ttail->date_min));
14 22
 }

+ 0
- 1
src/ttail_init.c View File

@@ -88,7 +88,6 @@ given\n");
88 88
 
89 89
 				break;
90 90
 			case 'l':
91
-				fprintf(stderr,"-l with %s\n", optarg);
92 91
 				ret = ttail_add_logfile(res, optarg);
93 92
 				if(ret < 0)
94 93
 				{

+ 44
- 8
src/ttail_search.c View File

@@ -2,6 +2,7 @@
2 2
 
3 3
 int ttail_search_closest(ttail_t* ttail, const struct tm *tm)
4 4
 {
5
+	int ret;
5 6
 	if(ttail->session != NULL)
6 7
 	{
7 8
 		fprintf(stderr, "A session is allready started\n");
@@ -16,9 +17,28 @@ int ttail_search_closest(ttail_t* ttail, const struct tm *tm)
16 17
 		return -1;
17 18
 	}
18 19
 	*/
19
-	return ttail->logfile_sz?\
20
+	ret = ttail->logfile_sz?\
20 21
 		_ttail_search_closest_files(ttail, tm):\
21 22
 		_ttail_search_closest_stdin(ttail, tm);
23
+	if(ret < 0)
24
+	{
25
+		fprintf(stderr, "Error will searching\n");
26
+		return -1;
27
+	}
28
+	if(ret > 0)
29
+	{
30
+		return ret;
31
+	}
32
+	if(ttail->logfile_sz)
33
+	{
34
+		_ttail_search_print_files(ttail, 1);
35
+		_ttail_search_file_free(ttail);
36
+	}
37
+	else
38
+	{
39
+		_ttail_search_print_stdin(ttail, 1);
40
+	}
41
+	return 0;
22 42
 }
23 43
 
24 44
 int _ttail_search_closest_stdin(ttail_t* t, const struct tm* tm)
@@ -26,17 +46,29 @@ int _ttail_search_closest_stdin(ttail_t* t, const struct tm* tm)
26 46
 	return 0;
27 47
 }
28 48
 
49
+void _ttail_search_print_stdin(ttail_t* t, int fd)
50
+{
51
+	return;
52
+}
53
+
29 54
 int ttail_logline2date(ttail_t* ttail, const char* logline, struct tm* tm)
30 55
 {
31 56
 	const char *subst, *ret;
32 57
 
33 58
 	ttail_tm_init(tm);
34
-
35
-	subst = ttail_logline_subst(ttail, logline);
36
-	if(!subst)
59
+	
60
+	if(ttail->flag & TTAIL_FLAG_PREFIX)
37 61
 	{
38
-		memset(tm, 0,sizeof(struct tm));
39
-		return 1;
62
+		subst = ttail_logline_subst(ttail, logline);
63
+		if(!subst)
64
+		{
65
+			memset(tm, 0,sizeof(struct tm));
66
+			return 1;
67
+		}
68
+	}
69
+	else
70
+	{
71
+		subst = logline;
40 72
 	}
41 73
 	ret = strptime(subst, ttail->fmt, tm);
42 74
 	if(!ret)
@@ -53,8 +85,12 @@ const char* ttail_logline_subst(ttail_t* t, const char* logline)
53 85
 	size_t nmatch;
54 86
 	int ret;
55 87
 	char err[1024];
56
-
57
-	if(t->prefix_sz >= 0)
88
+	
89
+	if(!t->prefix_sz)
90
+	{
91
+		return logline;
92
+	}
93
+	else if(t->prefix_sz > 0)
58 94
 	{
59 95
 		/* constant subst */
60 96
 		return strlen(logline) < t->prefix_sz ? \

+ 44
- 6
src/ttail_search_files.c View File

@@ -91,6 +91,48 @@ File sorting not implemented yet\n");
91 91
 	return -1;
92 92
 }
93 93
 
94
+void _ttail_search_print_files(ttail_t* t, int out)
95
+{
96
+	size_t i;
97
+	int fd;
98
+	char buf[8192];
99
+	int r;
100
+
101
+	for(i=t->session->file.id; i<t->logfile_sz; i++)
102
+	{
103
+		if(!t->logfile[i])
104
+		{
105
+			continue;
106
+		}
107
+		fd = fileno(t->logfile[i]);
108
+		if(i==t->session->file.id)
109
+		{
110
+			lseek(fd, t->session->file.off, SEEK_SET);
111
+		} else {
112
+			lseek(fd, 0, SEEK_SET);
113
+		}
114
+		while(1)
115
+		{
116
+			r = read(fd, buf, sizeof(buf));
117
+			if(r == -1)
118
+			{
119
+				perror("unable to read file");
120
+				return;
121
+			}
122
+			else if(r == 0)
123
+			{
124
+				break;
125
+			}
126
+			r = write(out, buf, r);
127
+			if(r == -1 || r == 0)
128
+			{
129
+				perror("Unable to write result");
130
+				return;
131
+			}
132
+		}
133
+	}
134
+}
135
+
94 136
 int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
95 137
 	const struct tm** ftm)
96 138
 {
@@ -104,7 +146,8 @@ int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
104 146
 	valid = 0;
105 147
 	if(ttail_tm_cmp(&(ftm[0][0]), in) > 0)
106 148
 	{
107
-		return 1;
149
+		*off = *id = 0;
150
+		return 0;
108 151
 	}
109 152
 	while(1)
110 153
 	{
@@ -134,11 +177,6 @@ int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
134 177
 		}
135 178
 		else if(cmin > 0)
136 179
 		{
137
-			if(!*id)
138
-			{
139
-				/* not found */
140
-				return 1;
141
-			}
142 180
 			/* found at start of file */
143 181
 			off = 0;
144 182
 			return 0;

Loading…
Cancel
Save