Browse Source

Initial commit

Yann Weber 7 years ago
parent
commit
9c02dd6800
7 changed files with 222 additions and 236 deletions
  1. 0
    42
      src/dateformats.c
  2. 0
    26
      src/fmt_regex.c
  3. 0
    101
      src/include/dateformats.h
  4. 0
    20
      src/include/fmt_regex.h
  5. 0
    18
      src/include/options.h
  6. 43
    14
      src/include/ttail.h
  7. 179
    15
      src/ttail.c

+ 0
- 42
src/dateformats.c View File

@@ -1,42 +0,0 @@
1
-#include "include/dateformats.h"
2
-
3
-ttail_datefmt** ttail_date_fmt_init(ttail_options *options)
4
-{
5
-	ttail_datefmt **res;
6
-	size_t i;
7
-	
8
-	i = 1;
9
-	#ifdef TTAIL_FMT_REGEX
10
-	i++;
11
-	#endif
12
-
13
-	res = malloc(sizeof(ttail_datefmt*)*i);
14
-	if(!res)
15
-	{
16
-		perror("Fails to allocate formats");
17
-		goto ttail_date_fmt_init_alloc_err;
18
-	}
19
-	i--;
20
-	res[i] = NULL;
21
-	#ifdef TTAIL_FMT_REGEX
22
-	res[i] = ttail_fmt_regex_init(options);
23
-	if(!res[i])
24
-	{
25
-		goto ttail_date_fmt_init_err;
26
-	}
27
-	i--;
28
-	#endif
29
-
30
-	return res;
31
-
32
-	ttail_date_fmt_init_err:
33
-	i++;
34
-	while(res[i]!=NULL)
35
-	{
36
-		res[i]->cleanup(res[i]);
37
-	}
38
-	free(res);
39
-
40
-	ttail_date_fmt_init_alloc_err:
41
-	return NULL;
42
-}

+ 0
- 26
src/fmt_regex.c View File

@@ -1,26 +0,0 @@
1
-#include "include/fmt_regex.h"
2
-
3
-ttail_datefmt* ttail_fmt_regex_init(ttail_options* options)
4
-{
5
-	ttail_datefmt *res;
6
-
7
-	res = malloc(sizeof(ttail_datefmt));
8
-	if(!res)
9
-	{
10
-		perror("Failed to allocate the dateformat");
11
-		goto ttail_fmt_regex_init_malloc_err;
12
-	}
13
-	res->name = malloc(sizeof(char)*(sizeof(TTAIL_FMT_REGEX_NAME)));
14
-	if(!res->name)
15
-	{
16
-		perror("Failed to allocate the dateformat name");
17
-		goto ttail_fmt_regex_init_err;
18
-	}
19
-
20
-	return res;
21
-
22
-	ttail_fmt_regex_init_err:
23
-		free(res);
24
-	ttail_fmt_regex_init_malloc_err:
25
-		return NULL;
26
-}

+ 0
- 101
src/include/dateformats.h View File

