Add format-string version of tui_abort and use it to display SQLite errors
This commit is contained in:
parent
1f64677fcf
commit
862c56d91e
2
main.cpp
2
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)
|
||||
{
|
||||
|
|
26
tui.cpp
26
tui.cpp
|
@ -5,6 +5,8 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
termpaint_integration *integration;
|
||||
termpaint_terminal *terminal;
|
||||
termpaint_surface *surface;
|
||||
|
@ -666,3 +668,27 @@ bool tui_handle_action(const Event &event, const std::vector<action> &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<char> 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);
|
||||
}
|
||||
|
|
1
tui.h
1
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, ...);
|
||||
|
|
2
yt.cpp
2
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;
|
||||
|
|
Loading…
Reference in New Issue