summaryrefslogtreecommitdiffstats
path: root/src/CLI/CLI.cpp
blob: d711c7d7b126862d885df18f5800dfed9f44dadd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "ABRTSocket.h"
#include "ABRTException.h"
#include <iostream>

#include <string.h>

#define SOCKET_FILE VAR_RUN"/abrt.socket"

typedef enum {HELP,
              GET_LIST,
              GET_LIST_FULL,
              REPORT,
              REPORT_ALWAYS,
              DELETE} param_mode_t;

typedef struct param_s
{
    param_mode_t m_Mode;
    char* m_sUUID;
} param_t;

void print_usage(char* pProgramName)
{
    std::cout << pProgramName << " [OPTION]" << std::endl << std::endl;
    std::cout << "[OPTION]" << std::endl;
    std::cout << "\t--help                   - prints this text" << std::endl;
    std::cout << "\t--get-list               - prints list of crashes which are not reported" << std::endl;
    std::cout << "\t--get-list-full          - prints list of all crashes" << std::endl;
    std::cout << "\t--report <uuid>          - create and send a report" << std::endl;
    std::cout << "\t--report-always <uuid>   - create and send a report without asking" << std::endl;
    std::cout << "\t--delete <uuid>          - delete crash" << std::endl;
}

void parse_args(int argc, char** argv, param_t& param)
{
    if (argc == 2)
    {
        if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "--version"))
        {
            param.m_Mode = HELP;
        }
        else if (!strcmp(argv[1], "--get-list"))
        {
            param.m_Mode = GET_LIST;
        }
        else if (!strcmp(argv[1], "--get-list-full"))
        {
            param.m_Mode = GET_LIST_FULL;
        }
        else
        {
            param.m_Mode = HELP;
        }
    }
    else if (argc == 3)
    {
        if (!strcmp(argv[1], "--report"))
        {
            param.m_Mode = REPORT;
            param.m_sUUID = argv[2];
        }
        else if (!strcmp(argv[1], "--report-always"))
        {
            param.m_Mode = REPORT_ALWAYS;
            param.m_sUUID = argv[2];
        }
        else if (!strcmp(argv[1], "--delete"))
        {
            param.m_Mode = DELETE;
            param.m_sUUID = argv[2];
        }
        else
        {
            param.m_Mode = HELP;
        }
    }
    else
    {
        param.m_Mode = HELP;
    }
}

void print_crash_infos(const vector_crash_infos_t& pCrashInfos,
                       const param_mode_t& pMode)
{
    unsigned int ii;
    for (ii = 0; ii < pCrashInfos.size(); ii++)
    {
        if (pCrashInfos[ii].find(CD_REPORTED)->second[CD_CONTENT] != "1" || pMode == GET_LIST_FULL)
        {
            std::cout << ii << ". " << std::endl;
            std::cout << "\tUID       : " << pCrashInfos[ii].find(CD_UID)->second[CD_CONTENT] << std::endl;
            std::cout << "\tUUID      : " << pCrashInfos[ii].find(CD_UUID)->second[CD_CONTENT] << std::endl;
            std::cout << "\tPackage   : " << pCrashInfos[ii].find(CD_PACKAGE)->second[CD_CONTENT] << std::endl;
            std::cout << "\tExecutable: " << pCrashInfos[ii].find(CD_EXECUTABLE)->second[CD_CONTENT] << std::endl;
            std::cout << "\tCrash time: " << pCrashInfos[ii].find(CD_TIME)->second[CD_CONTENT] << std::endl;
            std::cout << "\tCrash Rate: " << pCrashInfos[ii].find(CD_COUNT)->second[CD_CONTENT] << std::endl;
        }
    }
}

void print_crash_report(const map_crash_report_t& pCrashReport)
{
    map_crash_report_t::const_iterator it;
    for (it = pCrashReport.begin(); it != pCrashReport.end(); it++)
    {
        if (it->second[CD_TYPE] != CD_SYS)
        {
            std::cout << std::endl << it->first << std::endl;
            std::cout << "-----" << std::endl;
            std::cout << it->second[CD_CONTENT] << std::endl;
        }
    }
}

int main(int argc, char** argv)
{
    CABRTSocket ABRTSocket;
    vector_crash_infos_t ci;
    map_crash_report_t cr;
    param_t param;
    std::string answer = "n";

    parse_args(argc, argv, param);

    if (param.m_Mode == HELP)
    {
        print_usage(argv[0]);
        return 1;
    }

    try
    {
        ABRTSocket.Connect(SOCKET_FILE);

        switch (param.m_Mode)
        {
            case GET_LIST:
                ci = ABRTSocket.GetCrashInfos();
                print_crash_infos(ci, GET_LIST);
                break;
            case GET_LIST_FULL:
                ci = ABRTSocket.GetCrashInfos();
                print_crash_infos(ci, GET_LIST_FULL);
                break;
            case REPORT:
                cr = ABRTSocket.CreateReport(param.m_sUUID);
                print_crash_report(cr);
                std::cout << std::endl << "Do you want to send the report? [y/n]: ";
                std::flush(std::cout);
                std::cin >> answer;
                if (answer == "Y" || answer == "y")
                {
                    ABRTSocket.Report(cr);
                }
                break;
            case REPORT_ALWAYS:
                cr = ABRTSocket.CreateReport(param.m_sUUID);
                ABRTSocket.Report(cr);
                break;
            case DELETE:
                ABRTSocket.DeleteDebugDump(param.m_sUUID);
                break;
            default:
                print_usage(argv[0]);
                break;
        }

        ABRTSocket.DisConnect();
    }
    catch (CABRTException& e)
    {
        std::cout << e.what() << std::endl;
    }

    return 0;
}