@@ -1,101 +0,0 @@
1
-#ifndef _ttail_dateformats_h__
2
-#define _ttail_dateformats_h__
3
-
4
-#include <stdlib.h>
5
-#include <string.h>
6
-#include <stdio.h>
7
-#include <errno.h>
8
-
9
-#include "options.h"
10
-#include "config.h"
11
-
12
-#define TTAIL_NODATE_A 2
13
-#define TTAIL_NODATE_B 4
14
-
15
-
16
-#define TTAIL_FMTNAME_SZ 16
17
-
18
-
19
-typedef struct _ttail_datefmt_s ttail_datefmt;
20
-
21
-//<! Datetime format info
22
-typedef void* ttail_fmt_infos;
23
-
24
-/**@brief Dateformat detection function
25
- *@param const char* logline
26
- *@return Datetime format info or TTAIL_NODETECT if detection fails
27
- */
28
-typedef ttail_fmt_infos (*ttail_detect_f)(const char*);
29
-
30
-/**@brief Comparison results
31
- *
32
- * -1 if a < b, 1 if a > b, 0 if a == b, -2 if exception
33
- *
34
- * If an exception occur, exception flags are set with | on the result.
35
- * Exceptions flags are : TTAIL_NODATE_A and TTAIL_NODATE_B
36
- */
37
-typedef int ttail_cmp_res;
38
-
39
-/**@brief Loglines comparison function
40
- *@param ttail_fmt_infos datetime returned from a ttail_detect_f
41
- *@param const char* logline a
42
- *@param const char* logline b
43
- *@return -1 if a < b, 1 if a > b, 0 if a == b, -2 if exception
44
- *@see ttail_cmp_res
45
- */
46
-typedef ttail_cmp_res (*ttail_cmp_f)(ttail_fmt_infos, const char*, const char*);
47
-
48
-///<! Date detection infos free function
49
-typedef void (*ttail_free_infos_f)(ttail_fmt_infos);
50
-
51
-///<! ttail_datefmt cleanup function
52
-typedef void (*ttail_fmt_cleanup_f)(ttail_datefmt*);
53
-
54
-/**@brief ttail_datefmt initalisation function
55
- *@param ttail_options* options
56
- *@return NULL on error
57
- */
58
-typedef ttail_datefmt* (*ttail_fmt_init_f)(ttail_options*);
59
-
60
-/**@brief Stores a datetime format
61
- *
62
- * Represent a ttail dateformat.
63
- *
64
- * A dateformat provides functions for detection, sorting etc. of a date
65
- * and time in a logline
66
- */
67
-struct _ttail_datefmt_s
68
-{
69
-	///<! Format name
70
-	char *name;
71
-
72
-	///<! format initalisation function
73
-	ttail_fmt_init_f init;
74
-	///<! @brief Detection function
75
-	ttail_detect_f detect;
76
-	///<! @brief Comparison function
77
-	ttail_cmp_f cmp;
78
-	///<! @brief Datetime infos free function
79
-	ttail_free_infos_f date_free_infos;
80
-	///<! cleanup function
81
-	ttail_fmt_cleanup_f cleanup;
82
-
83
-	///<! internal format datas
84
-	void *_blob;
85
-};
86
-
87
-/**@brief Given a name of date format load the shared library and
88
- *build the format list calling the formats init functions
89
- *
90
- *@param const char **fmt_name_l format name list
91
- *@param size_t fmt_sz format name list size
92
- *@param ttail_optionts* options ttail_runtime options
93
- *@return NULL on error else a NULL terminated ttail_datefmt list
94
- */
95
-ttail_datefmt** ttail_date_fmt_init(ttail_options* options);
96
-
97
-#ifdef TTAIL_FMT_REGEX
98
-extern ttail_datefmt* ttail_fmt_regex_init(ttail_options* options);
99
-#endif
100
-
101
-#endif

+ 0
- 20
src/include/fmt_regex.h View File

@@ -1,20 +0,0 @@
1
-#include "config.h"
2
-#ifdef TTAIL_FMT_REGEX
3
-#ifndef _ttail_fmt_regex_h__
4
-#define _ttail_fmt_regex_h__
5
-
6
-#include <stdlib.h>
7
-#include <stdio.h>
8
-#include <errno.h>
9
-#include <regex.h>
10
-#include <sys/types.h>
11
-
12
-#include "dateformats.h"
13
-#include "options.h"
14
-
15
-#define TTAIL_FMT_REGEX_NAME "regex"
16
-
17
-ttail_datefmt* ttail_fmt_regex_init(ttail_options* options);
18
-
19
-#endif
20
-#endif

+ 0
- 18
src/include/options.h View File

@@ -1,18 +0,0 @@
1
-#ifndef _ttail_options_h__
2
-#define _ttail_options_h__
3
-
4
-#include <stdio.h>
5
-
6
-struct _ttail_options_s
7
-{
8
-	///<! NULL terminated format list
9
-	char **fmt_list;
10
-};
11
-
12
-typedef struct _ttail_options_s ttail_options;
13
-
14
-ttail_options *ttail_options_init(int argc, char **argv);
15
-
16
-void ttail_options_free(ttail_options*);
17
-
18
-#endif

+ 43
- 14
src/include/ttail.h View File

@@ -2,28 +2,57 @@
2 2
 #define _ttail_h__
3 3
 
4 4
 #include <errno.h>
5
+#include <unistd.h>
6
+#include <stdlib.h>
5 7
 #include <stdio.h>
8
+#include <string.h>
9
+#include <ctype.h>
10
+#include <getopt.h>
11
+#include <time.h>
12
+#include <regex.h>
13
+#include <sys/types.h>
6 14
 
7
-#include "options.h"
8
-#include "dateformats.h"
9 15
 
