Make report_error() reentrant

Remove the global error buffer and pass the error string directly
to the frontend. The frontend is then responsible for accumulating
errors.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-01-26 10:40:04 +01:00 committed by Jan Mulder
parent f8f14c5edb
commit 8f81a22e7f
4 changed files with 17 additions and 34 deletions

View file

@ -719,7 +719,7 @@ extern "C" {
extern int report_error(const char *fmt, ...); extern int report_error(const char *fmt, ...);
extern const char *get_error_string(void); extern const char *get_error_string(void);
extern void set_error_cb(void(*cb)(void)); extern void set_error_cb(void(*cb)(char *)); // Callback takes ownership of passed string
extern struct dive *find_dive_including(timestamp_t when); extern struct dive *find_dive_including(timestamp_t when);
extern bool dive_within_time_range(struct dive *dive, timestamp_t when, timestamp_t offset); extern bool dive_within_time_range(struct dive *dive, timestamp_t when, timestamp_t offset);

View file

@ -8,43 +8,23 @@
#define VA_BUF(b, fmt) do { va_list args; va_start(args, fmt); put_vformat(b, fmt, args); va_end(args); } while (0) #define VA_BUF(b, fmt) do { va_list args; va_start(args, fmt); put_vformat(b, fmt, args); va_end(args); } while (0)
static struct membuffer error_string_buffer = { 0 }; static void (*error_cb)(char *) = NULL;
static void (*error_cb)(void) = NULL;
/*
* Note that the act of "getting" the error string
* buffer doesn't de-allocate the buffer, but it does
* set the buffer length to zero, so that any future
* error reports will overwrite the string rather than
* append to it.
*/
const char *get_error_string(void)
{
const char *str;
if (!error_string_buffer.len)
return "";
str = mb_cstring(&error_string_buffer);
error_string_buffer.len = 0;
return str;
}
int report_error(const char *fmt, ...) int report_error(const char *fmt, ...)
{ {
struct membuffer *buf = &error_string_buffer; struct membuffer buf = { 0 };
/* Previous unprinted errors? Add a newline in between */ /* if there is no error callback registered, don't produce errors */
if (buf->len) if (!error_cb)
put_bytes(buf, "\n", 1); return -1;
VA_BUF(buf, fmt);
mb_cstring(buf);
/* if an error callback is registered, call it */ VA_BUF(&buf, fmt);
if (error_cb) mb_cstring(&buf);
error_cb(); error_cb(detach_buffer(&buf));
return -1; return -1;
} }
void set_error_cb(void(*cb)(void)) { void set_error_cb(void(*cb)(char *)) {
error_cb = cb; error_cb = cb;
} }

View file

@ -89,9 +89,11 @@ extern "C" int updateProgress(const char *text)
MainWindow *MainWindow::m_Instance = NULL; MainWindow *MainWindow::m_Instance = NULL;
extern "C" void showErrorFromC() extern "C" void showErrorFromC(char *buf)
{ {
emit MainWindow::instance()->showError(get_error_string()); QString error(buf);
free(buf);
emit MainWindow::instance()->showError(error);
} }
MainWindow::MainWindow() : QMainWindow(), MainWindow::MainWindow() : QMainWindow(),

View file

@ -36,11 +36,12 @@ QMLManager *QMLManager::m_instance = NULL;
#define NOCLOUD_LOCALSTORAGE format_string("%s/cloudstorage/localrepo[master]", system_default_directory()) #define NOCLOUD_LOCALSTORAGE format_string("%s/cloudstorage/localrepo[master]", system_default_directory())
extern "C" void showErrorFromC() extern "C" void showErrorFromC(char *buf)
{ {
QString error(buf);
free(buf);
// By using invokeMethod with Qt:AutoConnection, the error string is safely // By using invokeMethod with Qt:AutoConnection, the error string is safely
// transported across thread boundaries, if not called from the UI thread. // transported across thread boundaries, if not called from the UI thread.
QString error(get_error_string());
QMetaObject::invokeMethod(QMLManager::instance(), "registerError", Qt::AutoConnection, Q_ARG(QString, error)); QMetaObject::invokeMethod(QMLManager::instance(), "registerError", Qt::AutoConnection, Q_ARG(QString, error));
} }