2 #include <dlogutil-internal.h>
16 int pid_tid_filter_list[] = {
25 * @brief Checks if a log message with a tid is printed as expected
26 * @details Verify the log_should_print_line function returns the expected value
27 * @param[in] line The number of source code line in this file, printed in debug messages
28 * @param[in] p_filter The filter to work with
29 * @param[in] tid The tid to filter with
30 * @param[in] expected The expected result returned by log_should_print_line function
31 * @return 0 if the log_should_print_line returns the expected result, 1 otherwise
33 int check_print_line_tid_single(int line, struct log_filter *p_filter, pthread_t tid, bool expected)
35 struct dlogutil_entry_with_msg entry;
37 entry.msg[0] = '\0'; /* empty tag */
38 entry.msg[1] = '\0'; /* empty msg */
39 entry.header.tag_len = 0;
41 entry.header.tid = tid;
42 entry.header.priority = DLOG_DEBUG; // priority, for unmatched entries that have to face the global filter
43 entry.header.len = sizeof(dlogutil_entry_s) + 2;
45 if (expected != log_should_print_line(p_filter, &entry.header)) {
46 printf("Check in line %d for tid %lu failed to return %d \n", line, tid,
54 * @brief Checks if a log message if filtered properly for a group of tids
55 * @details Verify the log_should_print_line function returns the expected values
56 * @param[in] line The number of line in test, printed in debug messages
57 * @param[in] p_filter The filter to work with
58 * @param[in] tid The tid to filter with
59 * @return Number of tids for which log_should_print_line returns wrong answer
62 int check_print_line_tid(int line, struct log_filter *p_filter, pthread_t tid)
65 for (size_t tid_no = 0; tid_no < NELEMS(pid_tid_filter_list); tid_no++)
66 result += check_print_line_tid_single(line, p_filter, pid_tid_filter_list[tid_no], (pid_tid_filter_list[tid_no] == tid));
71 * @brief Checks if a log message with a pid is printed as expected
72 * @details Verify the log_should_print_line function returns the expected value
73 * @param[in] line The number of source code line in this file, printed in debug messages
74 * @param[in] p_filter The filter to work with
75 * @param[in] pid The pid to filter with
76 * @param[in] expected The expected result returned by log_should_print_line function
77 * @return 0 if the log_should_print_line returns the expected result, 1 otherwise
79 int check_print_line_pid_single(int line, struct log_filter *p_filter, pid_t pid, bool expected)
81 struct dlogutil_entry_with_msg entry;
83 entry.msg[0] = '\0'; /* empty tag */
84 entry.msg[1] = '\0'; /* empty msg */
85 entry.header.tag_len = 0;
87 entry.header.pid = pid;
88 entry.header.priority = DLOG_DEBUG; // priority, for unmatched entries that have to face the global filter
89 entry.header.len = sizeof(dlogutil_entry_s) + 2;
91 if (expected != log_should_print_line(p_filter, &entry.header)) {
92 printf("Check in line %d for pid %d failed to return %d \n", line, pid,
100 * @brief Checks if a log message if filtered properly for a group of pids
101 * @details Verify the log_should_print_line function returns the expected values
102 * @param[in] line The number of line in test, printed in debug messages
103 * @param[in] p_filter The filter to work with
104 * @param[in] pid The pid to filter with
105 * @return Number of pids for which log_should_print_line returns wrong answer
108 int check_print_line_pid(int line, struct log_filter *p_filter, pid_t pid)
111 for (size_t pid_no = 0; pid_no < NELEMS(pid_tid_filter_list); pid_no++)
112 result += check_print_line_pid_single(line, p_filter, pid_tid_filter_list[pid_no], (pid_tid_filter_list[pid_no] == pid));
117 * @brief Checks if a log message with a tag and priority is printed as expected
118 * @details Verify the log_should_print_line function returns the expected value
119 * @param[in] line The number of source code line in this file, printed in debug messages
120 * @param[in] p_filter The filter to work with
121 * @param[in] tag The message's tag
122 * @param[in] priority The message's priority
123 * @param[in] expected The expected result returned by log_should_print_line function
124 * @return 0 if the log_should_print_line returns the expected result, 1 otherwise
126 int check_print_line_tag_prior(int line, struct log_filter *p_filter, char *tag, int priority, bool expected)
128 struct dlogutil_entry_with_msg entry;
130 entry.header.tid = 0;
131 entry.header.pid = 0;
132 entry.header.priority = priority;
133 strncpy(entry.msg, tag, strlen(tag) + 1);
134 entry.msg[strlen(tag) + 1] = '\0'; // second NULL terminator: empty message
135 entry.header.len = sizeof(dlogutil_entry_s) + strlen(tag) + 2;
137 if (expected != log_should_print_line(p_filter, &entry.header)) {
138 printf("Check in line %d for tag %s and priority %c failed to return %d \n", line, tag,
139 filter_pri_to_char(priority), expected);
146 * @brief Checks if a log messages with a tag is printed ONLY for specified priorities
147 * @details Verify the log_should_print_line function returns the expected values
148 * @param[in] line The number of line in test, printed in debug messages
149 * @param[in] p_filter The filter to work with
150 * @param[in] tag The message's tag
151 * @param[in] priority The message's priorities which should be printed
152 * @return Number of priorities for which log_should_print_line returns wrong answer
155 int check_print_line_tag(int line, struct log_filter *p_filter, char *tag, int priors)
158 result += check_print_line_tag_prior(line, p_filter, tag, DLOG_VERBOSE, (priors & verbose) > 0);
159 result += check_print_line_tag_prior(line, p_filter, tag, DLOG_DEBUG, (priors & debug) > 0);
160 result += check_print_line_tag_prior(line, p_filter, tag, DLOG_INFO, (priors & info) > 0);
161 result += check_print_line_tag_prior(line, p_filter, tag, DLOG_WARN, (priors & warn) > 0);
162 result += check_print_line_tag_prior(line, p_filter, tag, DLOG_ERROR, (priors & error) > 0);
163 result += check_print_line_tag_prior(line, p_filter, tag, DLOG_FATAL, (priors & fatal) > 0);
167 int check_colons(void)
169 struct log_filter *const f = log_filter_new();
172 assert(log_add_filter_rule(f, "QWE:::Q:WE::WQE:WQE:W") == TIZEN_ERROR_NONE);
173 assert(log_add_filter_rule(f, "SD:A:DS:D:ASD:SA:D::D") == TIZEN_ERROR_NONE);
174 assert(log_add_filter_rule(f, ":::VXZVCXZVCXV::::::V") == TIZEN_ERROR_NONE);
176 int const result = check_print_line_tag(__LINE__, f, "QWE:::Q:WE::WQE:WQE", warn + error + fatal)
177 + check_print_line_tag(__LINE__, f, "SD:A:DS:D:ASD:SA:D:", debug + info + warn + error + fatal)
178 + check_print_line_tag(__LINE__, f, ":::VXZVCXZVCXV:::::", verbose + debug + info + warn + error + fatal)
181 // No colon at the end, this makes for '\0' priority
182 assert(log_add_filter_rule(f, "FOO:") == TIZEN_ERROR_INVALID_PARAMETER);
193 char *tag, *tag_short, *tag_middle, *tag_long;
194 struct log_filter *p_filter;
195 struct dlogutil_entry_with_msg entry;
197 entry.header.tid = 0;
198 entry.header.pid = 0;
200 p_filter = log_filter_new();
208 // Old tests - rewritten and kept for backward compatibility (before introduction '*' and '=' in filters
210 log_add_filter_rule(p_filter, "*:i"); //set global to info
211 result += check_print_line_tag(__LINE__, p_filter, tag, info | warn | error | fatal);
212 result += check_print_line_tag(__LINE__, p_filter, tag_short, info | warn | error | fatal);
213 result += check_print_line_tag(__LINE__, p_filter, tag_middle, info | warn | error | fatal);
214 result += check_print_line_tag(__LINE__, p_filter, tag_long, info | warn | error | fatal);
216 log_add_filter_rule(p_filter, "*"); //set global to debug
217 result += check_print_line_tag(__LINE__, p_filter, tag, debug | info | warn | error | fatal);
218 result += check_print_line_tag(__LINE__, p_filter, tag_short, debug | info | warn | error | fatal);
220 log_add_filter_rule(p_filter, "*:v"); //set global to verbose
221 result += check_print_line_tag(__LINE__, p_filter, tag, verbose | debug | info | warn | error | fatal);
222 result += check_print_line_tag(__LINE__, p_filter, tag_short, verbose | debug | info | warn | error | fatal);
224 log_add_filter_rule(p_filter, "*:*"); //debug is default global level
225 result += check_print_line_tag(__LINE__, p_filter, tag, debug | info | warn | error | fatal);
226 result += check_print_line_tag(__LINE__, p_filter, tag_short, debug | info | warn | error | fatal);
228 log_add_filter_rule(p_filter, "*:i");
229 result += check_print_line_tag(__LINE__, p_filter, tag, info | warn | error | fatal);
230 result += check_print_line_tag(__LINE__, p_filter, tag_short, info | warn | error | fatal);
232 log_add_filter_rule(p_filter, "random");
233 result += check_print_line_tag(__LINE__, p_filter, tag, verbose | debug | info | warn | error | fatal);
234 result += check_print_line_tag(__LINE__, p_filter, tag_short, info | warn | error | fatal);
236 log_add_filter_rule(p_filter, "random:v");
237 result += check_print_line_tag(__LINE__, p_filter, tag, verbose | debug | info | warn | error | fatal);
238 result += check_print_line_tag(__LINE__, p_filter, tag_short, info | warn | error | fatal);
241 log_add_filter_rule(p_filter, "random:d");
242 result += check_print_line_tag(__LINE__, p_filter, tag, verbose | debug | info | warn | error | fatal);
243 result += check_print_line_tag(__LINE__, p_filter, tag_short, info | warn | error | fatal);
245 log_add_filter_rule(p_filter, "random:w");
246 result += check_print_line_tag(__LINE__, p_filter, tag, verbose | debug | info | warn | error | fatal);
247 result += check_print_line_tag(__LINE__, p_filter, tag_short, info | warn | error | fatal);
250 log_add_filter_rule(p_filter, "crap:*");
252 static const size_t taglen = sizeof "crap" - 1;
254 entry.header.priority = DLOG_VERBOSE;
255 strncpy(entry.msg, "crap", taglen);
256 entry.msg[taglen] = '\0'; /* tag delimiter */
257 entry.msg[taglen + 1] = '\0'; /* empty msg */
258 entry.header.len = sizeof(dlogutil_entry_s) + taglen + 2;
260 assert(log_should_print_line(p_filter, &entry.header));
261 result += check_print_line_tag(__LINE__, p_filter, "crap", verbose | debug | info | warn | error | fatal);
262 result += check_print_line_tag(__LINE__, p_filter, tag, verbose | debug | info | warn | error | fatal);
263 result += check_print_line_tag(__LINE__, p_filter, tag_short, info | warn | error | fatal);
267 err = log_add_filter_rule(p_filter, "random:z");
269 result += check_print_line_tag(__LINE__, p_filter, tag, verbose | debug | info | warn | error | fatal);
270 result += check_print_line_tag(__LINE__, p_filter, tag_short, info | warn | error | fatal);
272 // Adding empty filter string is possible and does not erase existing
273 log_add_filter_rule(p_filter, "issue550946:w");
274 err = log_filter_set_filterspec(p_filter, " ");
276 result += check_print_line_tag(__LINE__, p_filter, "issue550946", warn | error | fatal);
278 // Adding a filter with wrong priority fails also when the tag was used before
279 err = log_filter_set_filterspec(p_filter, "*:s existing:d ");
281 result += check_print_line_tag(__LINE__, p_filter, "existing", debug | info | warn | error | fatal);
282 err = log_filter_set_filterspec(p_filter, "*:s existing:z");
285 log_filter_free(p_filter);
287 // Tests for '=' in filters
289 p_filter = log_filter_new();
291 log_add_filter_rule(p_filter, "pine:=i");
292 result += check_print_line_tag(__LINE__, p_filter, "pine", info);
293 result += check_print_line_tag(__LINE__, p_filter, tag, 0); //default policy for tag (slient) shall be used
295 log_add_filter_rule(p_filter, "pine:=e");
296 result += check_print_line_tag(__LINE__, p_filter, "pine", info | error);
297 result += check_print_line_tag(__LINE__, p_filter, tag, 0);
299 log_add_filter_rule(p_filter, "pine:i");
300 result += check_print_line_tag(__LINE__, p_filter, "pine", info | warn | error | fatal);
301 result += check_print_line_tag(__LINE__, p_filter, tag, 0);
303 log_add_filter_rule(p_filter, "oak:=i");
304 err = log_filter_set_filterspec(p_filter, "alder:=v alder:=f");
306 result += check_print_line_tag(__LINE__, p_filter, "pine", info | warn | error | fatal);
307 result += check_print_line_tag(__LINE__, p_filter, "alder", verbose | fatal);
308 result += check_print_line_tag(__LINE__, p_filter, tag, 0);
310 log_add_filter_rule(p_filter, "*:=v");
311 result += check_print_line_tag(__LINE__, p_filter, "pine", info | warn | error | fatal);
312 result += check_print_line_tag(__LINE__, p_filter, "alder", verbose | fatal);
313 result += check_print_line_tag(__LINE__, p_filter, tag, verbose);
315 log_filter_free(p_filter);
317 // Tests for '*' in the filters
319 p_filter = log_filter_new();
321 log_add_filter_rule(p_filter, "a:=v");
322 log_add_filter_rule(p_filter, "a*:w");
323 result += check_print_line_tag(__LINE__, p_filter, "a", verbose | warn | error | fatal);
324 result += check_print_line_tag(__LINE__, p_filter, "aa", warn | error | fatal);
325 result += check_print_line_tag(__LINE__, p_filter, "aaa", warn | error | fatal);
326 result += check_print_line_tag(__LINE__, p_filter, "aaab", warn | error | fatal);
327 result += check_print_line_tag(__LINE__, p_filter, "aaaaaaaaba", warn | error | fatal);
328 result += check_print_line_tag(__LINE__, p_filter, "b", 0);
329 result += check_print_line_tag(__LINE__, p_filter, "ba", 0);
331 log_add_filter_rule(p_filter, "c?:w");
332 result += check_print_line_tag(__LINE__, p_filter, "c", 0);
333 result += check_print_line_tag(__LINE__, p_filter, "cx", 0);
334 result += check_print_line_tag(__LINE__, p_filter, "c?", warn | error | fatal);
336 log_add_filter_rule(p_filter, "zwxaaa*:e");
337 result += check_print_line_tag(__LINE__, p_filter, "z", 0);
338 result += check_print_line_tag(__LINE__, p_filter, "zx?*", 0);
339 result += check_print_line_tag(__LINE__, p_filter, "zwxaa", 0);
340 result += check_print_line_tag(__LINE__, p_filter, "zwxaab", 0);
341 result += check_print_line_tag(__LINE__, p_filter, "zwxaa?", 0);
342 result += check_print_line_tag(__LINE__, p_filter, "zwxaa*", 0);
343 result += check_print_line_tag(__LINE__, p_filter, "zwxaa?*", 0);
344 result += check_print_line_tag(__LINE__, p_filter, "zwxaaa", error | fatal);
345 result += check_print_line_tag(__LINE__, p_filter, "zwxaaaa", error | fatal);
346 result += check_print_line_tag(__LINE__, p_filter, "zwxaaab", error | fatal);
347 result += check_print_line_tag(__LINE__, p_filter, "zwxaaa*", error | fatal);
349 log_filter_free(p_filter);
351 // Tests for '*' and '=' in filters
353 p_filter = log_filter_new();
354 err = log_filter_set_filterspec(p_filter, "*:=v TORUN:=f TOR*:=d T*:=i torun:s *:i");
356 result += check_print_line_tag(__LINE__, p_filter, "something", info | warn | error | fatal);
357 result += check_print_line_tag(__LINE__, p_filter, "t", info | warn | error | fatal);
358 result += check_print_line_tag(__LINE__, p_filter, "T", info);
359 result += check_print_line_tag(__LINE__, p_filter, "tor", info | warn | error | fatal);
360 result += check_print_line_tag(__LINE__, p_filter, "TOR", debug | info);
361 result += check_print_line_tag(__LINE__, p_filter, "TORUN", debug | info | fatal);
362 result += check_print_line_tag(__LINE__, p_filter, "something", info | warn | error | fatal);
363 result += check_print_line_tag(__LINE__, p_filter, "torun", 0);
364 log_filter_free(p_filter);
368 p_filter = log_filter_new();
369 log_add_filter_rule(p_filter, "*");
370 result += check_print_line_tag(__LINE__, p_filter, tag, debug | info | warn | error | fatal);
371 log_filter_free(p_filter);
373 //No rules - no print lines
375 p_filter = log_filter_new();
376 result += check_print_line_tag(__LINE__, p_filter, tag, 0);
377 err = log_filter_set_filterspec(p_filter, "");
379 result += check_print_line_tag(__LINE__, p_filter, tag, 0);
380 log_filter_free(p_filter);
382 //pid filtering rules
383 p_filter = log_filter_new();
384 result += check_print_line_pid(__LINE__, p_filter, 0);
385 err = log_filter_set_pid(p_filter, 9876);
387 result += check_print_line_pid(__LINE__, p_filter, 9876);
388 err = log_filter_set_pid(p_filter, pid_tid_filter_list[0]);
390 result += check_print_line_pid(__LINE__, p_filter, pid_tid_filter_list[0]);
391 log_filter_free(p_filter);
393 //tid filtering rules
394 p_filter = log_filter_new();
395 result += check_print_line_tid(__LINE__, p_filter, 0);
396 err = log_filter_set_tid(p_filter, 9876);
398 result += check_print_line_tid(__LINE__, p_filter, 9876);
399 err = log_filter_set_tid(p_filter, pid_tid_filter_list[0]);
401 result += check_print_line_tid(__LINE__, p_filter, pid_tid_filter_list[0]);
402 log_filter_free(p_filter);
404 result += check_colons();