logcatd_main.cpp 2.4 KB

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