filter core:
[lttv.git] / ltt / branches / poly / lttv / modules / text / textFilter.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Michel Dagenais
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
19 /*
20 * The text filter facility will prompt for user filter option
21 * and transmit them to the lttv core
22 */
23
24 #include <lttv/lttv.h>
25 #include <lttv/option.h>
26 #include <lttv/module.h>
27 #include <lttv/hook.h>
28 #include <lttv/attribute.h>
29 #include <lttv/iattribute.h>
30 #include <lttv/stats.h>
31 #include <lttv/filter.h>
32 #include <ltt/ltt.h>
33 #include <ltt/event.h>
34 #include <ltt/type.h>
35 #include <ltt/trace.h>
36 #include <ltt/facility.h>
37 #include <stdio.h>
38
39 /* Insert the hooks before and after each trace and tracefile, and for each
40 event. Print a global header. */
41
42 /*
43 * YET TO BE ANSWERED !
44 * - why does this module need dependency with batchAnalysis ?
45 */
46
47 /*
48 * TODO
49 * - specify wich hook function will be used to call the core filter
50 */
51
52 static char
53 *a_file_name = NULL,
54 *a_string = NULL;
55
56 static LttvHooks
57 *before_traceset,
58 *event_hook;
59
60 static FILE *a_file;
61
62 /**
63 * filters the file input from user
64 * @param hook_data the hook data
65 * @return success/failure of operation
66 */
67 void filter_analyze_file(void *hook_data) {
68
69 g_print("textFilter::filter_analyze_file\n");
70
71 LttvAttributeValue value;
72
73 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
74
75 /*
76 * User may specify filtering options through static file
77 * and/or command line string. From these sources, an
78 * option string is rebuilded and sent to the filter core
79 */
80 GString* a_file_content = g_string_new("");
81 a_file = fopen(a_file_name, "r");
82 if(a_file == NULL) {
83 g_warning("file %s does not exist", a_file_name);
84 return;
85 }
86
87 char* line = NULL;
88 size_t len = 0;
89
90 while(!feof(a_file)) {
91 len = getline(&line,&len,a_file);
92 g_string_append(a_file_content,line);
93 }
94 free(line);
95
96 g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression",
97 LTTV_POINTER, &value));
98
99 if(((GString*)*(value.v_pointer))->len != 0) g_string_append_c((GString*)*(value.v_pointer),'&');
100 g_string_append((GString*)*(value.v_pointer),a_file_content->str);
101
102 fclose(a_file);
103 }
104
105 /**
106 * filters the string input from user
107 * @param hook_data the hook data
108 * @return success/failure of operation
109 */
110 void filter_analyze_string(void *hook_data) {
111
112 g_print("textFilter::filter_analyze_string\n");
113
114 LttvAttributeValue value;
115
116 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
117
118 /*
119 * User may specify filtering options through static file
120 * and/or command line string. From these sources, an
121 * option string is rebuilded and sent to the filter core
122 */
123 /* if(a_filter_string==NULL) {
124 a_filter_string = g_string_new("");
125 g_string_append(a_filter_string,a_string);
126 }
127 else {
128 g_string_append(a_filter_string,"&");
129 g_string_append(a_filter_string,a_string);
130 }
131 */
132
133 g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression",
134 LTTV_POINTER, &value));
135
136 if(((GString*)*(value.v_pointer))->len != 0) g_string_append_c((GString*)*(value.v_pointer),'&');
137 g_string_append((GString*)*(value.v_pointer),a_string);
138
139 // LttvFilter* filter = lttv_filter_new();
140 // lttv_filter_append_expression(filter,a_string);
141 }
142
143 /**
144 * initialize the new module
145 */
146 static void init() {
147
148 g_print("textFilter::init()\n"); /* debug */
149
150 LttvAttributeValue value;
151
152 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
153
154 g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression",
155 LTTV_POINTER, &value));
156
157 *(value.v_pointer) = g_string_new("");
158
159 g_info("Init textFilter.c");
160
161 a_string = NULL;
162 lttv_option_add("expression", 'e',
163 "filters a string issued by the user on the command line",
164 "string",
165 LTTV_OPT_STRING, &a_string, filter_analyze_string, NULL);
166 // add function to call for option
167
168 a_file_name = NULL;
169 lttv_option_add("filename", 'f',
170 "browse the filter options contained in specified file",
171 "file name",
172 LTTV_OPT_STRING, &a_file_name, filter_analyze_file, NULL);
173
174 }
175
176 /**
177 * Destroy the current module
178 */
179 static void destroy() {
180 g_info("Destroy textFilter");
181
182 lttv_option_remove("expression");
183
184 lttv_option_remove("filename");
185
186 LttvAttributeValue value;
187
188 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
189
190 g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression",
191 LTTV_POINTER, &value));
192
193 g_string_free((GString*)*(value.v_pointer),TRUE);
194
195 }
196
197
198 LTTV_MODULE("textFilter", "Filters traces", \
199 "Filter the trace following commands issued by user input", \
200 init, destroy, "option")
201
This page took 0.034484 seconds and 5 git commands to generate.