• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "guard_args_parser.h"
17 
18 #include "utils/logger.h"
19 #include "utils/pandargs.h"
20 
Parse(int argc,const char ** argv)21 bool panda::guard::GuardArgsParser::Parse(int argc, const char **argv)
22 {
23     PandArg help("help", false, "Print this message and exit");
24     PandArg<bool> debug("debug", false,
25                         "enable debug messages (will be printed to standard output if no --debug-file was specified)");
26     PandArg<std::string> debugFile("debug-file", "",
27                                    "(--debug-file FILENAME) set debug file name. default is std::cout");
28     PandArg<std::string> configFilePath("config-file-path", "", "configuration file path");
29 
30     PandArgParser parser;
31     parser.Add(&help);
32     parser.Add(&debug);
33     parser.Add(&debugFile);
34     parser.PushBackTail(&configFilePath);
35     parser.EnableTail();
36     if (!parser.Parse(argc, argv)) {
37         PrintErrorMsg(parser.GetHelpString());
38         parser.DisableTail();
39         return false;
40     }
41 
42     Logger::ComponentMask component_mask;
43     component_mask.set(Logger::Component::PANDAGUARD);
44     component_mask.set(Logger::Component::ABC2PROGRAM);
45     component_mask.set(Logger::Component::ASSEMBLER);
46     if (debug.GetValue()) {
47         debugMode_ = true;
48         if (debugFile.GetValue().empty()) {
49             Logger::InitializeStdLogging(Logger::Level::DEBUG, component_mask);
50         } else {
51             Logger::InitializeFileLogging(debugFile.GetValue(), Logger::Level::DEBUG, component_mask);
52         }
53     } else {
54         Logger::InitializeStdLogging(Logger::Level::ERROR, component_mask);
55     }
56 
57     configFilePath_ = configFilePath.GetValue();
58     if (configFilePath_.empty()) {
59         std::cerr << "The config-file-path value is empty. Please check if the config-file-path is set correctly."
60                   << std::endl;
61         parser.DisableTail();
62         return false;
63     }
64     parser.DisableTail();
65     return true;
66 }
67 
PrintErrorMsg(const std::string & helpInfo)68 void panda::guard::GuardArgsParser::PrintErrorMsg(const std::string &helpInfo)
69 {
70     std::stringstream ss;
71     ss << "Usage:" << std::endl;
72     ss << "panda_guard [options] config-file-path" << std::endl;
73     ss << "Supported options:" << std::endl;
74     ss << helpInfo << std::endl;
75 
76     std::cerr << ss.str();
77 }
78 
GetConfigFilePath() const79 const std::string &panda::guard::GuardArgsParser::GetConfigFilePath() const
80 {
81     return configFilePath_;
82 }
83 
IsDebugMode() const84 bool panda::guard::GuardArgsParser::IsDebugMode() const
85 {
86     return debugMode_;
87 }
88