1 /*
2 	bctoolbox
3 	Copyright (C) 2016  Belledonne Communications SARL
4 
5 	This program is free software: you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation, either version 2 of the License, or
8 	(at your option) any later version.
9 
10 	This program is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 #include "bctoolbox/logging.h"
22 #include "bctoolbox_tester.h"
23 
24 static FILE * log_file = NULL;
25 static const char *log_domain = "bctoolbox-tester";
26 
log_handler(int lev,const char * fmt,va_list args)27 static void log_handler(int lev, const char *fmt, va_list args) {
28 #ifdef _WIN32
29 	/* We must use stdio to avoid log formatting (for autocompletion etc.) */
30 	vfprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, fmt, args);
31 	fprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, "\n");
32 #else
33 	va_list cap;
34 	va_copy(cap,args);
35 	vfprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, fmt, cap);
36 	fprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, "\n");
37 	va_end(cap);
38 #endif
39 	if (log_file){
40 		bctbx_logv(log_domain,lev, fmt, args);
41 	}
42 }
43 
44 
bctoolbox_tester_init(void (* ftester_printf)(int level,const char * fmt,va_list args))45 void bctoolbox_tester_init(void(*ftester_printf)(int level, const char *fmt, va_list args)) {
46 	bc_tester_init(log_handler,BCTBX_LOG_ERROR, 0,NULL);
47 	bc_tester_add_suite(&containers_test_suite);
48 	bc_tester_add_suite(&utils_test_suite);
49 	bc_tester_add_suite(&parser_test_suite);
50 }
51 
bctoolbox_tester_uninit(void)52 void bctoolbox_tester_uninit(void) {
53 	bc_tester_uninit();
54 }
55 
bctoolbox_tester_before_each()56 void bctoolbox_tester_before_each() {
57 }
58 
bctoolbox_tester_set_log_file(const char * filename)59 int bctoolbox_tester_set_log_file(const char *filename) {
60 	bctbx_log_handler_t* filehandler;
61 	char* dir;
62 	char* base;
63 	if (log_file) {
64 		fclose(log_file);
65 	}
66 	log_file = fopen(filename, "a");
67 	if (!log_file) {
68 		bctbx_error("Cannot open file [%s] for writing logs because [%s]", filename, strerror(errno));
69 		return -1;
70 	}
71 	dir = bctbx_dirname(filename);
72 	base = bctbx_basename(filename);
73 	bctbx_message("Redirecting traces to file [%s]", filename);
74 	filehandler = bctbx_create_file_log_handler(0, dir, base, log_file);
75 	bctbx_add_log_handler(filehandler);
76 	if (dir) bctbx_free(dir);
77 	if (base) bctbx_free(base);
78 	return 0;
79 }
80 
81 
82 #if !defined(__ANDROID__) && !(defined(BCTBX_WINDOWS_PHONE) || defined(BCTBX_WINDOWS_UNIVERSAL))
83 
84 static const char* bctoolbox_helper =
85 		"\t\t\t--verbose\n"
86 		"\t\t\t--silent\n"
87 		"\t\t\t--log-file <output log file path>\n";
88 
main(int argc,char * argv[])89 int main (int argc, char *argv[]) {
90 	int i;
91 	int ret;
92 
93 	bctbx_init_logger(TRUE);
94 	bctoolbox_tester_init(NULL);
95 
96 	for(i=1;i<argc;++i){
97 		if (strcmp(argv[i],"--verbose")==0){
98 			bctbx_set_log_level(log_domain, BCTBX_LOG_DEBUG);
99 			bctbx_set_log_level(BCTBX_LOG_DOMAIN,BCTBX_LOG_DEBUG);
100 		} else if (strcmp(argv[i],"--silent")==0){
101 			bctbx_set_log_level(log_domain, BCTBX_LOG_FATAL);
102 		} else if (strcmp(argv[i],"--log-file")==0){
103 			CHECK_ARG("--log-file", ++i, argc);
104 			if (bctoolbox_tester_set_log_file(argv[i]) < 0) return -2;
105 		}else {
106 			int ret = bc_tester_parse_args(argc, argv, i);
107 			if (ret>0) {
108 				i += ret - 1;
109 				continue;
110 			} else if (ret<0) {
111 				bc_tester_helper(argv[0], bctoolbox_helper);
112 			}
113 			return ret;
114 		}
115 	}
116 	ret = bc_tester_start(argv[0]);
117 	bctoolbox_tester_uninit();
118 	bctbx_uninit_logger();
119 	return ret;
120 }
121 
122 #endif
123