2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <android-base/logging.h>
26 static std::map<std::string, android::base::LogSeverity> log_severity_map = {
27 {"debug", android::base::DEBUG},
28 {"warning", android::base::WARNING},
29 {"error", android::base::ERROR},
30 {"fatal", android::base::FATAL},
33 int main(int argc, char** argv) {
34 InitLogging(argv, android::base::StderrLogger);
35 std::vector<std::string> args;
36 android::base::LogSeverity log_severity = android::base::WARNING;
39 args.push_back("help");
41 for (int i = 1; i < argc; ++i) {
42 if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
43 args.insert(args.begin(), "help");
44 } else if (strcmp(argv[i], "--log") == 0) {
47 auto it = log_severity_map.find(argv[i]);
48 if (it != log_severity_map.end()) {
49 log_severity = it->second;
51 LOG(ERROR) << "Unknown log severity: " << argv[i];
55 LOG(ERROR) << "Missing argument for --log option.\n";
59 args.push_back(argv[i]);
63 android::base::ScopedLogSeverity severity(log_severity);
65 std::unique_ptr<Command> command = CreateCommandInstance(args[0]);
66 if (command == nullptr) {
67 LOG(ERROR) << "malformed command line: unknown command " << args[0];
70 std::string command_name = args[0];
71 args.erase(args.begin());
73 LOG(DEBUG) << "command '" << command_name << "' starts running";
74 bool result = command->Run(args);
75 LOG(DEBUG) << "command '" << command_name << "' "
76 << (result ? "finished successfully" : "failed");
77 return result ? 0 : 1;