From 862c56d91eb02358dc7eb73572cfbbb5d369c23a Mon Sep 17 00:00:00 2001 From: trilader Date: Sun, 22 Nov 2020 18:11:58 +0100 Subject: [PATCH] Add format-string version of tui_abort and use it to display SQLite errors --- main.cpp | 2 +- tui.cpp | 26 ++++++++++++++++++++++++++ tui.h | 1 + yt.cpp | 2 +- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 74acbfe..f28ecce 100644 --- a/main.cpp +++ b/main.cpp @@ -30,7 +30,7 @@ size_t current_page_count = 0; size_t title_offset = 0; bool any_title_in_next_half = false; -#define SC(x) { const int res = (x); if(res != SQLITE_OK && res != SQLITE_ROW && res != SQLITE_DONE) { fprintf(stderr, "%s failed: (%d) %s\n", #x, res, sqlite3_errstr(res)); std::abort(); }} +#define SC(x) { const int res = (x); if(res != SQLITE_OK && res != SQLITE_ROW && res != SQLITE_DONE) { tui_abort("Database error:\n%s failed: (%d) %s", #x, res, sqlite3_errstr(res)); }} static termpaint_attr* get_attr(const AttributeSetType type, const bool highlight=false) { diff --git a/tui.cpp b/tui.cpp index fc5a72e..cc3402a 100644 --- a/tui.cpp +++ b/tui.cpp @@ -5,6 +5,8 @@ #include #include +#include + termpaint_integration *integration; termpaint_terminal *terminal; termpaint_surface *surface; @@ -666,3 +668,27 @@ bool tui_handle_action(const Event &event, const std::vector &actions) it->func(); return true; } + +void tui_abort(const char *fmt, ...) +{ + va_list ap, ap2; + va_start(ap, fmt); + va_copy(ap2, ap); + int required = vsnprintf(nullptr, 0, fmt, ap); + va_end(ap); + if(required < 0) { + va_end(ap2); + abort(); + } + std::vector buffer(required + 1, 0); + int written = vsnprintf(buffer.data(), required + 1, fmt, ap2); + if(written != required) { + va_end(ap2); + abort(); + } + const std::string message(buffer.data()); + const size_t cols = termpaint_surface_width(surface); + + message_box("Error", simple_wrap(message, cols/2)); + exit(1); +} diff --git a/tui.h b/tui.h index 1fbfeae..b2550e2 100644 --- a/tui.h +++ b/tui.h @@ -84,3 +84,4 @@ extern void update_progress(progress_info *info, const int val, const int maxval extern void end_progress(progress_info *info); extern void tui_abort(std::string message); +extern void tui_abort(const char *fmt, ...); diff --git a/yt.cpp b/yt.cpp index b23a406..67f2078 100644 --- a/yt.cpp +++ b/yt.cpp @@ -6,7 +6,7 @@ #include "tui.h" -#define SC(x) { const int res = (x); if(res != SQLITE_OK && res != SQLITE_ROW && res != SQLITE_DONE) { fprintf(stderr, "%s failed: (%d) %s\n", #x, res, sqlite3_errstr(res)); std::abort(); }} +#define SC(x) { const int res = (x); if(res != SQLITE_OK && res != SQLITE_ROW && res != SQLITE_DONE) { tui_abort("Database error:\n%s failed: (%d) %s", #x, res, sqlite3_errstr(res)); }} using json = nlohmann::json; struct yt_config yt_config;