summaryrefslogtreecommitdiffstats
path: root/lib/Utils/make_descr.cpp
blob: 2a0d4a99dac882960bf99192f1c6309accc5c926 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "abrtlib.h"
#include "CrashTypes.h"
#include "DebugDump.h" /* FILENAME_ARCHITECTURE etc */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if ENABLE_NLS
# include <libintl.h>
# define _(S) gettext(S)
#else
# define _(S) (S)
#endif

using namespace std;

static void add_content(bool &was_multiline, string& description, const char *header, const char *content)
{
    /* We separate multiline contents with emply line */
    if (was_multiline)
        description += '\n';

    while (content[0] == '\n')
        content++;

    if (strchr(content, '\n') == NULL)
    {
        if (skip_whitespace(content)[0] == '\0')
        {
            /* empty, dont report at all */
            return;
        }
        /* one string value, like OS release */
        description += header;
        description += ": ";
        description += content;
        description += '\n';
        was_multiline = 0;
    }
    else
    {
        /* multi-string value, like backtrace */
        if (!was_multiline && description.size() != 0) /* if wasn't yet separated */
            description += '\n'; /* do it now */
        description += header;
        description += "\n-----\n";
        description += content;
        if (content[strlen(content) - 1] != '\n')
            description += '\n';
        was_multiline = 1;
    }
}

string make_description_bz(const map_crash_report_t& pCrashReport)
{
    string description;

    map_crash_report_t::const_iterator it;
    map_crash_report_t::const_iterator end = pCrashReport.end();

    bool was_multiline = 0;
    it = pCrashReport.find(CD_REPRODUCE);
    if (it != end && it->second[CD_CONTENT] != "1.\n2.\n3.\n")
    {
        add_content(was_multiline, description, "How to reproduce", it->second[CD_CONTENT].c_str());
    }

    it = pCrashReport.find(CD_COMMENT);
    if (it != end)
    {
        add_content(was_multiline, description, "Comment", it->second[CD_CONTENT].c_str());
    }

    it = pCrashReport.begin();
    for (; it != end; it++)
    {
        const string &filename = it->first;
        const string &type = it->second[CD_TYPE];
        const string &content = it->second[CD_CONTENT];
        if (type == CD_TXT)
        {
            if (content.size() <= CD_TEXT_ATT_SIZE)
            {
                if (filename != CD_UUID
                 && filename != FILENAME_ARCHITECTURE
                 && filename != FILENAME_RELEASE
                 && filename != CD_REPRODUCE
                 && filename != CD_COMMENT
                ) {
                    add_content(was_multiline, description, filename.c_str(), content.c_str());
                }
            } else {
                add_content(was_multiline, description, "Attached file", filename.c_str());
            }
        }
    }

    return description;
}

string make_description_logger(const map_crash_report_t& pCrashReport)
{
    string description;
    string long_description;

    map_crash_report_t::const_iterator it = pCrashReport.begin();
    for (; it != pCrashReport.end(); it++)
    {
        const string &filename = it->first;
        const string &type = it->second[CD_TYPE];
        const string &content = it->second[CD_CONTENT];
        if (type == CD_TXT
         || type == CD_BIN
        ) {
            if (content == "1.\n2.\n3.\n")
                continue; /* user did not change default "How to reproduce" */

            bool was_multiline = 0;
            string tmp;
            add_content(was_multiline, tmp, filename.c_str(), content.c_str());

            if (was_multiline)
            {
                if (long_description.size() != 0)
                    long_description += '\n';
                long_description += tmp;
            }
            else
            {
                description += tmp;
            }
        }
    }

    if (description.size() != 0 && long_description.size() != 0)
    {
        description += '\n';
        description += long_description;
    }

    return description;
}

/* This needs more work to make the result less ugly */
string make_description_catcut(const map_crash_report_t& pCrashReport)
{
    map_crash_report_t::const_iterator end = pCrashReport.end();
    map_crash_report_t::const_iterator it;

    string howToReproduce;
    it = pCrashReport.find(CD_REPRODUCE);
    if (it != end)
    {
        howToReproduce = "\n\nHow to reproduce\n"
                         "-----\n";
        howToReproduce += it->second[CD_CONTENT];
    }
    string comment;
    it = pCrashReport.find(CD_COMMENT);
    if (it != end)
    {
        comment = "\n\nComment\n"
                 "-----\n";
        comment += it->second[CD_CONTENT];
    }

    string pDescription = "\nabrt "VERSION" detected a crash.\n";
    pDescription += howToReproduce;
    pDescription += comment;
    pDescription += "\n\nAdditional information\n"
                    "======\n";

    for (it = pCrashReport.begin(); it != end; it++)
    {
        const string &filename = it->first;
        const string &type = it->second[CD_TYPE];
        const string &content = it->second[CD_CONTENT];
        if (type == CD_TXT)
        {
            if (content.length() <= CD_TEXT_ATT_SIZE)
            {
                if (filename != CD_UUID
                 && filename != FILENAME_ARCHITECTURE
                 && filename != FILENAME_RELEASE
                 && filename != CD_REPRODUCE
                 && filename != CD_COMMENT
                ) {
                    pDescription += '\n';
                    pDescription += filename;
                    pDescription += "\n-----\n";
                    pDescription += content;
                    pDescription += "\n\n";
                }
            } else {
                pDescription += "\n\nAttached files\n"
                                "----\n";
                pDescription += filename;
                pDescription += '\n';
            }
        }
        else if (type == CD_BIN)
        {
            error_msg(_("Binary file %s will not be reported"), filename.c_str());
        }
    }

    return pDescription;
}