main.cpp revision 0a3406fc4f8e9a8c8a9155fc7886a0496f692496
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
26using namespace android::base;
27using namespace std;
28
29static void usage(FILE* out) {
30    fprintf(out, "incident_helper is not designed to run manually, see README.md\n");
31    fprintf(out, "usage: incident_helper -s SECTION -i INPUT -o OUTPUT\n");
32    fprintf(out, "REQUIRED:\n");
33    fprintf(out, "  -s           section id, must be positive\n");
34    fprintf(out, "  -i           (default stdin) input fd\n");
35    fprintf(out, "  -o           (default stdout) output fd\n");
36}
37
38//=============================================================================
39static TextParserBase* selectParser(int section) {
40    switch (section) {
41        // IDs smaller than or equal to 0 are reserved for testing
42        case -1:
43            return new TimeoutParser();
44        case 0:
45            return new ReverseParser();
46/* ========================================================================= */
47        // IDs larger than 0 are reserved in incident.proto
48        case 2002:
49            return new KernelWakesParser();
50        default:
51            return NULL;
52    }
53}
54
55//=============================================================================
56int main(int argc, char** argv) {
57    fprintf(stderr, "Start incident_helper...\n");
58
59    // Parse the args
60    int opt;
61    int sectionID = 0;
62    int inputFd = STDIN_FILENO;
63    int outputFd = STDOUT_FILENO;
64    while ((opt = getopt(argc, argv, "hs:i:o:")) != -1) {
65        switch (opt) {
66            case 'h':
67                usage(stdout);
68                return 0;
69            case 's':
70                sectionID = atoi(optarg);
71                break;
72            case 'i':
73                inputFd = atoi(optarg);
74                break;
75            case 'o':
76                outputFd = atoi(optarg);
77                break;
78        }
79    }
80
81    // Check mandatory parameters:
82    if (inputFd < 0) {
83        fprintf(stderr, "invalid input fd: %d\n", inputFd);
84        return 1;
85    }
86    if (outputFd < 0) {
87        fprintf(stderr, "invalid output fd: %d\n", outputFd);
88        return 1;
89    }
90
91    fprintf(stderr, "Pasring section %d...\n", sectionID);
92    TextParserBase* parser = selectParser(sectionID);
93    if (parser != NULL) {
94        fprintf(stderr, "Running parser: %s\n", parser->name.string());
95        status_t err = parser->Parse(inputFd, outputFd);
96        if (err != NO_ERROR) {
97            fprintf(stderr, "Parse error in section %d: %s\n", sectionID, strerror(-err));
98            return -1;
99        }
100
101        delete parser;
102    }
103    fprintf(stderr, "Finish section %d, exiting...\n", sectionID);
104
105    return 0;
106}
107