lib: replace almost all fprintfs in library with _n_d_log

This is not supposed to change any functionality from an end user
point of view. Note that it will eliminate some output to stderr. The
query debugging output is left as is; it doesn't really fit with the
current primitive logging model. The remaining "bad" fprintf will need
an internal API change.
This commit is contained in:
David Bremner 2014-12-26 17:25:35 +01:00
parent 9b73a8bcc9
commit 736ac26407
6 changed files with 50 additions and 33 deletions

View file

@ -473,7 +473,7 @@ notmuch_database_find_message (notmuch_database_t *notmuch,
return NOTMUCH_STATUS_SUCCESS; return NOTMUCH_STATUS_SUCCESS;
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred finding message: %s.\n", _notmuch_database_log (notmuch, "A Xapian exception occurred finding message: %s.\n",
error.get_msg().c_str()); error.get_msg().c_str());
notmuch->exception_reported = TRUE; notmuch->exception_reported = TRUE;
*message_ret = NULL; *message_ret = NULL;
@ -722,7 +722,7 @@ notmuch_status_t
_notmuch_database_ensure_writable (notmuch_database_t *notmuch) _notmuch_database_ensure_writable (notmuch_database_t *notmuch)
{ {
if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) { if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
fprintf (stderr, "Cannot write to a read-only database.\n"); _notmuch_database_log (notmuch, "Cannot write to a read-only database.\n");
return NOTMUCH_STATUS_READ_ONLY_DATABASE; return NOTMUCH_STATUS_READ_ONLY_DATABASE;
} }
@ -1022,7 +1022,7 @@ notmuch_database_close (notmuch_database_t *notmuch)
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
status = NOTMUCH_STATUS_XAPIAN_EXCEPTION; status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
if (! notmuch->exception_reported) { if (! notmuch->exception_reported) {
fprintf (stderr, "Error: A Xapian exception occurred closing database: %s\n", _notmuch_database_log (notmuch, "Error: A Xapian exception occurred closing database: %s\n",
error.get_msg().c_str()); error.get_msg().c_str());
} }
} }
@ -1154,12 +1154,12 @@ notmuch_database_compact (const char *path,
} }
if (stat (backup_path, &statbuf) != -1) { if (stat (backup_path, &statbuf) != -1) {
fprintf (stderr, "Path already exists: %s\n", backup_path); _notmuch_database_log (notmuch, "Path already exists: %s\n", backup_path);
ret = NOTMUCH_STATUS_FILE_ERROR; ret = NOTMUCH_STATUS_FILE_ERROR;
goto DONE; goto DONE;
} }
if (errno != ENOENT) { if (errno != ENOENT) {
fprintf (stderr, "Unknown error while stat()ing path: %s\n", _notmuch_database_log (notmuch, "Unknown error while stat()ing path: %s\n",
strerror (errno)); strerror (errno));
ret = NOTMUCH_STATUS_FILE_ERROR; ret = NOTMUCH_STATUS_FILE_ERROR;
goto DONE; goto DONE;
@ -1179,20 +1179,20 @@ notmuch_database_compact (const char *path,
compactor.set_destdir (compact_xapian_path); compactor.set_destdir (compact_xapian_path);
compactor.compact (); compactor.compact ();
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "Error while compacting: %s\n", error.get_msg().c_str()); _notmuch_database_log (notmuch, "Error while compacting: %s\n", error.get_msg().c_str());
ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION; ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
goto DONE; goto DONE;
} }
if (rename (xapian_path, backup_path)) { if (rename (xapian_path, backup_path)) {
fprintf (stderr, "Error moving %s to %s: %s\n", _notmuch_database_log (notmuch, "Error moving %s to %s: %s\n",
xapian_path, backup_path, strerror (errno)); xapian_path, backup_path, strerror (errno));
ret = NOTMUCH_STATUS_FILE_ERROR; ret = NOTMUCH_STATUS_FILE_ERROR;
goto DONE; goto DONE;
} }
if (rename (compact_xapian_path, xapian_path)) { if (rename (compact_xapian_path, xapian_path)) {
fprintf (stderr, "Error moving %s to %s: %s\n", _notmuch_database_log (notmuch, "Error moving %s to %s: %s\n",
compact_xapian_path, xapian_path, strerror (errno)); compact_xapian_path, xapian_path, strerror (errno));
ret = NOTMUCH_STATUS_FILE_ERROR; ret = NOTMUCH_STATUS_FILE_ERROR;
goto DONE; goto DONE;
@ -1200,7 +1200,7 @@ notmuch_database_compact (const char *path,
if (! keep_backup) { if (! keep_backup) {
if (rmtree (backup_path)) { if (rmtree (backup_path)) {
fprintf (stderr, "Error removing old database %s: %s\n", _notmuch_database_log (notmuch, "Error removing old database %s: %s\n",
backup_path, strerror (errno)); backup_path, strerror (errno));
ret = NOTMUCH_STATUS_FILE_ERROR; ret = NOTMUCH_STATUS_FILE_ERROR;
goto DONE; goto DONE;
@ -1211,6 +1211,10 @@ notmuch_database_compact (const char *path,
if (notmuch) { if (notmuch) {
notmuch_status_t ret2; notmuch_status_t ret2;
const char *str = notmuch_database_status_string (notmuch);
if (status_cb && str)
status_cb (str, closure);
ret2 = notmuch_database_destroy (notmuch); ret2 = notmuch_database_destroy (notmuch);
/* don't clobber previous error status */ /* don't clobber previous error status */
@ -1229,7 +1233,7 @@ notmuch_database_compact (unused (const char *path),
unused (notmuch_compact_status_cb_t status_cb), unused (notmuch_compact_status_cb_t status_cb),
unused (void *closure)) unused (void *closure))
{ {
fprintf (stderr, "notmuch was compiled against a xapian version lacking compaction support.\n"); _notmuch_database_log (notmuch, "notmuch was compiled against a xapian version lacking compaction support.\n");
return NOTMUCH_STATUS_UNSUPPORTED_OPERATION; return NOTMUCH_STATUS_UNSUPPORTED_OPERATION;
} }
#endif #endif
@ -1507,7 +1511,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
} }
if (private_status) { if (private_status) {
fprintf (stderr, _notmuch_database_log (notmuch,
"Upgrade failed while creating ghost messages.\n"); "Upgrade failed while creating ghost messages.\n");
status = COERCE_STATUS (private_status, "Unexpected status from _notmuch_message_initialize_ghost"); status = COERCE_STATUS (private_status, "Unexpected status from _notmuch_message_initialize_ghost");
goto DONE; goto DONE;
@ -1557,7 +1561,7 @@ notmuch_database_begin_atomic (notmuch_database_t *notmuch)
try { try {
(static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->begin_transaction (false); (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->begin_transaction (false);
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred beginning transaction: %s.\n", _notmuch_database_log (notmuch, "A Xapian exception occurred beginning transaction: %s.\n",
error.get_msg().c_str()); error.get_msg().c_str());
notmuch->exception_reported = TRUE; notmuch->exception_reported = TRUE;
return NOTMUCH_STATUS_XAPIAN_EXCEPTION; return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
@ -1591,7 +1595,7 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch)
if (thresh && atoi (thresh) == 1) if (thresh && atoi (thresh) == 1)
db->flush (); db->flush ();
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred committing transaction: %s.\n", _notmuch_database_log (notmuch, "A Xapian exception occurred committing transaction: %s.\n",
error.get_msg().c_str()); error.get_msg().c_str());
notmuch->exception_reported = TRUE; notmuch->exception_reported = TRUE;
return NOTMUCH_STATUS_XAPIAN_EXCEPTION; return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
@ -1837,7 +1841,7 @@ notmuch_database_get_directory (notmuch_database_t *notmuch,
*directory = _notmuch_directory_create (notmuch, path, *directory = _notmuch_directory_create (notmuch, path,
NOTMUCH_FIND_LOOKUP, &status); NOTMUCH_FIND_LOOKUP, &status);
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred getting directory: %s.\n", _notmuch_database_log (notmuch, "A Xapian exception occurred getting directory: %s.\n",
error.get_msg().c_str()); error.get_msg().c_str());
notmuch->exception_reported = TRUE; notmuch->exception_reported = TRUE;
status = NOTMUCH_STATUS_XAPIAN_EXCEPTION; status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
@ -2419,7 +2423,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
_notmuch_message_sync (message); _notmuch_message_sync (message);
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred adding message: %s.\n", _notmuch_database_log (notmuch, "A Xapian exception occurred adding message: %s.\n",
error.get_msg().c_str()); error.get_msg().c_str());
notmuch->exception_reported = TRUE; notmuch->exception_reported = TRUE;
ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION; ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
@ -2511,7 +2515,7 @@ notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
status = NOTMUCH_STATUS_OUT_OF_MEMORY; status = NOTMUCH_STATUS_OUT_OF_MEMORY;
} }
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "Error: A Xapian exception occurred finding message by filename: %s\n", _notmuch_database_log (notmuch, "Error: A Xapian exception occurred finding message by filename: %s\n",
error.get_msg().c_str()); error.get_msg().c_str());
notmuch->exception_reported = TRUE; notmuch->exception_reported = TRUE;
status = NOTMUCH_STATUS_XAPIAN_EXCEPTION; status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
@ -2564,7 +2568,7 @@ notmuch_database_get_all_tags (notmuch_database_t *db)
_notmuch_string_list_sort (tags); _notmuch_string_list_sort (tags);
return _notmuch_tags_create (db, tags); return _notmuch_tags_create (db, tags);
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred getting tags: %s.\n", _notmuch_database_log (db, "A Xapian exception occurred getting tags: %s.\n",
error.get_msg().c_str()); error.get_msg().c_str());
db->exception_reported = TRUE; db->exception_reported = TRUE;
return NULL; return NULL;

