Vishnu Nair | 33fd3be | 2017-09-27 11:06:37 -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 | |
Vishnu Nair | 3579887 | 2017-10-06 16:00:36 -0700 | [diff] [blame] | 17 | #include "include/serviceutils/PriorityDumper.h" |
Vishnu Nair | 33fd3be | 2017-09-27 11:06:37 -0700 | [diff] [blame] | 18 | |
| 19 | namespace android { |
| 20 | |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 21 | const char16_t PriorityDumper::PROTO_ARG[] = u"--proto"; |
| 22 | const char16_t PriorityDumper::PRIORITY_ARG[] = u"--dump-priority"; |
| 23 | const char16_t PriorityDumper::PRIORITY_ARG_CRITICAL[] = u"CRITICAL"; |
| 24 | const char16_t PriorityDumper::PRIORITY_ARG_HIGH[] = u"HIGH"; |
| 25 | const char16_t PriorityDumper::PRIORITY_ARG_NORMAL[] = u"NORMAL"; |
| 26 | |
| 27 | enum class PriorityType { INVALID, CRITICAL, HIGH, NORMAL }; |
| 28 | |
| 29 | static PriorityType getPriorityType(const String16& arg) { |
| 30 | if (arg == PriorityDumper::PRIORITY_ARG_CRITICAL) { |
| 31 | return PriorityType::CRITICAL; |
| 32 | } else if (arg == PriorityDumper::PRIORITY_ARG_HIGH) { |
| 33 | return PriorityType::HIGH; |
| 34 | } else if (arg == PriorityDumper::PRIORITY_ARG_NORMAL) { |
| 35 | return PriorityType::NORMAL; |
Vishnu Nair | 33fd3be | 2017-09-27 11:06:37 -0700 | [diff] [blame] | 36 | } |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 37 | return PriorityType::INVALID; |
Vishnu Nair | 33fd3be | 2017-09-27 11:06:37 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 40 | status_t PriorityDumper::dumpAll(int fd, const Vector<String16>& args, bool asProto) { |
Vishnu Nair | 3579887 | 2017-10-06 16:00:36 -0700 | [diff] [blame] | 41 | status_t status; |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 42 | status = dumpCritical(fd, args, asProto); |
Vishnu Nair | 3579887 | 2017-10-06 16:00:36 -0700 | [diff] [blame] | 43 | if (status != OK) return status; |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 44 | status = dumpHigh(fd, args, asProto); |
Vishnu Nair | 3579887 | 2017-10-06 16:00:36 -0700 | [diff] [blame] | 45 | if (status != OK) return status; |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 46 | status = dumpNormal(fd, args, asProto); |
Vishnu Nair | 3579887 | 2017-10-06 16:00:36 -0700 | [diff] [blame] | 47 | if (status != OK) return status; |
| 48 | return status; |
| 49 | } |
| 50 | |
| 51 | status_t PriorityDumper::priorityDump(int fd, const Vector<String16>& args) { |
| 52 | status_t status; |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 53 | bool asProto = false; |
| 54 | PriorityType priority = PriorityType::INVALID; |
| 55 | |
| 56 | Vector<String16> strippedArgs; |
| 57 | for (uint32_t argIndex = 0; argIndex < args.size(); argIndex++) { |
| 58 | if (args[argIndex] == PROTO_ARG) { |
| 59 | asProto = true; |
| 60 | } else if (args[argIndex] == PRIORITY_ARG) { |
| 61 | if (argIndex + 1 < args.size()) { |
| 62 | argIndex++; |
| 63 | priority = getPriorityType(args[argIndex]); |
| 64 | } |
Vishnu Nair | 33fd3be | 2017-09-27 11:06:37 -0700 | [diff] [blame] | 65 | } else { |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 66 | strippedArgs.add(args[argIndex]); |
Vishnu Nair | 33fd3be | 2017-09-27 11:06:37 -0700 | [diff] [blame] | 67 | } |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | switch (priority) { |
| 71 | case PriorityType::CRITICAL: |
| 72 | status = dumpCritical(fd, strippedArgs, asProto); |
| 73 | break; |
| 74 | case PriorityType::HIGH: |
| 75 | status = dumpHigh(fd, strippedArgs, asProto); |
| 76 | break; |
| 77 | case PriorityType::NORMAL: |
| 78 | status = dumpNormal(fd, strippedArgs, asProto); |
| 79 | break; |
| 80 | default: |
| 81 | status = dumpAll(fd, strippedArgs, asProto); |
| 82 | break; |
Vishnu Nair | 33fd3be | 2017-09-27 11:06:37 -0700 | [diff] [blame] | 83 | } |
Vishnu Nair | 3579887 | 2017-10-06 16:00:36 -0700 | [diff] [blame] | 84 | return status; |
Vishnu Nair | 33fd3be | 2017-09-27 11:06:37 -0700 | [diff] [blame] | 85 | } |
Vishnu Nair | 3579887 | 2017-10-06 16:00:36 -0700 | [diff] [blame] | 86 | } // namespace android |