From 6ec7d2d87764e25d58dcb7ffdcb02b58bcce18d8 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 26 Oct 2017 14:33:02 +0200 Subject: Move error reporting into its own source file This doesn't really seem to belong in save_git.c. Signed-off-by: Dirk Hohndel --- core/CMakeLists.txt | 1 + core/errorhelper.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ core/save-git.c | 36 ------------------------------------ 3 files changed, 47 insertions(+), 36 deletions(-) create mode 100644 core/errorhelper.c (limited to 'core') diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 03a8c4fc4..f063f3aa5 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -40,6 +40,7 @@ set(SUBSURFACE_CORE_LIB_SRCS divesite.cpp divelist.c equipment.c + errorhelper.c file.c gas-model.c git-access.c 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); +} diff --git a/core/save-git.c b/core/save-git.c index 548664239..f0752f549 100644 --- a/core/save-git.c +++ b/core/save-git.c @@ -440,42 +440,6 @@ static void create_dive_buffer(struct dive *dive, struct membuffer *b) save_dive_temperature(b, dive); } -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); -} /* * libgit2 has a "git_treebuilder" concept, but it's broken, and can not -- cgit v1.2.3-70-g09d2 From eccd4b993a7cc47e96ddab486197570c38f21701 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 26 Oct 2017 14:37:29 +0200 Subject: Set error callback helper Signed-off-by: Dirk Hohndel --- core/dive.h | 1 + core/errorhelper.c | 9 +++++++++ 2 files changed, 10 insertions(+) (limited to 'core') diff --git a/core/dive.h b/core/dive.h index 64dee5196..f72aaf1d5 100644 --- a/core/dive.h +++ b/core/dive.h @@ -698,6 +698,7 @@ extern "C" { extern int report_error(const char *fmt, ...); extern void report_message(const char *msg); extern const char *get_error_string(void); +extern void set_error_cb(void(*cb)(void)); 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 c8e6ae956..66c01fd21 100644 --- a/core/errorhelper.c +++ b/core/errorhelper.c @@ -9,6 +9,7 @@ #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 @@ -37,6 +38,10 @@ int report_error(const char *fmt, ...) VA_BUF(buf, fmt); mb_cstring(buf); + /* if an error callback is registered, call it */ + if (error_cb) + error_cb(); + return -1; } @@ -44,3 +49,7 @@ void report_message(const char *msg) { (void)report_error("%s", msg); } + +void set_error_cb(void(*cb)(void)) { + error_cb = cb; +} -- cgit v1.2.3-70-g09d2 From 3b92585a47387d1d7e68d99678fb3956117ce416 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 26 Oct 2017 15:52:45 +0200 Subject: Don't show error if cloud credentials aren't set up This became rather obvious with the change to immediately show errors. The commit also fixes a small memory leak. Signed-off-by: Dirk Hohndel --- core/subsurfacestartup.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'core') diff --git a/core/subsurfacestartup.c b/core/subsurfacestartup.c index c51080cd1..5147b3c37 100644 --- a/core/subsurfacestartup.c +++ b/core/subsurfacestartup.c @@ -163,20 +163,24 @@ void print_files() const char *remote = 0; const char *filename, *local_git; - filename = cloud_url(); - - is_git_repository(filename, &branch, &remote, true); printf("\nFile locations:\n\n"); + if (!same_string(prefs.cloud_storage_email, "") && !same_string(prefs.cloud_storage_password, "")) { + filename = cloud_url(); + + is_git_repository(filename, &branch, &remote, true); + } else { + /* strdup so the free below works in either case */ + filename = strdup("No valid cloud credentials set.\n"); + } if (branch && remote) { local_git = get_local_dir(remote, branch); printf("Local git storage: %s\n", local_git); } else { printf("Unable to get local git directory\n"); } - char *tmp = cloud_url(); - printf("Cloud URL: %s\n", tmp); - free(tmp); - tmp = hashfile_name_string(); + printf("Cloud URL: %s\n", filename); + free((void *)filename); + char *tmp = hashfile_name_string(); printf("Image hashes: %s\n", tmp); free(tmp); tmp = picturedir_string(); -- cgit v1.2.3-70-g09d2 From 405923ecfdca7993a3f6433eba4f0de0d03161f5 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Thu, 26 Oct 2017 15:57:03 +0200 Subject: Desktop UI: notify user if they need to enter a cloud PIN This will need to be merged / cleaned up once the git storage fixes have been merged. Signed-off-by: Dirk Hohndel --- core/cloudstorage.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'core') diff --git a/core/cloudstorage.cpp b/core/cloudstorage.cpp index b6a16f8d2..46888e2b2 100644 --- a/core/cloudstorage.cpp +++ b/core/cloudstorage.cpp @@ -61,6 +61,7 @@ void CloudStorageAuthenticate::uploadFinished() myLastError.clear(); } else if (cloudAuthReply == QLatin1String("[VERIFY]")) { csSettings.setVerificationStatus(CS_NEED_TO_VERIFY); + report_error(qPrintable(tr("Cloud account verification required, enter PIN in preferences"))); } else if (cloudAuthReply == QLatin1String("[PASSWDCHANGED]")) { free(prefs.cloud_storage_password); prefs.cloud_storage_password = prefs.cloud_storage_newpassword; -- cgit v1.2.3-70-g09d2