diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2017-10-26 14:33:02 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2017-10-26 14:37:38 +0200 |
commit | 6ec7d2d87764e25d58dcb7ffdcb02b58bcce18d8 (patch) | |
tree | 4cae2030bb12a0ac4c84fada1e30ce9696150b65 /core/errorhelper.c | |
parent | 2c67b387ea5571675db68c46b7879b4746571fd9 (diff) | |
download | subsurface-6ec7d2d87764e25d58dcb7ffdcb02b58bcce18d8.tar.gz |
Move error reporting into its own source file
This doesn't really seem to belong in save_git.c.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'core/errorhelper.c')
-rw-r--r-- | core/errorhelper.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/core/errorhelper.c b/core/errorhelper.c new file mode 100644 index 000000000..c8e6ae956 --- /dev/null +++ b/core/errorhelper.c @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifdef __clang__ +// Clang has a bug on zero-initialization of C structs. +#pragma clang diagnostic ignored "-Wmissing-field-initializers" +#endif +#include "dive.h" +#include "membuffer.h" + +#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 }; +/* + * 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, ...) +{ + struct membuffer *buf = &error_string_buffer; + + /* Previous unprinted errors? Add a newline in between */ + if (buf->len) + put_bytes(buf, "\n", 1); + VA_BUF(buf, fmt); + mb_cstring(buf); + + return -1; +} + +void report_message(const char *msg) +{ + (void)report_error("%s", msg); +} |