View file

@ -186,7 +186,7 @@ _notmuch_directory_create (notmuch_database_t *notmuch,
directory->mtime = Xapian::sortable_unserialise ( directory->mtime = Xapian::sortable_unserialise (
directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP)); directory->doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, _notmuch_database_log (notmuch,
"A Xapian exception occurred creating a directory: %s.\n", "A Xapian exception occurred creating a directory: %s.\n",
error.get_msg().c_str()); error.get_msg().c_str());
notmuch->exception_reported = TRUE; notmuch->exception_reported = TRUE;
@ -228,7 +228,7 @@ notmuch_directory_set_mtime (notmuch_directory_t *directory,
db->replace_document (directory->document_id, directory->doc); db->replace_document (directory->document_id, directory->doc);
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, _notmuch_database_log (notmuch,
"A Xapian exception occurred setting directory mtime: %s.\n", "A Xapian exception occurred setting directory mtime: %s.\n",
error.get_msg().c_str()); error.get_msg().c_str());
notmuch->exception_reported = TRUE; notmuch->exception_reported = TRUE;

View file

@ -314,7 +314,8 @@ _index_mime_part (notmuch_message_t *message,
const char *charset; const char *charset;
if (! part) { if (! part) {
fprintf (stderr, "Warning: Not indexing empty mime part.\n"); _notmuch_database_log (_notmuch_message_database (message),
"Warning: Not indexing empty mime part.\n");
return; return;
} }
@ -344,7 +345,8 @@ _index_mime_part (notmuch_message_t *message,
if (i == 1) if (i == 1)
continue; continue;
if (i > 1) if (i > 1)
fprintf (stderr, "Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n"); _notmuch_database_log (_notmuch_message_database (message),
"Warning: Unexpected extra parts of multipart/signed. Indexing anyway.\n");
} }
if (GMIME_IS_MULTIPART_ENCRYPTED (multipart)) { if (GMIME_IS_MULTIPART_ENCRYPTED (multipart)) {
/* Don't index encrypted parts. */ /* Don't index encrypted parts. */
@ -367,7 +369,8 @@ _index_mime_part (notmuch_message_t *message,
} }
if (! (GMIME_IS_PART (part))) { if (! (GMIME_IS_PART (part))) {
fprintf (stderr, "Warning: Not indexing unknown mime part: %s.\n", _notmuch_database_log (_notmuch_message_database (message),
"Warning: Not indexing unknown mime part: %s.\n",
g_type_name (G_OBJECT_TYPE (part))); g_type_name (G_OBJECT_TYPE (part)));
return; return;
} }

View file

@ -252,7 +252,7 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
doc_id = _notmuch_database_generate_doc_id (notmuch); doc_id = _notmuch_database_generate_doc_id (notmuch);
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred creating message: %s\n", _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred creating message: %s\n",
error.get_msg().c_str()); error.get_msg().c_str());
notmuch->exception_reported = TRUE; notmuch->exception_reported = TRUE;
*status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION; *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
@ -467,7 +467,7 @@ notmuch_message_get_header (notmuch_message_t *message, const char *header)
return talloc_strdup (message, value.c_str ()); return talloc_strdup (message, value.c_str ());
} catch (Xapian::Error &error) { } catch (Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred when reading header: %s\n", _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred when reading header: %s\n",
error.get_msg().c_str()); error.get_msg().c_str());
message->notmuch->exception_reported = TRUE; message->notmuch->exception_reported = TRUE;
return NULL; return NULL;
@ -921,7 +921,7 @@ notmuch_message_get_date (notmuch_message_t *message)
try { try {
value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP); value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
} catch (Xapian::Error &error) { } catch (Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred when reading date: %s\n", _notmuch_database_log(_notmuch_message_database (message), "A Xapian exception occurred when reading date: %s\n",
error.get_msg().c_str()); error.get_msg().c_str());
message->notmuch->exception_reported = TRUE; message->notmuch->exception_reported = TRUE;
return 0; return 0;

