Apollo 10.0
自动驾驶开放平台
bridge_argument.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <getopt.h>
20#include <libgen.h>
21
22namespace apollo {
23namespace cyber {
24
26 AINFO << "Usage: \n " << binary_name_ << " [OPTION]...\n"
27 << "Description: \n"
28 << " -h, --help: help information \n"
29 << " -c, --cpuprofile: enable gperftools cpu profile\n"
30 << " -o, --profile_filename=filename: the filename to dump the "
31 "profile to, default value is ${binary_name}_cpu.prof. Only work "
32 "with -c option\n"
33 << " -H, --heapprofile: enable gperftools heap profile\n"
34 << " -O, --heapprofile_filename=filename: the filename to dump the "
35 "profile to, default value is ${binary_name}_mem.prof. Only work "
36 "with -H option\n"
37 << "Example:\n"
38 << " " << binary_name_ << " -h\n"
39 << " " << binary_name_ << " -c -H ";
40}
41
42void BridgeArgument::ParseArgument(const int argc, char* const argv[]) {
43 binary_name_ = std::string(basename(argv[0]));
44 GetOptions(argc, argv);
45
46 if (enable_cpuprofile_ && profile_filename_.empty()) {
47 profile_filename_ = binary_name_ + std::string("_cpu.prof");
48 }
49
50 if (enable_heapprofile_ && heapprofile_filename_.empty()) {
51 heapprofile_filename_ = binary_name_ + std::string("_mem.prof");
52 }
53
54 AINFO << "binary_name_ is " << binary_name_;
55}
56
57void BridgeArgument::GetOptions(const int argc, char* const argv[]) {
58 opterr = 0; // extern int opterr
59 int long_index = 0;
60 const std::string short_opts = "hco:HO:";
61 static const struct option long_opts[] = {
62 {"help", no_argument, nullptr, 'h'},
63 {"cpuprofile", no_argument, nullptr, 'c'},
64 {"profile_filename", required_argument, nullptr, 'o'},
65 {"heapprofile", no_argument, nullptr, 'H'},
66 {"heapprofile_filename", required_argument, nullptr, 'O'},
67 {NULL, no_argument, nullptr, 0}};
68
69 // log command for info
70 std::string cmd("");
71 for (int i = 0; i < argc; ++i) {
72 cmd += argv[i];
73 cmd += " ";
74 }
75 AINFO << "command: " << cmd;
76
77 do {
78 int opt =
79 getopt_long(argc, argv, short_opts.c_str(), long_opts, &long_index);
80 if (opt == -1) {
81 break;
82 }
83 switch (opt) {
84 case 'c':
85 enable_cpuprofile_ = true;
86 break;
87 case 'o':
88 profile_filename_ = std::string(optarg);
89 break;
90 case 'H':
91 enable_heapprofile_ = true;
92 break;
93 case 'O':
94 heapprofile_filename_ = std::string(optarg);
95 break;
96 case 'h':
98 exit(0);
99 default:
100 break;
101 }
102 } while (true);
103
104 if (optind < argc) {
105 AINFO << "Found non-option ARGV-element \"" << argv[optind++] << "\"";
106 DisplayUsage();
107 exit(1);
108 }
109}
110
111} // namespace cyber
112} // namespace apollo
void GetOptions(const int argc, char *const argv[])
void ParseArgument(int argc, char *const argv[])
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37