--- /dev/null
+Return-Path: <bremner@tesseract.cs.unb.ca>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 8179F429E25\r
+ for <notmuch@notmuchmail.org>; Sat, 27 Dec 2014 14:06:11 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id oop9y-KI+yfa for <notmuch@notmuchmail.org>;\r
+ Sat, 27 Dec 2014 14:06:09 -0800 (PST)\r
+Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155])\r
+ (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 93CCF431FBC\r
+ for <notmuch@notmuchmail.org>; Sat, 27 Dec 2014 14:06:09 -0800 (PST)\r
+Received: from remotemail by yantan.tethera.net with local (Exim 4.80)\r
+ (envelope-from <bremner@tesseract.cs.unb.ca>)\r
+ id 1Y4zUv-0000E8-9f; Sat, 27 Dec 2014 18:06:09 -0400\r
+Received: (nullmailer pid 1473 invoked by uid 1000); Sat, 27 Dec 2014\r
+ 22:05:48 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 1/6] lib: add "verbose" versions of\r
+ notmuch_database_{open,create}\r
+Date: Sat, 27 Dec 2014 23:05:32 +0100\r
+Message-Id: <1419717937-1108-2-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.1.3\r
+In-Reply-To: <1419717937-1108-1-git-send-email-david@tethera.net>\r
+References: <1419717937-1108-1-git-send-email-david@tethera.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 27 Dec 2014 22:06:11 -0000\r
+\r
+This has the the non-trivial side effect silencing all output to\r
+stderror from these two routines.\r
+\r
+The stdargs based infrastucture will be used in following commits for\r
+a more general logging mechanism.\r
+\r
+The changes to notmuch-{new,search} and test/symbol-test are just to\r
+make the test suite pass. The fact that no other changes are needed is\r
+probably a sign of weakness in the test suite.\r
+---\r
+ lib/database.cc | 75 +++++++++++++++++++++++++++++++++++++++++++++--------\r
+ lib/notmuch.h | 21 +++++++++++++++\r
+ notmuch-new.c | 8 ++++--\r
+ notmuch-search.c | 8 ++++--\r
+ test/symbol-test.cc | 6 ++++-\r
+ 5 files changed, 102 insertions(+), 16 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 3601f9d..06e6fc5 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -343,6 +343,35 @@ notmuch_status_to_string (notmuch_status_t status)\r
+ }\r
+ \r
+ static void\r
++vlog_to_string (void *ctx,\r
++ char **status_string,\r
++ const char *format,\r
++ va_list va_args)\r
++{\r
++ if (!status_string)\r
++ return;\r
++\r
++ if (*status_string)\r
++ talloc_free (*status_string);\r
++\r
++ *status_string = talloc_vasprintf (ctx, format, va_args);\r
++}\r
++\r
++static void\r
++log_to_string (char **str,\r
++ const char *format,\r
++ ...)\r
++{\r
++ va_list va_args;\r
++\r
++ va_start (va_args, format);\r
++\r
++ vlog_to_string (NULL, str, format, va_args);\r
++\r
++ va_end (va_args);\r
++}\r
++\r
++static void\r
+ find_doc_ids_for_term (notmuch_database_t *notmuch,\r
+ const char *term,\r
+ Xapian::PostingIterator *begin,\r
+@@ -602,28 +631,37 @@ parse_references (void *ctx,\r
+ notmuch_status_t\r
+ notmuch_database_create (const char *path, notmuch_database_t **database)\r
+ {\r
++ return notmuch_database_create_verbose (path, database, NULL);\r
++}\r
++\r
++notmuch_status_t\r
++notmuch_database_create_verbose (const char *path,\r
++ notmuch_database_t **database,\r
++ char **status_string)\r
++{\r
+ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
+ notmuch_database_t *notmuch = NULL;\r
+ char *notmuch_path = NULL;\r
++ char *message = NULL;\r
+ struct stat st;\r
+ int err;\r
+ \r
+ if (path == NULL) {\r
+- fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");\r
++ log_to_string (&message, "Error: Cannot create a database for a NULL path.\n");\r
+ status = NOTMUCH_STATUS_NULL_POINTER;\r
+ goto DONE;\r
+ }\r
+ \r
+ err = stat (path, &st);\r
+ if (err) {\r
+- fprintf (stderr, "Error: Cannot create database at %s: %s.\n",\r
++ log_to_string (&message, "Error: Cannot create database at %s: %s.\n",\r
+ path, strerror (errno));\r
+ status = NOTMUCH_STATUS_FILE_ERROR;\r
+ goto DONE;\r
+ }\r
+ \r
+ if (! S_ISDIR (st.st_mode)) {\r
+- fprintf (stderr, "Error: Cannot create database at %s: Not a directory.\n",\r
++ log_to_string (&message, "Error: Cannot create database at %s: Not a directory.\n",\r
+ path);\r
+ status = NOTMUCH_STATUS_FILE_ERROR;\r
+ goto DONE;\r
+@@ -634,7 +672,7 @@ notmuch_database_create (const char *path, notmuch_database_t **database)\r
+ err = mkdir (notmuch_path, 0755);\r
+ \r
+ if (err) {\r
+- fprintf (stderr, "Error: Cannot create directory %s: %s.\n",\r
++ log_to_string (&message, "Error: Cannot create directory %s: %s.\n",\r
+ notmuch_path, strerror (errno));\r
+ status = NOTMUCH_STATUS_FILE_ERROR;\r
+ goto DONE;\r
+@@ -660,6 +698,8 @@ notmuch_database_create (const char *path, notmuch_database_t **database)\r
+ if (notmuch_path)\r
+ talloc_free (notmuch_path);\r
+ \r
++ if (message)\r
++ *status_string = strdup(message);\r
+ if (database)\r
+ *database = notmuch;\r
+ else\r
+@@ -760,37 +800,47 @@ notmuch_database_open (const char *path,\r
+ notmuch_database_mode_t mode,\r
+ notmuch_database_t **database)\r
+ {\r
++ return notmuch_database_open_verbose(path, mode, database, NULL);\r
++}\r
++\r
++notmuch_status_t\r
++notmuch_database_open_verbose (const char *path,\r
++ notmuch_database_mode_t mode,\r
++ notmuch_database_t **database,\r
++ char **status_string)\r
++{\r
+ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
+ void *local = talloc_new (NULL);\r
+ notmuch_database_t *notmuch = NULL;\r
+ char *notmuch_path, *xapian_path, *incompat_features;\r
++ char *message = NULL;\r
+ struct stat st;\r
+ int err;\r
+ unsigned int i, version;\r
+ static int initialized = 0;\r
+ \r
+ if (path == NULL) {\r
+- fprintf (stderr, "Error: Cannot open a database for a NULL path.\n");\r
++ log_to_string (&message, "Error: Cannot open a database for a NULL path.\n");\r
+ status = NOTMUCH_STATUS_NULL_POINTER;\r
+ goto DONE;\r
+ }\r
+ \r
+ if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {\r
+- fprintf (stderr, "Out of memory\n");\r
++ log_to_string (&message, "Out of memory\n");\r
+ status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
+ goto DONE;\r
+ }\r
+ \r
+ err = stat (notmuch_path, &st);\r
+ if (err) {\r
+- fprintf (stderr, "Error opening database at %s: %s\n",\r
++ log_to_string (&message, "Error opening database at %s: %s\n",\r
+ notmuch_path, strerror (errno));\r
+ status = NOTMUCH_STATUS_FILE_ERROR;\r
+ goto DONE;\r
+ }\r
+ \r
+ if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {\r
+- fprintf (stderr, "Out of memory\n");\r
++ log_to_string (&message, "Out of memory\n");\r
+ status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
+ goto DONE;\r
+ }\r
+@@ -830,7 +880,7 @@ notmuch_database_open (const char *path,\r
+ * means a dramatically incompatible change. */\r
+ version = notmuch_database_get_version (notmuch);\r
+ if (version > NOTMUCH_DATABASE_VERSION) {\r
+- fprintf (stderr,\r
++ log_to_string (&message,\r
+ "Error: Notmuch database at %s\n"\r
+ " has a newer database format version (%u) than supported by this\n"\r
+ " version of notmuch (%u).\n",\r
+@@ -849,7 +899,7 @@ notmuch_database_open (const char *path,\r
+ version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',\r
+ &incompat_features);\r
+ if (incompat_features) {\r
+- fprintf (stderr,\r
++ log_to_string (&message,\r
+ "Error: Notmuch database at %s\n"\r
+ " requires features (%s)\n"\r
+ " not supported by this version of notmuch.\n",\r
+@@ -899,7 +949,7 @@ notmuch_database_open (const char *path,\r
+ notmuch->query_parser->add_prefix (prefix->name, prefix->prefix);\r
+ }\r
+ } catch (const Xapian::Error &error) {\r
+- fprintf (stderr, "A Xapian exception occurred opening database: %s\n",\r
++ log_to_string (&message, "A Xapian exception occurred opening database: %s\n",\r
+ error.get_msg().c_str());\r
+ notmuch_database_destroy (notmuch);\r
+ notmuch = NULL;\r
+@@ -909,6 +959,9 @@ notmuch_database_open (const char *path,\r
+ DONE:\r
+ talloc_free (local);\r
+ \r
++ if (status_string && message)\r
++ *status_string = strdup (message);\r
++\r
+ if (database)\r
+ *database = notmuch;\r
+ else\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 220839b..0dfac8f 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -230,6 +230,16 @@ notmuch_status_t\r
+ notmuch_database_create (const char *path, notmuch_database_t **database);\r
+ \r
+ /**\r
++ * Like notmuch_database_create, except optionally return an error\r
++ * message. This message is allocated by malloc and should be freed by\r
++ * the caller.\r
++ */\r
++notmuch_status_t\r
++notmuch_database_create_verbose (const char *path,\r
++ notmuch_database_t **database,\r
++ char **error_message);\r
++\r
++/**\r
+ * Database open mode for notmuch_database_open.\r
+ */\r
+ typedef enum {\r
+@@ -279,6 +289,17 @@ notmuch_status_t\r
+ notmuch_database_open (const char *path,\r
+ notmuch_database_mode_t mode,\r
+ notmuch_database_t **database);\r
++/**\r
++ * Like notmuch_database_open, except optionally return an error\r
++ * message. This message is allocated by malloc and should be freed by\r
++ * the caller.\r
++ */\r
++\r
++notmuch_status_t\r
++notmuch_database_open_verbose (const char *path,\r
++ notmuch_database_mode_t mode,\r
++ notmuch_database_t **database,\r
++ char **error_message);\r
+ \r
+ /**\r
+ * Commit changes and close the given notmuch database.\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index ddf42c1..93b70bf 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -985,9 +985,13 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
+ return EXIT_FAILURE;\r
+ add_files_state.total_files = count;\r
+ } else {\r
+- if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,\r
+- ¬much))\r
++ char *status_string = NULL;\r
++ if (notmuch_database_open_verbose (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,\r
++ ¬much, &status_string)) {\r
++ if (status_string) fputs (status_string, stderr);\r
++\r
+ return EXIT_FAILURE;\r
++ }\r
+ \r
+ if (notmuch_database_needs_upgrade (notmuch)) {\r
+ time_t now = time (NULL);\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 14b9f01..250e567 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -545,6 +545,7 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar\r
+ {\r
+ char *query_str;\r
+ unsigned int i;\r
++ char *status_string = NULL;\r
+ \r
+ switch (ctx->format_sel) {\r
+ case NOTMUCH_FORMAT_TEXT:\r
+@@ -570,9 +571,12 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar\r
+ \r
+ notmuch_exit_if_unsupported_format ();\r
+ \r
+- if (notmuch_database_open (notmuch_config_get_database_path (config),\r
+- NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))\r
++ if (notmuch_database_open_verbose (\r
++ notmuch_config_get_database_path (config),\r
++ NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {\r
++ if (status_string) fputs (status_string, stderr);\r
+ return EXIT_FAILURE;\r
++ }\r
+ \r
+ query_str = query_string_from_args (ctx->notmuch, argc, argv);\r
+ if (query_str == NULL) {\r
+diff --git a/test/symbol-test.cc b/test/symbol-test.cc\r
+index 3e96c03..9f8eea7 100644\r
+--- a/test/symbol-test.cc\r
++++ b/test/symbol-test.cc\r
+@@ -5,7 +5,11 @@\r
+ \r
+ int main() {\r
+ notmuch_database_t *notmuch;\r
+- notmuch_database_open("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much);\r
++ char *message = NULL;\r
++\r
++ if (notmuch_database_open_verbose ("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much, &message))\r
++ if (message) fputs (message, stderr);\r
++\r
+ \r
+ try {\r
+ (void) new Xapian::WritableDatabase("./nonexistant", Xapian::DB_OPEN);\r
+-- \r
+2.1.3\r
+\r