Welcome to mirror list, hosted at ThFree Co, Russian Federation.

error.cpp « Internal « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 724ed0fd0ab327b35d775159afd7873e04163b86 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//-----------------------------------------------------------------------------
//           Name: error.cpp
//      Developer: Wolfire Games LLC
//         Author: David Rosen
//    Description: This is a simple wrapper for displaying error messages
//        License: Read below
//-----------------------------------------------------------------------------
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#include "error.h"

#include <Internal/config.h>
#include <Internal/common.h>
#include <Internal/modloading.h>

#include <UserInput/input.h>
#include <Logging/logdata.h>
#include <Graphics/graphics.h>
#include <Threading/thread_sanity.h>
#include <Compat/os_dialogs.h>
#include <Compat/platform.h>
#include <Utility/stacktrace.h>

#include <map>
#include <string>
#include <thread>
#include <chrono>
#include <mutex>

extern Config config;

void FatalError(const char* title, const char* fmt, ...) {
    static const int kBufSize = 1024;
    char err_buf[kBufSize];
    va_list args;
    va_start(args, fmt);
    VFormatString(err_buf, kBufSize, fmt, args);
    va_end(args);
    DisplayError(title, err_buf, _ok);
    LOGF << "Showing fatal message: " << title << "," << err_buf << std::endl;
    LOGF << "Shutting down after fatal error" << std::endl;
    LogSystem::Flush();
    _exit(1);
}

ErrorResponse DisplayFormatError(ErrorType type,
                                 bool allow_repetition,
                                 const char* title,
                                 const char* fmtcontents,
                                 ...) {
    static const int kBufSize = 2048;
    char err_buf[kBufSize];
    va_list args;
    va_start(args, fmtcontents);
    VFormatString(err_buf, kBufSize, fmtcontents, args);
    va_end(args);
    return DisplayError(title, err_buf, type, allow_repetition);
}

struct ErrorDisplay {
    int id;
    std::string title;
    std::string pretext;
    std::string contents;
    ErrorType type;
};

std::mutex error_queue_mutex;
static int error_id_counter = 1;
std::vector<ErrorDisplay> error_queue;
std::map<int, ErrorResponse> error_return;

std::mutex display_last_queued_error_mutex;

ErrorResponse DisplayLastQueuedError() {
    ErrorResponse response = _continue;

    display_last_queued_error_mutex.lock();

    ErrorDisplay ed;
    ed.id = 0;
    ed.type = _ok;
    bool show_error = false;

    error_queue_mutex.lock();
    if (error_queue.size() > 0) {
        ed = error_queue[0];
        error_queue.erase(error_queue.begin());
        show_error = true;
    }

    error_queue_mutex.unlock();

    std::stringstream ss;

    ss << ed.pretext << std::endl
       << ed.contents;

    if (show_error) {
        response = OSDisplayError(
            ed.title.c_str(),
            ss.str().c_str(),
            ed.type);

        switch (response) {
            case _er_exit:
                LOGI.Format("\"Cancel\" chosen, shutting down program.");
                LogSystem::Flush();
                _exit(1);
                break;
            case _retry:
                LOGI.Format("\"Retry\" chosen");
                break;
            case _continue:
                LOGI.Format("\"Continue\" chosen");
                break;
        }
    }

    error_queue_mutex.lock();
    error_return[ed.id] = response;
    error_queue_mutex.unlock();

    display_last_queued_error_mutex.unlock();
    return response;
}

static int PushDisplayError(ErrorDisplay& ed) {
    int id;
    error_queue_mutex.lock();
    id = error_id_counter++;
    ed.id = id;
    error_queue.push_back(ed);
    error_queue_mutex.unlock();
    return id;
}

ErrorResponse WaitResponseForDisplayError(int error_id) {
    ErrorResponse ret;
    bool result = false;
    while (result == false) {
        error_queue_mutex.lock();
        result = (error_return.find(error_id) != error_return.end());
        error_queue_mutex.unlock();
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }

    error_queue_mutex.lock();
    ret = error_return[error_id];
    error_return.erase(error_return.find(error_id));
    error_queue_mutex.unlock();
    return ret;
}

std::mutex display_error_mutex;
std::map<std::string, int> error_message_history;

ErrorResponse DisplayError(const char* title, const char* contents, ErrorType type, bool allow_repetition) {
    display_error_mutex.lock();

    bool no_log = false;
    if (type == _ok_no_log) {
        no_log = true;
        type = _ok;
    }

    if (!no_log) {
        LOGI << "Displaying message: " << title << ", " << contents << std::endl;
        LOGI << GenerateStacktrace() << std::endl;
    }

    if (config["no_dialogues"].toBool()) {
        display_error_mutex.unlock();
        return _continue;
    }

    if (!allow_repetition && error_message_history[contents]) {
        display_error_mutex.unlock();
        return _continue;
    }

    error_message_history[contents]++;

    std::stringstream modlist;

    bool active_mods = false;
    int active_count = 0;
    std::vector<ModInstance*> mods = ModLoading::Instance().GetAllMods();
    for (auto& mod : mods) {
        if (mod->IsActive() && mod->IsCore() == false) {
            if (active_mods == false) {
                modlist << "Following mods are active" << std::endl;
                active_mods = true;
            } else {
                if ((active_count % 5) == 0) {
                    modlist << "," << std::endl;
                } else {
                    modlist << ", ";
                }
            }
            modlist << mod->id;
            active_count++;
        }
    }
    modlist << std::endl
            << "Before reporting, see if disabling mods makes a difference and include this info." << std::endl;

    ErrorDisplay ed;

    ed.title = title;
    if (active_mods) {
        ed.pretext = modlist.str();
    }
    ed.contents = contents;
    ed.type = type;

    int error_id = PushDisplayError(ed);

    ErrorResponse response;

// On windows, showing this dialogue on the non-main window seems fine. (not sure i like it though, might wanna not do this, it's helpful for stacktraces in visual studio however).
#if PLATFORM_WINDOWS
    response = DisplayLastQueuedError();
#else
    if (IsMainThread()) {
        response = DisplayLastQueuedError();
    } else {
        response = WaitResponseForDisplayError(error_id);
    }
#endif

    display_error_mutex.unlock();

    return response;
}

void DisplayFormatMessage(const char* title,
                          const char* fmtcontents,
                          ...) {
    static const int kBufSize = 2048;
    char mess_buf[kBufSize];
    va_list args;
    va_start(args, fmtcontents);
    VFormatString(mess_buf, kBufSize, fmtcontents, args);
    va_end(args);
    return DisplayMessage(title, mess_buf);
}

void DisplayMessage(const char* title,
                    const char* contents) {
    display_error_mutex.lock();

    if (config["no_dialogues"].toBool()) {
        display_error_mutex.unlock();
        return;
    }

    ErrorDisplay ed;

    ed.title = title;
    ed.contents = contents;
    ed.type = _ok;

    int error_id = PushDisplayError(ed);

    ErrorResponse response;

// On windows, showing this dialogue on the non-main window seems fine. (not sure i like it though, might wanna not do this, it's helpful for stacktraces in visual studio however).
#if PLATFORM_WINDOWS
    response = DisplayLastQueuedError();
#else
    if (IsMainThread()) {
        response = DisplayLastQueuedError();
    } else {
        response = WaitResponseForDisplayError(error_id);
    }
#endif

    display_error_mutex.unlock();
}