[PATCH 3/3] show the error string in the GUI rather than stderr

Linus Torvalds torvalds at linux-foundation.org
Fri Mar 14 10:39:36 PDT 2014


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Fri, 14 Mar 2014 10:35:09 -0700
Subject: [PATCH 3/3] show the error string in the GUI rather than stderr

This makes the error string just be an internal "membuffer", which the
GUI can fetch and show when errors occur.  The error string keeps
accumulating until somebody retrieves it with "get_error_string()".

This should make any write errors actually show up to the user.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

This concludes the series, and I currently have nothing planned or 
pending.

 dive.h               |  1 +
 qt-ui/mainwindow.cpp |  8 ++++++--
 save-git.c           | 33 ++++++++++++++++++++++++++-------
 3 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/dive.h b/dive.h
index 5188184b5660..1455bb5dfa91 100644
--- a/dive.h
+++ b/dive.h
@@ -661,6 +661,7 @@ extern "C" {
 #endif
 
 extern int report_error(const char *fmt, ...);
+extern const char *get_error_string(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/qt-ui/mainwindow.cpp b/qt-ui/mainwindow.cpp
index 671f39cf792e..44b02b1253a9 100644
--- a/qt-ui/mainwindow.cpp
+++ b/qt-ui/mainwindow.cpp
@@ -899,8 +899,10 @@ int MainWindow::file_save_as(void)
 	if (ui.InfoWidget->isEditing())
 		ui.InfoWidget->acceptChanges();
 
-	if (save_dives(filename.toUtf8().data()))
+	if (save_dives(filename.toUtf8().data())) {
+		showError(get_error_string());
 		return -1;
+	}
 
 	set_filename(filename.toUtf8().data(), true);
 	setTitle(MWTF_FILENAME);
@@ -927,8 +929,10 @@ int MainWindow::file_save(void)
 		if (!current_def_dir.exists())
 			current_def_dir.mkpath(current_def_dir.absolutePath());
 	}
-	if (save_dives(existing_filename))
+	if (save_dives(existing_filename)) {
+		showError(get_error_string());
 		return -1;
+	}
 	mark_divelist_changed(false);
 	addRecentFile(QStringList() << QString(existing_filename));
 	return 0;
diff --git a/save-git.c b/save-git.c
index ab00ba9519b3..745f26b01e36 100644
--- a/save-git.c
+++ b/save-git.c
@@ -340,16 +340,35 @@ static void create_dive_buffer(struct dive *dive, struct membuffer *b)
 	save_dive_temperature(b, dive);
 }
 
-int report_error(const char *fmt, ...)
+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)
 {
-	struct membuffer b = { 0 };
-	VA_BUF(&b, fmt);
+	const char *str;
 
-	/* We should do some UI element thing describing the failure */
-	put_bytes(&b, "\n", 1);
-	flush_buffer(&b, stderr);
-	free_buffer(&b);
+	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;
 }
 
-- 
1.9.0



More information about the subsurface mailing list