--- /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 C6889431E62\r
+ for <notmuch@notmuchmail.org>; Tue, 24 Mar 2015 06:25:14 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 2.438\r
+X-Spam-Level: **\r
+X-Spam-Status: No, score=2.438 tagged_above=-999 required=5\r
+ tests=[DNS_FROM_AHBL_RHSBL=2.438] 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 F+XJB1qPsWwq for <notmuch@notmuchmail.org>;\r
+ Tue, 24 Mar 2015 06:25:11 -0700 (PDT)\r
+Received: from mx.xen14.node3324.gplhost.com (gitolite.debian.net\r
+ [87.98.215.224])\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 26C44431FD2\r
+ for <notmuch@notmuchmail.org>; Tue, 24 Mar 2015 06:25:11 -0700 (PDT)\r
+Received: from remotemail by mx.xen14.node3324.gplhost.com with local (Exim\r
+ 4.80) (envelope-from <bremner@tesseract.cs.unb.ca>)\r
+ id 1YaOon-0000rq-Pd; Tue, 24 Mar 2015 13:24:29 +0000\r
+Received: (nullmailer pid 2634 invoked by uid 1000); Tue, 24 Mar 2015\r
+ 13:24:19 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: David Bremner <david@tethera.net>, notmuch@notmuchmail.org\r
+Subject: Update to library logging, version 5\r
+Date: Tue, 24 Mar 2015 09:24:03 -0400\r
+Message-Id: <1427203451-1540-1-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <1426352554-4383-10-git-send-email-david@tethera.net>\r
+References: <1426352554-4383-10-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: Tue, 24 Mar 2015 13:25:15 -0000\r
+\r
+This takes into account (most of) Tomi's comments and adds a bunch\r
+more tests. We bikeshedded a bit about log_to_string on IRC, and\r
+eventually convered on eliminating it. I guess it might be necessary\r
+to add a compat version of asprintf for some environments (Solaris?)\r
+but this is easy enough using talloc_asprintf and strdup\r
+\r
+Here is the diff between the two versions\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index b5f3549..85054df 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -348,35 +348,6 @@ notmuch_status_to_string (notmuch_status_t status)\r
+ }\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
+ void\r
+ _notmuch_database_log (notmuch_database_t *notmuch,\r
+ const char *format,\r
+@@ -386,7 +357,10 @@ _notmuch_database_log (notmuch_database_t *notmuch,\r
+ \r
+ va_start (va_args, format);\r
+ \r
+- vlog_to_string (notmuch, ¬much->status_string, format, va_args);\r
++ if (notmuch->status_string)\r
++ talloc_free (notmuch->status_string);\r
++\r
++ notmuch->status_string = talloc_vasprintf (notmuch, format, va_args);\r
+ \r
+ va_end (va_args);\r
+ }\r
+@@ -667,22 +641,23 @@ notmuch_database_create_verbose (const char *path,\r
+ int err;\r
+ \r
+ if (path == NULL) {\r
+- log_to_string (&message, "Error: Cannot create a database for a NULL path.\n");\r
++ message = strdup ("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
+- log_to_string (&message, "Error: Cannot create database at %s: %s.\n",\r
+- path, strerror (errno));\r
++ IGNORE_RESULT (asprintf (&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
+- log_to_string (&message, "Error: Cannot create database at %s: Not a directory.\n",\r
+- path);\r
++ IGNORE_RESULT (asprintf (&message, "Error: Cannot create database at %s: "\r
++ "Not a directory.\n",\r
++ path));\r
+ status = NOTMUCH_STATUS_FILE_ERROR;\r
+ goto DONE;\r
+ }\r
+@@ -692,8 +667,8 @@ notmuch_database_create_verbose (const char *path,\r
+ err = mkdir (notmuch_path, 0755);\r
+ \r
+ if (err) {\r
+- log_to_string (&message, "Error: Cannot create directory %s: %s.\n",\r
+- notmuch_path, strerror (errno));\r
++ IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",\r
++ notmuch_path, strerror (errno)));\r
+ status = NOTMUCH_STATUS_FILE_ERROR;\r
+ goto DONE;\r
+ }\r
+@@ -720,7 +695,7 @@ notmuch_database_create_verbose (const char *path,\r
+ talloc_free (notmuch_path);\r
+ \r
+ if (message)\r
+- *status_string = strdup(message);\r
++ *status_string = message;\r
+ if (database)\r
+ *database = notmuch;\r
+ else\r
+@@ -849,27 +824,27 @@ notmuch_database_open_verbose (const char *path,\r
+ static int initialized = 0;\r
+ \r
+ if (path == NULL) {\r
+- log_to_string (&message, "Error: Cannot open a database for a NULL path.\n");\r
++ message = strdup ("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
+- log_to_string (&message, "Out of memory\n");\r
++ message = strdup ("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
+- log_to_string (&message, "Error opening database at %s: %s\n",\r
+- notmuch_path, strerror (errno));\r
++ IGNORE_RESULT (asprintf (&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
+- log_to_string (&message, "Out of memory\n");\r
++ message = strdup ("Out of memory\n");\r
+ status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
+ goto DONE;\r
+ }\r
+@@ -910,11 +885,11 @@ notmuch_database_open_verbose (const char *path,\r
+ * means a dramatically incompatible change. */\r
+ version = notmuch_database_get_version (notmuch);\r
+ if (version > NOTMUCH_DATABASE_VERSION) {\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
+- notmuch_path, version, NOTMUCH_DATABASE_VERSION);\r
++ IGNORE_RESULT (asprintf (&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
++ notmuch_path, version, NOTMUCH_DATABASE_VERSION));\r
+ notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY;\r
+ notmuch_database_destroy (notmuch);\r
+ notmuch = NULL;\r
+@@ -929,11 +904,11 @@ notmuch_database_open_verbose (const char *path,\r
+ version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',\r
+ &incompat_features);\r
+ if (incompat_features) {\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
+- notmuch_path, incompat_features);\r
++ IGNORE_RESULT (asprintf (&message,\r
++ "Error: Notmuch database at %s\n"\r
++ " requires features (%s)\n"\r
++ " not supported by this version of notmuch.\n",\r
++ notmuch_path, incompat_features));\r
+ notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY;\r
+ notmuch_database_destroy (notmuch);\r
+ notmuch = NULL;\r
+@@ -979,8 +954,8 @@ notmuch_database_open_verbose (const char *path,\r
+ notmuch->query_parser->add_prefix (prefix->name, prefix->prefix);\r
+ }\r
+ } catch (const Xapian::Error &error) {\r
+- log_to_string (&message, "A Xapian exception occurred opening database: %s\n",\r
+- error.get_msg().c_str());\r
++ IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",\r
++ error.get_msg().c_str()));\r
+ notmuch_database_destroy (notmuch);\r
+ notmuch = NULL;\r
+ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+@@ -990,7 +965,7 @@ notmuch_database_open_verbose (const char *path,\r
+ talloc_free (local);\r
+ \r
+ if (status_string && message)\r
+- *status_string = strdup (message);\r
++ *status_string = message;\r
+ \r
+ if (database)\r
+ *database = notmuch;\r
+diff --git a/lib/query.cc b/lib/query.cc\r
+index 7b59786..9cedb6a 100644\r
+--- a/lib/query.cc\r
++++ b/lib/query.cc\r
+@@ -296,9 +296,12 @@ notmuch_query_search_messages_st (notmuch_query_t *query,\r
+ return NOTMUCH_STATUS_SUCCESS;\r
+ \r
+ } catch (const Xapian::Error &error) {\r
+- _notmuch_database_log (notmuch, "A Xapian exception occurred performing query: %s\n",\r
+- error.get_msg().c_str());\r
+- _notmuch_database_log (notmuch, "Query string was: %s\n", query->query_string);\r
++ _notmuch_database_log (notmuch,\r
++ "A Xapian exception occurred performing query: %s\n"\r
++ "Query string was: %s\n",\r
++ error.get_msg().c_str(),\r
++ query->query_string);\r
++\r
+ notmuch->exception_reported = TRUE;\r
+ talloc_free (messages);\r
+ return NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+@@ -597,9 +600,12 @@ notmuch_query_count_messages (notmuch_query_t *query)\r
+ count = mset.get_matches_estimated();\r
+ \r
+ } catch (const Xapian::Error &error) {\r
+- _notmuch_database_log (notmuch, "A Xapian exception occurred: %s\n",\r
+- error.get_msg().c_str());\r
+- _notmuch_database_log (notmuch, "Query string was: %s\n", query->query_string);\r
++ _notmuch_database_log (notmuch,\r
++ "A Xapian exception occurred performing query: %s\n"\r
++ "Query string was: %s\n",\r
++ error.get_msg().c_str(),\r
++ query->query_string);\r
++\r
+ }\r
+ \r
+ return count;\r
+diff --git a/test/T560-lib-error.sh b/test/T560-lib-error.sh\r
+index 7621e7f..ec7552a 100755\r
+--- a/test/T560-lib-error.sh\r
++++ b/test/T560-lib-error.sh\r
+@@ -3,6 +3,16 @@ test_description="error reporting for library"\r
+ \r
+ . ./test-lib.sh\r
+ \r
++backup_database (){\r
++ rm -rf notmuch-dir-backup\r
++ cp -a ${MAIL_DIR}/.notmuch notmuch-dir-backup\r
++}\r
++restore_database (){\r
++ rm -rf ${MAIL_DIR}/.notmuch\r
++ cp -a notmuch-dir-backup ${MAIL_DIR}/.notmuch\r
++}\r
++\r
++\r
+ add_email_corpus\r
+ \r
+ test_expect_success "building database" "NOTMUCH_NEW"\r
+@@ -68,6 +78,31 @@ Cannot write to a read-only database.\r
+ EOF\r
+ test_expect_equal_file EXPECTED OUTPUT\r
+ \r
++test_begin_subtest "Add non-existent file"\r
++test_C ${MAIL_DIR} <<EOF\r
++#include <stdio.h>\r
++#include <notmuch.h>\r
++int main (int argc, char** argv)\r
++{\r
++ notmuch_database_t *db;\r
++ notmuch_status_t stat;\r
++ stat = notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db);\r
++ if (stat != NOTMUCH_STATUS_SUCCESS) {\r
++ fprintf (stderr, "error opening database: %d\n", stat);\r
++ }\r
++ stat = notmuch_database_add_message (db, "/nonexistent", NULL);\r
++ if (stat)\r
++ fputs (notmuch_database_status_string (db), stderr);\r
++\r
++}\r
++EOF\r
++cat <<EOF >EXPECTED\r
++== stdout ==\r
++== stderr ==\r
++Error opening /nonexistent: No such file or directory\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
+ test_begin_subtest "compact, overwriting existing backup"\r
+ test_C ${MAIL_DIR} <<EOF\r
+ #include <stdio.h>\r
+@@ -92,4 +127,128 @@ Path already exists: CWD/mail\r
+ EOF\r
+ test_expect_equal_file EXPECTED OUTPUT\r
+ \r
++cat <<EOF > head.c\r
++#include <stdio.h>\r
++#include <sys/types.h>\r
++#include <sys/stat.h>\r
++#include <fcntl.h>\r
++#include <talloc.h>\r
++#include <notmuch.h>\r
++\r
++int main (int argc, char** argv)\r
++{\r
++ notmuch_database_t *db;\r
++ notmuch_status_t stat;\r
++ char *path;\r
++ int fd;\r
++\r
++ stat = notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db);\r
++ if (stat != NOTMUCH_STATUS_SUCCESS) {\r
++ fprintf (stderr, "error opening database: %d\n", stat);\r
++ }\r
++ path = talloc_asprintf (db, "%s/.notmuch/xapian/postlist.DB", argv[1]);\r
++ fd = open(path,O_WRONLY|O_TRUNC);\r
++ if (fd < 0)\r
++ fprintf (stderr, "error opening %s\n");\r
++EOF\r
++cat <<EOF > tail.c\r
++ if (stat) {\r
++ const char *stat_str = notmuch_database_status_string (db);\r
++ if (stat_str)\r
++ fputs (stat_str, stderr);\r
++ }\r
++\r
++}\r
++EOF\r
++\r
++backup_database\r
++test_begin_subtest "Xapian exception finding message"\r
++cat head.c - tail.c <<EOF | test_C ${MAIL_DIR}\r
++ {\r
++ notmuch_message_t *message = NULL;\r
++ stat = notmuch_database_find_message (db, "id:nonexistant", &message);\r
++ }\r
++EOF\r
++sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean\r
++cat <<EOF >EXPECTED\r
++== stdout ==\r
++== stderr ==\r
++A Xapian exception occurred finding message\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT.clean\r
++restore_database\r
++\r
++backup_database\r
++test_begin_subtest "Xapian exception getting tags"\r
++cat head.c - tail.c <<EOF | test_C ${MAIL_DIR}\r
++ {\r
++ notmuch_tags_t *tags = NULL;\r
++ tags = notmuch_database_get_all_tags (db);\r
++ stat = (tags == NULL);\r
++ }\r
++EOF\r
++sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean\r
++cat <<EOF >EXPECTED\r
++== stdout ==\r
++== stderr ==\r
++A Xapian exception occurred getting tags\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT.clean\r
++restore_database\r
++\r
++backup_database\r
++test_begin_subtest "Xapian exception creating directory"\r
++cat head.c - tail.c <<EOF | test_C ${MAIL_DIR}\r
++ {\r
++ notmuch_directory_t *directory = NULL;\r
++ stat = notmuch_database_get_directory (db, "none/existing", &directory);\r
++ }\r
++EOF\r
++sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean\r
++cat <<EOF >EXPECTED\r
++== stdout ==\r
++== stderr ==\r
++A Xapian exception occurred creating a directory\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT.clean\r
++restore_database\r
++\r
++backup_database\r
++test_begin_subtest "Xapian exception searching messages"\r
++cat head.c - tail.c <<EOF | test_C ${MAIL_DIR}\r
++ {\r
++ notmuch_messages_t *messages = NULL;\r
++ notmuch_query_t *query=notmuch_query_create (db, "*");\r
++ stat = notmuch_query_search_messages_st (query, &messages);\r
++ }\r
++EOF\r
++sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean\r
++cat <<EOF >EXPECTED\r
++== stdout ==\r
++== stderr ==\r
++A Xapian exception occurred performing query\r
++Query string was: *\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT.clean\r
++restore_database\r
++\r
++backup_database\r
++test_begin_subtest "Xapian exception counting messages"\r
++cat head.c - tail.c <<EOF | test_C ${MAIL_DIR}\r
++ {\r
++ notmuch_query_t *query=notmuch_query_create (db, "id:87ocn0qh6d.fsf@yoom.home.cworth.org");\r
++ int count = notmuch_query_count_messages (query);\r
++ stat = (count == 0);\r
++ }\r
++EOF\r
++sed 's/^\(A Xapian exception [^:]*\):.*$/\1/' < OUTPUT > OUTPUT.clean\r
++cat <<EOF >EXPECTED\r
++== stdout ==\r
++== stderr ==\r
++A Xapian exception occurred performing query\r
++Query string was: id:87ocn0qh6d.fsf@yoom.home.cworth.org\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT.clean\r
++restore_database\r
++\r
+ test_done\r
+diff --git a/test/test-lib.sh b/test/test-lib.sh\r
+index c7af003..fdb84ea 100644\r
+--- a/test/test-lib.sh\r
++++ b/test/test-lib.sh\r
+@@ -1163,16 +1163,16 @@ test_python() {\r
+ | $cmd -\r
+ }\r
+ \r
+-test_C() {\r
+- test_file="test${test_count}.c"\r
+- exec_file=${test_file%%.c}\r
++test_C () {\r
++ exec_file="test${test_count}"\r
++ test_file="${exec_file}.c"\r
+ cat > ${test_file}\r
+ export LD_LIBRARY_PATH=${TEST_DIRECTORY}/../lib\r
+- ${TEST_CC} ${TEST_CFLAGS} -I${TEST_DIRECTORY}/../lib -o ${exec_file} ${test_file} -L${TEST_DIRECTORY}/../lib/ -lnotmuch\r
++ ${TEST_CC} ${TEST_CFLAGS} -I${TEST_DIRECTORY}/../lib -o ${exec_file} ${test_file} -L${TEST_DIRECTORY}/../lib/ -lnotmuch -ltalloc\r
+ echo "== stdout ==" > OUTPUT.stdout\r
+ echo "== stderr ==" > OUTPUT.stderr\r
+ ./${exec_file} "$@" 1>>OUTPUT.stdout 2>>OUTPUT.stderr\r
+- cat OUTPUT.stdout OUTPUT.stderr | sed "s,$(pwd),CWD," > OUTPUT\r
++ sed "s,$(pwd),CWD," OUTPUT.stdout OUTPUT.stderr > OUTPUT\r
+ }\r
+ \r
+ \r