10
-struct _ttail_runtime_s
16
+struct _ttail_s
11 17
 {
12
-	///<! Date formats
13
-	ttail_datefmt *fmt_l;
18
+	char **logfile_name; /*!< logfiles name */
19
+	FILE **logfile; /*!< logfiles pointers */
20
+	size_t logfile_sz; /*<! logfiles count */
21
+	
22
+	/*! A regex matching the datetime prefix in loglines */
23
+	regex_t date_prefix;
24
+	int date_prefix_flag;
14 25
 
15
-	///<! Formats count
16
-	size_t fmt_sz;
26
+	/*! A strptime format matching datetime in logfile */
27
+	char *fmt;
17 28
 
18
-	///<! Runtime options
19
-	ttail_options *options;
29
+	int verbose;
20 30
 };
21
-typedef struct _ttail_runtime_s ttail_runtime_t;
31
+typedef struct _ttail_s ttail_t;
22 32
 
23
-/**@brief Init ttail lib
24
- *
25
- *Initialise the TTAIL_date_fmts and set the options
33
+
34
+/**@brief Parse cli arguments and return a ttail_t
35
+ *@param int argc
36
+ *@param char** argv
37
+ *@return NULL on error
38
+ */
39
+ttail_t *ttail_init(int argc, char **argv);
40
+
41
+/**@brief Add a logfile
42
+ *@param ttail_t*
43
+ *@param const char * filename
44
+ *@return 0 if no errors -1 if fatal error
26 45
  */
27
-ttail_runtime_t *ttail_init(int argc, char **argv);
46
+int ttail_add_logfile(ttail_t*, const char*);
47
+
48
+/**@brief Set a date prefix regex
49
+ *@param ttail_t*
50
+ *@param const char * regex
51
+ *@return 0 if no errors
52
+ */
53
+int ttail_set_prefix(ttail_t*, const char*);
54
+
55
+void ttail_free(ttail_t*);
56
+
28 57
 
29 58
 #endif

+ 179
- 15
src/ttail.c View File

@@ -1,29 +1,193 @@
1 1
 #include "include/ttail.h"
2 2
 
