--- /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 E2634429E3A\r
+ for <notmuch@notmuchmail.org>; Fri, 27 Mar 2015 15:14:52 -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 T97lnoXouX8G for <notmuch@notmuchmail.org>;\r
+ Fri, 27 Mar 2015 15:14:50 -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 F2B3B429E36\r
+ for <notmuch@notmuchmail.org>; Fri, 27 Mar 2015 15:14:49 -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 1YbcW0-0000Fh-SY; Fri, 27 Mar 2015 22:14:08 +0000\r
+Received: (nullmailer pid 1819 invoked by uid 1000); Fri, 27 Mar 2015\r
+ 22:12:15 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v6 7/8] lib: replace almost all fprintfs in library with\r
+ _n_d_log\r
+Date: Fri, 27 Mar 2015 23:11:59 +0100\r
+Message-Id: <1427494320-1483-8-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <1427494320-1483-1-git-send-email-david@tethera.net>\r
+References: <1427494320-1483-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: Fri, 27 Mar 2015 22:14:53 -0000\r
+\r
+This is not supposed to change any functionality from an end user\r
+point of view. Note that it will eliminate some output to stderr. The\r
+query debugging output is left as is; it doesn't really fit with the\r
+current primitive logging model. The remaining "bad" fprintf will need\r
+an internal API change.\r
+---\r
+ lib/database.cc | 38 +++++++++++++++++++++-----------------\r
+ lib/directory.cc | 4 ++--\r
+ lib/index.cc | 11 +++++++----\r
+ lib/message.cc | 6 +++---\r
+ lib/query.cc | 18 ++++++++++++------\r
+ test/T560-lib-error.sh | 6 +++++-\r
+ 6 files changed, 50 insertions(+), 33 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index c114765..e40674a 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -473,7 +473,7 @@ notmuch_database_find_message (notmuch_database_t *notmuch,\r
+ \r
+ return NOTMUCH_STATUS_SUCCESS;\r
+ } catch (const Xapian::Error &error) {\r
+- fprintf (stderr, "A Xapian exception occurred finding message: %s.\n",\r
++ _notmuch_database_log (notmuch, "A Xapian exception occurred finding message: %s.\n",\r
+ error.get_msg().c_str());\r
+ notmuch->exception_reported = TRUE;\r
+ *message_ret = NULL;\r
+@@ -719,7 +719,7 @@ notmuch_status_t\r
+ _notmuch_database_ensure_writable (notmuch_database_t *notmuch)\r
+ {\r
+ if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {\r
+- fprintf (stderr, "Cannot write to a read-only database.\n");\r
++ _notmuch_database_log (notmuch, "Cannot write to a read-only database.\n");\r
+ return NOTMUCH_STATUS_READ_ONLY_DATABASE;\r
+ }\r
+ \r
+@@ -1015,7 +1015,7 @@ notmuch_database_close (notmuch_database_t *notmuch)\r
+ } catch (const Xapian::Error &error) {\r
+ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+ if (! notmuch->exception_reported) {\r
+- fprintf (stderr, "Error: A Xapian exception occurred closing database: %s\n",\r
++ _notmuch_database_log (notmuch, "Error: A Xapian exception occurred closing database: %s\n",\r
+ error.get_msg().c_str());\r
+ }\r
+ }\r
+@@ -1147,12 +1147,12 @@ notmuch_database_compact (const char *path,\r
+ }\r
+ \r
+ if (stat (backup_path, &statbuf) != -1) {\r
+- fprintf (stderr, "Path already exists: %s\n", backup_path);\r
++ _notmuch_database_log (notmuch, "Path already exists: %s\n", backup_path);\r
+ ret = NOTMUCH_STATUS_FILE_ERROR;\r
+ goto DONE;\r
+ }\r
+ if (errno != ENOENT) {\r
+- fprintf (stderr, "Unknown error while stat()ing path: %s\n",\r
++ _notmuch_database_log (notmuch, "Unknown error while stat()ing path: %s\n",\r
+ strerror (errno));\r
+ ret = NOTMUCH_STATUS_FILE_ERROR;\r
+ goto DONE;\r
+@@ -1172,20 +1172,20 @@ notmuch_database_compact (const char *path,\r
+ compactor.set_destdir (compact_xapian_path);\r
+ compactor.compact ();\r
+ } catch (const Xapian::Error &error) {\r
+- fprintf (stderr, "Error while compacting: %s\n", error.get_msg().c_str());\r
++ _notmuch_database_log (notmuch, "Error while compacting: %s\n", error.get_msg().c_str());\r
+ ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+ goto DONE;\r
+ }\r
+ \r
+ if (rename (xapian_path, backup_path)) {\r
+- fprintf (stderr, "Error moving %s to %s: %s\n",\r
++ _notmuch_database_log (notmuch, "Error moving %s to %s: %s\n",\r
+ xapian_path, backup_path, strerror (errno));\r
+ ret = NOTMUCH_STATUS_FILE_ERROR;\r
+ goto DONE;\r
+ }\r
+ \r
+ if (rename (compact_xapian_path, xapian_path)) {\r
+- fprintf (stderr, "Error moving %s to %s: %s\n",\r
++ _notmuch_database_log (notmuch, "Error moving %s to %s: %s\n",\r
+ compact_xapian_path, xapian_path, strerror (errno));\r
+ ret = NOTMUCH_STATUS_FILE_ERROR;\r
+ goto DONE;\r
+@@ -1193,7 +1193,7 @@ notmuch_database_compact (const char *path,\r
+ \r
+ if (! keep_backup) {\r
+ if (rmtree (backup_path)) {\r
+- fprintf (stderr, "Error removing old database %s: %s\n",\r
++ _notmuch_database_log (notmuch, "Error removing old database %s: %s\n",\r
+ backup_path, strerror (errno));\r
+ ret = NOTMUCH_STATUS_FILE_ERROR;\r
+ goto DONE;\r
+@@ -1204,6 +1204,10 @@ notmuch_database_compact (const char *path,\r
+ if (notmuch) {\r
+ notmuch_status_t ret2;\r
+ \r
++ const char *str = notmuch_database_status_string (notmuch);\r
++ if (status_cb && str)\r
++ status_cb (str, closure);\r
++\r
+ ret2 = notmuch_database_destroy (notmuch);\r
+ \r
+ /* don't clobber previous error status */\r
+@@ -1222,7 +1226,7 @@ notmuch_database_compact (unused (const char *path),\r
+ unused (notmuch_compact_status_cb_t status_cb),\r
+ unused (void *closure))\r
+ {\r
+- fprintf (stderr, "notmuch was compiled against a xapian version lacking compaction support.\n");\r
++ _notmuch_database_log (notmuch, "notmuch was compiled against a xapian version lacking compaction support.\n");\r
+ return NOTMUCH_STATUS_UNSUPPORTED_OPERATION;\r
+ }\r
+ #endif\r
+@@ -1500,7 +1504,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
+ }\r
+ \r
+ if (private_status) {\r
+- fprintf (stderr,\r
++ _notmuch_database_log (notmuch,\r
+ "Upgrade failed while creating ghost messages.\n");\r
+ status = COERCE_STATUS (private_status, "Unexpected status from _notmuch_message_initialize_ghost");\r
+ goto DONE;\r
+@@ -1550,7 +1554,7 @@ notmuch_database_begin_atomic (notmuch_database_t *notmuch)\r
+ try {\r
+ (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->begin_transaction (false);\r
+ } catch (const Xapian::Error &error) {\r
+- fprintf (stderr, "A Xapian exception occurred beginning transaction: %s.\n",\r
++ _notmuch_database_log (notmuch, "A Xapian exception occurred beginning transaction: %s.\n",\r
+ error.get_msg().c_str());\r
+ notmuch->exception_reported = TRUE;\r
+ return NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+@@ -1584,7 +1588,7 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch)\r
+ if (thresh && atoi (thresh) == 1)\r
+ db->flush ();\r
+ } catch (const Xapian::Error &error) {\r
+- fprintf (stderr, "A Xapian exception occurred committing transaction: %s.\n",\r
++ _notmuch_database_log (notmuch, "A Xapian exception occurred committing transaction: %s.\n",\r
+ error.get_msg().c_str());\r
+ notmuch->exception_reported = TRUE;\r
+ return NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+@@ -1830,7 +1834,7 @@ notmuch_database_get_directory (notmuch_database_t *notmuch,\r
+ *directory = _notmuch_directory_create (notmuch, path,\r
+ NOTMUCH_FIND_LOOKUP, &status);\r
+ } catch (const Xapian::Error &error) {\r
+- fprintf (stderr, "A Xapian exception occurred getting directory: %s.\n",\r
++ _notmuch_database_log (notmuch, "A Xapian exception occurred getting directory: %s.\n",\r
+ error.get_msg().c_str());\r
+ notmuch->exception_reported = TRUE;\r
+ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+@@ -2412,7 +2416,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,\r
+ \r
+ _notmuch_message_sync (message);\r
+ } catch (const Xapian::Error &error) {\r
+- fprintf (stderr, "A Xapian exception occurred adding message: %s.\n",\r
++ _notmuch_database_log (notmuch, "A Xapian exception occurred adding message: %s.\n",\r
+ error.get_msg().c_str());\r
+ notmuch->exception_reported = TRUE;\r
+ ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+@@ -2504,7 +2508,7 @@ notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,\r
+ status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
+ }\r
+ } catch (const Xapian::Error &error) {\r
+- fprintf (stderr, "Error: A Xapian exception occurred finding message by filename: %s\n",\r
++ _notmuch_database_log (notmuch, "Error: A Xapian exception occurred finding message by filename: %s\n",\r
+ error.get_msg().c_str());\r
+ notmuch->exception_reported = TRUE;\r
+ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
+@@ -2557,7 +2561,7 @@ notmuch_database_get_all_tags (notmuch_database_t *db)\r
+ _notmuch_string_list_sort (tags);\r
+ return _notmuch_tags_create (db, tags);\r
+ } catch (const Xapian::Error &error) {\r
+- fprintf (stderr, "A Xapian exception occurred getting tags: %s.\n",\r
++ _notmuch_database_log (db, "A Xapian exception occurred getting tags: %s.\n",\r
+ error.get_msg().c_str());\r
+ db->exception_reported = TRUE;\r
+ return NULL;\r
+diff --git a/lib/directory.cc b/lib/directory.cc\r
+index 8daaec8..b836ea2 100644\r
+--- a/lib/directory.cc\r
++++ b/lib/directory.cc\r
+@@ -186,7 +186,7 @@ _notmuch_directory_create (notmuch_database_t *notmuch,\r
+ directory->mtime = Xapian::sortable_unserialise (\r
+ directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));\r
+ } catch (const Xapian::Error &error) {\r
+- fprintf (stderr,\r
++ _notmuch_database_log (notmuch,\r
+ "A Xapian exception occurred creating a directory: %s.\n",\r
+ error.get_msg().c_str());\r
+ notmuch->exception_reported = TRUE;\r
+@@ -228,7 +228,7 @@ notmuch_directory_set_mtime (notmuch_directory_t *directory,\r
+ \r
+ db->replace_document (directory->document_id, directory->doc);\r
+ } catch (const Xapian::Error &error) {\r
+- fprintf (stderr,\r
++ _notmuch_database_log (notmuch,\r
+ "A Xapian exception occurred setting directory mtime: %s.\n",\r
+ error.get_msg().c_str());\r
+ notmuch->exception_reported = TRUE;\r
+diff --git a/lib/index.cc b/lib/index.cc\r
+index c88ed8d..e81aa81 100644\r
+--- a/lib/index.cc\r
++++ b/lib/index.cc\r
+@@ -314,7 +314,8 @@ _index_mime_part (notmuch_message_t *message,\r
+ const char *charset;\r
+ \r
+ if (! part) {\r
+- fprintf (stderr, "Warning: Not indexing empty mime part.\n");\r
++ _notmuch_database_log (_notmuch_message_database (message),\r
++ "Warning: Not indexing empty mime part.\n");\r
+ return;\r
+ }\r
+ \r
+@@ -344,7 +345,8 @@ _index_mime_part (notmuch_message_t *message,\r
+ if (i == 1)\r
+ continue;\r
+ if (i > 1)\r
+- fprintf (stderr, "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");\r
++ _notmuch_database_log (_notmuch_message_database (message),\r
++ "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");\r
+ }\r
+ if (GMIME_IS_MULTIPART_ENCRYPTED (multipart)) {\r
+ /* Don't index encrypted parts. */\r
+@@ -367,8 +369,9 @@ _index_mime_part (notmuch_message_t *message,\r
+ }\r
+ \r
+ if (! (GMIME_IS_PART (part))) {\r
+- fprintf (stderr, "Warning: Not indexing unknown mime part: %s.\n",\r
+- g_type_name (G_OBJECT_TYPE (part)));\r
++ _notmuch_database_log (_notmuch_message_database (message),\r
++ "Warning: Not indexing unknown mime part: %s.\n",\r
++ g_type_name (G_OBJECT_TYPE (part)));\r
+ return;\r
+ }\r
+ \r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index b84e5e1..a8ca988 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -252,7 +252,7 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,\r
+ \r
+ doc_id = _notmuch_database_generate_doc_id (notmuch);\r
+ } catch (const Xapian::Error &error) {\r
+- fprintf (stderr, "A Xapian exception occurred creating message: %s\n",\r
++ _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred creating message: %s\n",\r
+ error.get_msg().c_str());\r
+ notmuch->exception_reported = TRUE;\r
+ *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;\r
+@@ -467,7 +467,7 @@ notmuch_message_get_header (notmuch_message_t *message, const char *header)\r
+ return talloc_strdup (message, value.c_str ());\r
+ \r
+ } catch (Xapian::Error &error) {\r
+- fprintf (stderr, "A Xapian exception occurred when reading header: %s\n",\r
++ _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred when reading header: %s\n",\r
+ error.get_msg().c_str());\r
+ message->notmuch->exception_reported = TRUE;\r
+ return NULL;\r
+@@ -921,7 +921,7 @@ notmuch_message_get_date (notmuch_message_t *message)\r
+ try {\r
+ value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);\r
+ } catch (Xapian::Error &error) {\r
+- fprintf (stderr, "A Xapian exception occurred when reading date: %s\n",\r
++ _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred when reading date: %s\n",\r
+ error.get_msg().c_str());\r
+ message->notmuch->exception_reported = TRUE;\r
+ return 0;\r
+diff --git a/lib/query.cc b/lib/query.cc\r
+index 57aa6d2..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
+- fprintf (stderr, "A Xapian exception occurred performing query: %s\n",\r
+- error.get_msg().c_str());\r
+- fprintf (stderr, "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
+- fprintf (stderr, "A Xapian exception occurred: %s\n",\r
+- error.get_msg().c_str());\r
+- fprintf (stderr, "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 f1ca543..67a5e8d 100755\r
+--- a/test/T560-lib-error.sh\r
++++ b/test/T560-lib-error.sh\r
+@@ -101,6 +101,9 @@ int main (int argc, char** argv)\r
+ fprintf (stderr, "error opening database: %d\n", stat);\r
+ }\r
+ stat = notmuch_database_add_message (db, "/dev/null", NULL);\r
++ if (stat)\r
++ fputs (notmuch_database_status_string (db), stderr);\r
++\r
+ }\r
+ EOF\r
+ cat <<'EOF' >EXPECTED\r
+@@ -153,8 +156,9 @@ int main (int argc, char** argv)\r
+ EOF\r
+ cat <<'EOF' >EXPECTED\r
+ == stdout ==\r
+-== stderr ==\r
+ Path already exists: CWD/mail\r
++\r
++== stderr ==\r
+ EOF\r
+ test_expect_equal_file EXPECTED OUTPUT\r
+ \r
+-- \r
+2.1.4\r
+\r