11#include " callgrind_converter.hpp"
22#include " chrome_trace_converter.hpp"
3- #include " tcp_client_stream.hpp"
4- #include < hawktracer/parser/file_stream.hpp>
3+
54#include < hawktracer/parser/protocol_reader.hpp>
65#include < hawktracer/parser/make_unique.hpp>
76
7+ #include " client_utils/command_line_parser.hpp"
8+ #include " client_utils/stream_factory.hpp"
9+
810#include < iostream>
9- #include < cstring>
1011#include < map>
1112
1213using namespace HawkTracer ;
13-
14- bool file_exists (const char * name)
15- {
16- std::ifstream f (name);
17- return f.good ();
18- }
19-
20- bool scan_ip_address (const std::string& address, std::string& out_ip, uint16_t & out_port, uint16_t default_port)
21- {
22- unsigned n1, n2, n3, n4, port;
23- int num = sscanf (address.c_str (), " %u.%u.%u.%u:%u" , &n1, &n2, &n3, &n4, &port);
24-
25- if (num >= 4 )
26- {
27- out_ip = address.substr (0 , address.find (' :' ));
28- out_port = (num == 5 ) ? (uint16_t )port : default_port;
29- return true ;
30- }
31- return false ;
32- }
14+ using ClientUtils::CommandLineParser;
3315
3416std::string create_output_path (const char * path)
3517{
@@ -52,16 +34,6 @@ std::string supported_formats(std::map<std::string, std::unique_ptr<client::Conv
5234 return supported_formats;
5335}
5436
55- void print_help (const char * app_name, std::map<std::string, std::unique_ptr<client::Converter>>& formats)
56- {
57- std::cout << " usage: " << app_name << " [OPTION]..." << std::endl
58- << " --format supported formats are: " << supported_formats (formats) << " (default is chrome-tracing)" << std::endl
59- << " --source data source description (either filename, or server address)" << std::endl
60- << " --output an output Chrome Tracing Json file" << std::endl
61- << " --map comma-separated list of map files" << std::endl
62- << " --help print this text" << std::endl;
63- }
64-
6537void init_supported_formats (std::map<std::string, std::unique_ptr<client::Converter>>& formats)
6638{
6739 formats[" chrome-tracing" ] = parser::make_unique<client::ChromeTraceConverter>();
@@ -70,68 +42,31 @@ void init_supported_formats(std::map<std::string, std::unique_ptr<client::Conver
7042
7143int main (int argc, char ** argv)
7244{
73- std::string format = " chrome-tracing" ;
74- std::string output_path = " hawktracer-trace-%d-%m-%Y-%H_%M_%S.httrace" ;
75- std::string source;
76- std::string map_files;
7745 std::map<std::string, std::unique_ptr<client::Converter>> formats;
7846
7947 init_supported_formats (formats);
8048
81- for (int i = 1 ; i < argc; i++)
49+ CommandLineParser parser (" --" , argv[0 ]);
50+ parser.register_option (" format" , CommandLineParser::OptionInfo (false , false , " Output format. Supported formats: " + supported_formats (formats)));
51+ parser.register_option (" output" , CommandLineParser::OptionInfo (false , false , " Output file" ));
52+ parser.register_option (" source" , CommandLineParser::OptionInfo (false , true , " Data source description (either filename, or server address)" ));
53+ parser.register_option (" map" , CommandLineParser::OptionInfo (false , false , " Comma-separated list of map files" ));
54+ parser.register_option (" help" , CommandLineParser::OptionInfo (true , false , " Print this help" ));
55+
56+ if (!parser.parse (argc, argv) || parser.has_value (" help" ))
8257 {
83- if (strcmp (argv[i], " --format" ) == 0 && i < argc - 1 )
84- {
85- format = argv[++i];
86- }
87- else if (strcmp (argv[i], " --output" ) == 0 && i < argc - 1 )
88- {
89- output_path = argv[++i];
90- }
91- else if (strcmp (argv[i], " --source" ) == 0 && i < argc - 1 )
92- {
93- source = argv[++i];
94- }
95- else if (strcmp (argv[i], " --map" ) == 0 && i < argc - 1 )
96- {
97- map_files = argv[++i];
98- }
99- else
100- {
101- int ret = 0 ;
102- if (strcmp (argv[i], " --help" ) != 0 )
103- {
104- std::cerr << " unrecognized option '" << argv[i] << " '" << std::endl;
105- ret = 1 ;
106- }
107- print_help (argv[0 ], formats);
108- return ret;
109- }
110- }
111-
112- if (source.empty ())
113- {
114- std::cerr << " --source parameter is mandatory" << std::endl;
115- print_help (argv[0 ], formats);
58+ parser.print_help (std::cerr);
11659 return 1 ;
11760 }
11861
119- std::unique_ptr<parser::Stream> stream;
120- std::string ip;
121- uint16_t port;
122- bool is_network = false ;
123- if (file_exists (source.c_str ()))
124- {
125- stream = parser::make_unique<parser::FileStream>(source);
126- }
127- else if (scan_ip_address (source, ip, port, 8765 ))
128- {
129- stream = parser::make_unique<client::TCPClientStream>(ip, port);
130- is_network = true ;
131- }
132- else
62+ std::string format = parser.get_value (" format" , " chrome-tracing" );
63+ std::string output_path = parser.get_value (" output" , " hawktracer-trace-%d-%m-%Y-%H_%M_%S.httrace" );
64+ std::string source = parser.get_value (" source" , " " );
65+ std::string map_files = parser.get_value (" map" , " " );
66+
67+ std::unique_ptr<parser::Stream> stream = ClientUtils::make_stream_from_string (source);
68+ if (!stream)
13369 {
134- std::cerr << " Invalid stream: " << source << std::endl;
13570 return 1 ;
13671 }
13772
@@ -176,7 +111,7 @@ int main(int argc, char** argv)
176111 return 1 ;
177112 }
178113
179- if (is_network )
114+ if (stream-> is_continuous () )
180115 {
181116 std::cout << " Hit [Enter] to finish the trace..." << std::endl;
182117 getchar ();
0 commit comments