Yi Jin | 0a3406f | 2017-06-22 19:23:11 -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 | #define LOG_TAG "incident_helper" |
| 18 | |
| 19 | #include "IncidentHelper.h" |
| 20 | |
| 21 | #include <android-base/file.h> |
| 22 | #include <getopt.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | using namespace android::base; |
| 27 | using namespace std; |
| 28 | |
| 29 | static void usage(FILE* out) { |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 30 | fprintf(out, "incident_helper is not designed to run manually,"); |
| 31 | fprintf(out, "it reads from stdin and writes to stdout, see README.md for details.\n"); |
| 32 | fprintf(out, "usage: incident_helper -s SECTION\n"); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 33 | fprintf(out, "REQUIRED:\n"); |
| 34 | fprintf(out, " -s section id, must be positive\n"); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | //============================================================================= |
| 38 | static TextParserBase* selectParser(int section) { |
| 39 | switch (section) { |
| 40 | // IDs smaller than or equal to 0 are reserved for testing |
| 41 | case -1: |
| 42 | return new TimeoutParser(); |
| 43 | case 0: |
| 44 | return new ReverseParser(); |
| 45 | /* ========================================================================= */ |
| 46 | // IDs larger than 0 are reserved in incident.proto |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 47 | case 2000: |
| 48 | return new ProcrankParser(); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 49 | case 2002: |
| 50 | return new KernelWakesParser(); |
| 51 | default: |
| 52 | return NULL; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | //============================================================================= |
| 57 | int main(int argc, char** argv) { |
| 58 | fprintf(stderr, "Start incident_helper...\n"); |
| 59 | |
| 60 | // Parse the args |
| 61 | int opt; |
| 62 | int sectionID = 0; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 63 | while ((opt = getopt(argc, argv, "hs:")) != -1) { |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 64 | switch (opt) { |
| 65 | case 'h': |
| 66 | usage(stdout); |
| 67 | return 0; |
| 68 | case 's': |
| 69 | sectionID = atoi(optarg); |
| 70 | break; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 74 | fprintf(stderr, "Pasring section %d...\n", sectionID); |
| 75 | TextParserBase* parser = selectParser(sectionID); |
| 76 | if (parser != NULL) { |
| 77 | fprintf(stderr, "Running parser: %s\n", parser->name.string()); |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 78 | status_t err = parser->Parse(STDIN_FILENO, STDOUT_FILENO); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 79 | if (err != NO_ERROR) { |
| 80 | fprintf(stderr, "Parse error in section %d: %s\n", sectionID, strerror(-err)); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | delete parser; |
| 85 | } |
| 86 | fprintf(stderr, "Finish section %d, exiting...\n", sectionID); |
| 87 | |
| 88 | return 0; |
| 89 | } |