diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-01-26 10:40:04 +0100 |
---|---|---|
committer | Jan Mulder <jlmulder@xs4all.nl> | 2018-01-31 14:47:26 +0100 |
commit | 8f81a22e7f26729cc2f4902ba7db8f696314539f (patch) | |
tree | fada0afb75654a203016e7b24ec6d1677d0622f6 /core | |
parent | f8f14c5edb43aafcc1d4c689af4a91d7274a4b82 (diff) | |
download | subsurface-8f81a22e7f26729cc2f4902ba7db8f696314539f.tar.gz |
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>
Diffstat (limited to 'core')
-rw-r--r-- | core/dive.h | 2 | ||||
-rw-r--r-- | core/errorhelper.c | 38 |
2 files changed, 10 insertions, 30 deletions
diff --git a/core/dive.h b/core/dive.h index c155abbfd..3793cc359 100644 --- a/core/dive.h +++ b/core/dive.h @@ -719,7 +719,7 @@ extern "C" { extern int report_error(const char *fmt, ...); 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 bool dive_within_time_range(struct dive *dive, timestamp_t when, timestamp_t offset); diff --git a/core/errorhelper.c b/core/errorhelper.c index 0f544efb6..5390c4c20 100644 --- a/core/errorhelper.c +++ b/core/errorhelper.c @@ -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) -static struct membuffer error_string_buffer = { 0 }; -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; -} +static void (*error_cb)(char *) = NULL; 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 (buf->len) - put_bytes(buf, "\n", 1); - VA_BUF(buf, fmt); - mb_cstring(buf); + /* if there is no error callback registered, don't produce errors */ + if (!error_cb) + return -1; - /* if an error callback is registered, call it */ - if (error_cb) - error_cb(); + VA_BUF(&buf, fmt); + mb_cstring(&buf); + error_cb(detach_buffer(&buf)); return -1; } -void set_error_cb(void(*cb)(void)) { +void set_error_cb(void(*cb)(char *)) { error_cb = cb; } |