Mark Salyzyn | a073c39 | 2017-04-03 09:30:20 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 24 | #include <log/logcat.h> |
| 25 | |
| 26 | int main(int argc, char** argv, char** envp) { |
| 27 | android_logcat_context ctx = create_android_logcat(); |
| 28 | if (!ctx) return -1; |
| 29 | |
| 30 | signal(SIGPIPE, exit); |
| 31 | |
| 32 | // Save and detect presence of -L or --last flag |
| 33 | std::vector<std::string> args; |
| 34 | bool last = false; |
| 35 | for (int i = 0; i < argc; ++i) { |
| 36 | if (!argv[i]) continue; |
| 37 | args.push_back(std::string(argv[i])); |
| 38 | if (!strcmp(argv[i], "-L") || !strcmp(argv[i], "--last")) last = true; |
| 39 | } |
| 40 | |
| 41 | // Generate argv from saved content |
| 42 | std::vector<const char*> argv_hold; |
| 43 | for (auto& str : args) argv_hold.push_back(str.c_str()); |
| 44 | argv_hold.push_back(nullptr); |
| 45 | |
| 46 | int ret = 0; |
| 47 | if (last) { |
| 48 | // Run logcat command with -L flag |
| 49 | ret = android_logcat_run_command(ctx, -1, -1, argv_hold.size() - 1, |
| 50 | (char* const*)&argv_hold[0], envp); |
| 51 | // Remove -L and --last flags from argument list |
| 52 | for (std::vector<const char*>::iterator it = argv_hold.begin(); |
| 53 | it != argv_hold.end();) { |
| 54 | if (!*it || (strcmp(*it, "-L") && strcmp(*it, "--last"))) { |
| 55 | ++it; |
| 56 | } else { |
| 57 | it = argv_hold.erase(it); |
| 58 | } |
| 59 | } |
| 60 | // fall through to re-run the command regardless of the arguments |
| 61 | // passed in. For instance, we expect -h to report help stutter. |
| 62 | } |
| 63 | |
| 64 | // Run logcat command without -L flag |
| 65 | int retval = android_logcat_run_command(ctx, -1, -1, argv_hold.size() - 1, |
| 66 | (char* const*)&argv_hold[0], envp); |
| 67 | if (!ret) ret = retval; |
| 68 | retval = android_logcat_destroy(&ctx); |
| 69 | if (!ret) ret = retval; |
| 70 | return ret; |
| 71 | } |