blob: ae3e62928872879ecca2709d4f96ef6029eb3361 [file] [log] [blame]
Yabin Cui67d3abd2015-04-16 15:26:31 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
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
17#include <string.h>
Yabin Cui8f680f62016-03-18 18:47:43 -070018
Yabin Cui67d3abd2015-04-16 15:26:31 -070019#include <string>
20#include <vector>
21
Elliott Hughes66dd09e2015-12-04 14:00:57 -080022#include <android-base/logging.h>
Yabin Cui67d3abd2015-04-16 15:26:31 -070023
24#include "command.h"
Yabin Cui8f680f62016-03-18 18:47:43 -070025#include "utils.h"
Yabin Cui6d2db332015-06-30 18:03:34 -070026
Yabin Cui67d3abd2015-04-16 15:26:31 -070027int main(int argc, char** argv) {
Yabin Cuif560a6f2016-12-14 17:43:26 -080028 android::base::InitLogging(argv, android::base::StderrLogger);
Yabin Cui67d3abd2015-04-16 15:26:31 -070029 std::vector<std::string> args;
Yabin Cui767dd172016-06-02 21:02:43 -070030 android::base::LogSeverity log_severity = android::base::INFO;
Yabin Cui67d3abd2015-04-16 15:26:31 -070031
Yabin Cuib1a885b2016-02-14 19:18:02 -080032 for (int i = 1; i < argc; ++i) {
33 if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
34 args.insert(args.begin(), "help");
35 } else if (strcmp(argv[i], "--log") == 0) {
36 if (i + 1 < argc) {
37 ++i;
Yabin Cui8f680f62016-03-18 18:47:43 -070038 if (!GetLogSeverity(argv[i], &log_severity)) {
Yabin Cuib1a885b2016-02-14 19:18:02 -080039 LOG(ERROR) << "Unknown log severity: " << argv[i];
Yabin Cui6d2db332015-06-30 18:03:34 -070040 return 1;
41 }
Yabin Cui9759e1b2015-04-28 15:54:13 -070042 } else {
Yabin Cuib1a885b2016-02-14 19:18:02 -080043 LOG(ERROR) << "Missing argument for --log option.\n";
44 return 1;
Yabin Cui9759e1b2015-04-28 15:54:13 -070045 }
Yabin Cui6ef55f72016-07-26 18:42:38 -070046 } else if (strcmp(argv[i], "--version") == 0) {
Yabin Cuidf6333c2017-05-03 16:34:02 -070047 LOG(INFO) << "Simpleperf version " << GetSimpleperfVersion();
Yabin Cui6ef55f72016-07-26 18:42:38 -070048 return 0;
Yabin Cuib1a885b2016-02-14 19:18:02 -080049 } else {
50 args.push_back(argv[i]);
Yabin Cui67d3abd2015-04-16 15:26:31 -070051 }
52 }
Yabin Cui6d2db332015-06-30 18:03:34 -070053 android::base::ScopedLogSeverity severity(log_severity);
Yabin Cui67d3abd2015-04-16 15:26:31 -070054
Yabin Cuib1a885b2016-02-14 19:18:02 -080055 if (args.empty()) {
56 args.push_back("help");
57 }
Yabin Cuif79f07e2015-06-01 11:21:37 -070058 std::unique_ptr<Command> command = CreateCommandInstance(args[0]);
Yabin Cui67d3abd2015-04-16 15:26:31 -070059 if (command == nullptr) {
60 LOG(ERROR) << "malformed command line: unknown command " << args[0];
61 return 1;
62 }
Yabin Cui323e9452015-04-20 18:07:17 -070063 std::string command_name = args[0];
Yabin Cuif79f07e2015-06-01 11:21:37 -070064 args.erase(args.begin());
Yabin Cui323e9452015-04-20 18:07:17 -070065
66 LOG(DEBUG) << "command '" << command_name << "' starts running";
Yabin Cui67d3abd2015-04-16 15:26:31 -070067 bool result = command->Run(args);
Yabin Cui323e9452015-04-20 18:07:17 -070068 LOG(DEBUG) << "command '" << command_name << "' "
69 << (result ? "finished successfully" : "failed");
Yabin Cui67d3abd2015-04-16 15:26:31 -070070 return result ? 0 : 1;
71}