3
-ttail_runtime_t* ttail_init(int argc, char **argv)
3
+ttail_t *ttail_init(int argc, char **argv)
4 4
 {
5
-	ttail_runtime_t *res;
6
-	
7
-	res = malloc(sizeof(ttail_runtime_t));
5
+	ttail_t *res;
6
+	int c, opt_i, ret;
7
+
8
+	res = malloc(sizeof(ttail_t));
8 9
 	if(!res)
9 10
 	{
10
-		perror("Unable to allocate memory for runtime");
11
-		goto ttail_init_malloc_err;
11
+		perror("Unable to allocate memory");
12
+		goto ttail_init_alloc_err;
12 13
 	}
13
-	res->options = ttail_options_init(argc, argv);
14
-	if(!res->options)
14
+
15
+	opterr = 0;
16
+	res->verbose = 0;
17
+	res->fmt = NULL;
18
+	regfree(&(res->date_prefix));
19
+	res->date_prefix_flag = 0;
20
+	res->logfile = NULL;
21
+	res->logfile_name = NULL;
22
+	res->logfile_sz = 0;
23
+
24
+	while(1)
15 25
 	{
16
-		goto ttail_init_opt_err;
26
+		static struct option long_options[] =
27
+		{
28
+			{"verbose", no_argument, 0, 'v'},
29
+			{"re-prefix", required_argument, 0, 'p'},
30
+			{"date-format", required_argument, 0, 'd'},
31
+			{"file", required_argument, 0, 'f'},
32
+		};
33
+		opt_i = 0;
34
+		c = getopt_long(argc, argv, "p:f:d:", long_options, &opt_i);
35
+		if(c == -1)
36
+			break;
37
+		
38
+		switch(c)
39
+		{
40
+			case 'v':
41
+				res->verbose++;
42
+				break;
43
+			case 'p':
44
+				if(ttail_set_prefix(res, optarg) < 0)
45
+				{
46
+					goto ttail_init_err;
47
+				}
48
+				break;
49
+			case 'd':
50
+				if(res->fmt)
51
+				{
52
+					fprintf(stderr,"Warning : multiple\
53
+date format given\n");
54
+				}
55
+				res->fmt = malloc(
56
+					sizeof(char)*(strlen(optarg)+1));
57
+				if(!res->fmt)
58
+				{
59
+					perror("Unable to allocate memory\
60
+for date format");
61
+					goto ttail_init_err;
62
+				}
63
+				strcpy(res->fmt, optarg);
64
+
65
+				break;
66
+			case 'f':
67
+				ret = ttail_add_logfile(res, optarg);
68
+				if(ret < 0)
69
+				{
70
+					goto ttail_init_err;
71
+				}
72
+				break;
73
+		}
17 74
 	}
18
-	
19 75
 
20
-	ttail_init_opt_err:
21
-		free(res);
22
-	ttail_init_malloc_err:
23
-		return NULL;
76
+	return res;
77
+
78
+	ttail_init_err:
79
+	ttail_free(res);
80
+	ttail_init_alloc_err:
81
+	return NULL;
24 82
 }
25 83
 
26
-int main(int argc, char **argv)
84
+int ttail_add_logfile(ttail_t* res, const char* filename)
27 85
 {
86
+	void *tmp;
87
+	FILE *fp;
88
+	char *fname;
89
+	res->logfile_sz++;
90
+	tmp = res->logfile;
91
+	res->logfile = realloc(res->logfile,
92
+		sizeof(FILE*)*res->logfile_sz);
93
+	if(!res->logfile)
94
+	{
95
+		perror("Unable to allocate memory for logfiles");
96
+		res->logfile = tmp;
97
+		goto ttail_add_logfile_fpalloc_err;
98
+	}
99
+	fp = fopen(optarg, "r");
100
+	if(!fp)
101
+	{
102
+		fprintf(stderr, "Unable to open file : %s", filename);
103
+	}
104
+	res->logfile[res->logfile_sz-1] = fp;
105
+
106
+	tmp = res->logfile_name;
107
+	res->logfile_name = realloc(res->logfile_name,
108
+		sizeof(char*)*res->logfile_sz);
109
+	if(!res->logfile_name)
110
+	{
111
+		perror("Unable to allocate memory for logfiles");
112
+		res->logfile_name = tmp;
113
+		goto ttail_add_logfile_fnalloc_err;
114
+
115
+	}
116
+	fname = malloc(sizeof(char)*(strlen(filename)+1));
117
+	if(!fname)
118
+	{
119
+		perror("Unable to allocate memory for logfiles");
120
+		goto ttail_add_logfile_fnalloc_err;
121
+	}
122
+	strcpy(fname, filename);
123
+	res->logfile_name[res->logfile_sz-1] = fname;
124
+	
28 125
 	return 0;
126
+
127
+	ttail_add_logfile_fnalloc_err:
128
+	fclose(res->logfile[res->logfile_sz-2]);
129
+	ttail_add_logfile_fpalloc_err:
130
+	res->logfile_sz--;
131
+	return -1;
29 132
 }
133
+
134
+int ttail_set_prefix(ttail_t* res, const char* regex)
135
+{
136
+	int ret;
137
+	char *re_errbuff;
138
+	size_t re_errbuff_sz;
139
+
140
+	if(res->date_prefix_flag)
141
+	{
142
+		fprintf(stderr, "Warning : Regex prefix allready set");
143
+		return 0;
144
+	}
145
+	res->date_prefix_flag = 1;
146
+	ret = regcomp(&res->date_prefix, regex, 0);
147
+	if(!ret)
148
+	{
149
+		return 0;
150
+	}
151
+	/*compilation error */
152
+	re_errbuff_sz = regerror(ret,
153
+		&res->date_prefix, NULL, 0);
154
+	re_errbuff = malloc(
155
+		sizeof(char)*re_errbuff_sz);
156
+	if(!re_errbuff)
157
+	{
158
+		perror("Failed to allocate memory for regex compilation \
159
+error message");
160
+		goto ttail_set_prefix_err;
161
+	}
162
+	regerror(ret, &res->date_prefix,
163
+		re_errbuff,
164
+		sizeof(char)*re_errbuff_sz);
165
+	fprintf(stderr, "Regex compilation fails : %s", re_errbuff);
166
+
167
+	free(re_errbuff);
168
+	ttail_set_prefix_err:
169
+	return -1;
170
+}
171
+
172
+void ttail_free(ttail_t* t)
173
+{
174
+	size_t i;
175
+	for(i=0; i<t->logfile_sz; i++)
176
+	{
177
+		if(t->logfile[i])
178
+		{
179
+			fclose(t->logfile[i]);
180
+		}
181
+		if(t->logfile_name[i])
182
+		{
183
+			free(t->logfile_name[i]);
184
+		}
185
+	}
186
+	t->logfile_sz = 0;
187
+	if(t->fmt != NULL)
188
+	{
189
+		free(t->fmt);
190
+	}
191
+	free(t);
192
+}
193
+

Loading…
Cancel
Save