blob: 4da933f582f85f7bb9c054f553b8f97be543f749 [file] [log] [blame]
Mark Salyzyna073c392017-04-03 09:30:20 -07001/*
2 * Copyright (C) 2017 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 <signal.h>
18#include <stdlib.h>
19#include <string.h>
20
21#include <string>
22#include <vector>
23
Elliott Hughes61b580e2018-06-15 15:16:20 -070024#include "logcat.h"
Mark Salyzyna073c392017-04-03 09:30:20 -070025
Tom Cherryd162f142019-10-24 17:35:26 -070026int main(int argc, char** argv) {
Mark Salyzyna073c392017-04-03 09:30:20 -070027 signal(SIGPIPE, exit);
28
29 // Save and detect presence of -L or --last flag
30 std::vector<std::string> args;
31 bool last = false;
32 for (int i = 0; i < argc; ++i) {
33 if (!argv[i]) continue;
34 args.push_back(std::string(argv[i]));
35 if (!strcmp(argv[i], "-L") || !strcmp(argv[i], "--last")) last = true;
36 }
37
38 // Generate argv from saved content
39 std::vector<const char*> argv_hold;
40 for (auto& str : args) argv_hold.push_back(str.c_str());
41 argv_hold.push_back(nullptr);
42
43 int ret = 0;
44 if (last) {
45 // Run logcat command with -L flag
Tom Cherryd162f142019-10-24 17:35:26 -070046 ret = RunLogcat(argv_hold.size() - 1, (char**)&argv_hold[0]);
Mark Salyzyna073c392017-04-03 09:30:20 -070047 // Remove -L and --last flags from argument list
48 for (std::vector<const char*>::iterator it = argv_hold.begin();
49 it != argv_hold.end();) {
50 if (!*it || (strcmp(*it, "-L") && strcmp(*it, "--last"))) {
51 ++it;
52 } else {
53 it = argv_hold.erase(it);
54 }
55 }
56 // fall through to re-run the command regardless of the arguments
57 // passed in. For instance, we expect -h to report help stutter.
58 }
59
60 // Run logcat command without -L flag
Tom Cherryd162f142019-10-24 17:35:26 -070061 int retval = RunLogcat(argv_hold.size() - 1, (char**)&argv_hold[0]);
Mark Salyzyna073c392017-04-03 09:30:20 -070062 if (!ret) ret = retval;
63 return ret;
64}