View file

@ -296,9 +296,12 @@ notmuch_query_search_messages_st (notmuch_query_t *query,
return NOTMUCH_STATUS_SUCCESS; return NOTMUCH_STATUS_SUCCESS;
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred performing query: %s\n", _notmuch_database_log (notmuch,
error.get_msg().c_str()); "A Xapian exception occurred performing query: %s\n"
fprintf (stderr, "Query string was: %s\n", query->query_string); "Query string was: %s\n",
error.get_msg().c_str(),
query->query_string);
notmuch->exception_reported = TRUE; notmuch->exception_reported = TRUE;
talloc_free (messages); talloc_free (messages);
return NOTMUCH_STATUS_XAPIAN_EXCEPTION; return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
@ -597,9 +600,12 @@ notmuch_query_count_messages (notmuch_query_t *query)
count = mset.get_matches_estimated(); count = mset.get_matches_estimated();
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred: %s\n", _notmuch_database_log (notmuch,
error.get_msg().c_str()); "A Xapian exception occurred performing query: %s\n"
fprintf (stderr, "Query string was: %s\n", query->query_string); "Query string was: %s\n",
error.get_msg().c_str(),
query->query_string);
} }
return count; return count;

View file

@ -101,6 +101,9 @@ int main (int argc, char** argv)
fprintf (stderr, "error opening database: %d\n", stat); fprintf (stderr, "error opening database: %d\n", stat);
} }
stat = notmuch_database_add_message (db, "/dev/null", NULL); stat = notmuch_database_add_message (db, "/dev/null", NULL);
if (stat)
fputs (notmuch_database_status_string (db), stderr);
} }
EOF EOF
cat <<'EOF' >EXPECTED cat <<'EOF' >EXPECTED
@ -128,8 +131,9 @@ int main (int argc, char** argv)
EOF EOF
cat <<'EOF' >EXPECTED cat <<'EOF' >EXPECTED
== stdout == == stdout ==
== stderr ==
Path already exists: CWD/mail Path already exists: CWD/mail
== stderr ==
EOF EOF
test_expect_equal_file EXPECTED OUTPUT test_expect_equal_file EXPECTED OUTPUT