1 Return-Path: <bremner@tesseract.cs.unb.ca>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id 8179F429E25
\r
6 for <notmuch@notmuchmail.org>; Sat, 27 Dec 2014 14:06:11 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]
\r
13 Received: from olra.theworths.org ([127.0.0.1])
\r
14 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
15 with ESMTP id oop9y-KI+yfa for <notmuch@notmuchmail.org>;
\r
16 Sat, 27 Dec 2014 14:06:09 -0800 (PST)
\r
17 Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155])
\r
18 (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))
\r
19 (No client certificate requested)
\r
20 by olra.theworths.org (Postfix) with ESMTPS id 93CCF431FBC
\r
21 for <notmuch@notmuchmail.org>; Sat, 27 Dec 2014 14:06:09 -0800 (PST)
\r
22 Received: from remotemail by yantan.tethera.net with local (Exim 4.80)
\r
23 (envelope-from <bremner@tesseract.cs.unb.ca>)
\r
24 id 1Y4zUv-0000E8-9f; Sat, 27 Dec 2014 18:06:09 -0400
\r
25 Received: (nullmailer pid 1473 invoked by uid 1000); Sat, 27 Dec 2014
\r
27 From: David Bremner <david@tethera.net>
\r
28 To: notmuch@notmuchmail.org
\r
29 Subject: [PATCH v2 1/6] lib: add "verbose" versions of
\r
30 notmuch_database_{open,create}
\r
31 Date: Sat, 27 Dec 2014 23:05:32 +0100
\r
32 Message-Id: <1419717937-1108-2-git-send-email-david@tethera.net>
\r
33 X-Mailer: git-send-email 2.1.3
\r
34 In-Reply-To: <1419717937-1108-1-git-send-email-david@tethera.net>
\r
35 References: <1419717937-1108-1-git-send-email-david@tethera.net>
\r
36 X-BeenThere: notmuch@notmuchmail.org
\r
37 X-Mailman-Version: 2.1.13
\r
39 List-Id: "Use and development of the notmuch mail system."
\r
40 <notmuch.notmuchmail.org>
\r
41 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
42 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
43 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
44 List-Post: <mailto:notmuch@notmuchmail.org>
\r
45 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
46 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
47 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
48 X-List-Received-Date: Sat, 27 Dec 2014 22:06:11 -0000
\r
50 This has the the non-trivial side effect silencing all output to
\r
51 stderror from these two routines.
\r
53 The stdargs based infrastucture will be used in following commits for
\r
54 a more general logging mechanism.
\r
56 The changes to notmuch-{new,search} and test/symbol-test are just to
\r
57 make the test suite pass. The fact that no other changes are needed is
\r
58 probably a sign of weakness in the test suite.
\r
60 lib/database.cc | 75 +++++++++++++++++++++++++++++++++++++++++++++--------
\r
61 lib/notmuch.h | 21 +++++++++++++++
\r
62 notmuch-new.c | 8 ++++--
\r
63 notmuch-search.c | 8 ++++--
\r
64 test/symbol-test.cc | 6 ++++-
\r
65 5 files changed, 102 insertions(+), 16 deletions(-)
\r
67 diff --git a/lib/database.cc b/lib/database.cc
\r
68 index 3601f9d..06e6fc5 100644
\r
69 --- a/lib/database.cc
\r
70 +++ b/lib/database.cc
\r
71 @@ -343,6 +343,35 @@ notmuch_status_to_string (notmuch_status_t status)
\r
75 +vlog_to_string (void *ctx,
\r
76 + char **status_string,
\r
77 + const char *format,
\r
80 + if (!status_string)
\r
83 + if (*status_string)
\r
84 + talloc_free (*status_string);
\r
86 + *status_string = talloc_vasprintf (ctx, format, va_args);
\r
90 +log_to_string (char **str,
\r
91 + const char *format,
\r
96 + va_start (va_args, format);
\r
98 + vlog_to_string (NULL, str, format, va_args);
\r
100 + va_end (va_args);
\r
104 find_doc_ids_for_term (notmuch_database_t *notmuch,
\r
106 Xapian::PostingIterator *begin,
\r
107 @@ -602,28 +631,37 @@ parse_references (void *ctx,
\r
109 notmuch_database_create (const char *path, notmuch_database_t **database)
\r
111 + return notmuch_database_create_verbose (path, database, NULL);
\r
115 +notmuch_database_create_verbose (const char *path,
\r
116 + notmuch_database_t **database,
\r
117 + char **status_string)
\r
119 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
\r
120 notmuch_database_t *notmuch = NULL;
\r
121 char *notmuch_path = NULL;
\r
122 + char *message = NULL;
\r
126 if (path == NULL) {
\r
127 - fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
\r
128 + log_to_string (&message, "Error: Cannot create a database for a NULL path.\n");
\r
129 status = NOTMUCH_STATUS_NULL_POINTER;
\r
133 err = stat (path, &st);
\r
135 - fprintf (stderr, "Error: Cannot create database at %s: %s.\n",
\r
136 + log_to_string (&message, "Error: Cannot create database at %s: %s.\n",
\r
137 path, strerror (errno));
\r
138 status = NOTMUCH_STATUS_FILE_ERROR;
\r
142 if (! S_ISDIR (st.st_mode)) {
\r
143 - fprintf (stderr, "Error: Cannot create database at %s: Not a directory.\n",
\r
144 + log_to_string (&message, "Error: Cannot create database at %s: Not a directory.\n",
\r
146 status = NOTMUCH_STATUS_FILE_ERROR;
\r
148 @@ -634,7 +672,7 @@ notmuch_database_create (const char *path, notmuch_database_t **database)
\r
149 err = mkdir (notmuch_path, 0755);
\r
152 - fprintf (stderr, "Error: Cannot create directory %s: %s.\n",
\r
153 + log_to_string (&message, "Error: Cannot create directory %s: %s.\n",
\r
154 notmuch_path, strerror (errno));
\r
155 status = NOTMUCH_STATUS_FILE_ERROR;
\r
157 @@ -660,6 +698,8 @@ notmuch_database_create (const char *path, notmuch_database_t **database)
\r
159 talloc_free (notmuch_path);
\r
162 + *status_string = strdup(message);
\r
164 *database = notmuch;
\r
166 @@ -760,37 +800,47 @@ notmuch_database_open (const char *path,
\r
167 notmuch_database_mode_t mode,
\r
168 notmuch_database_t **database)
\r
170 + return notmuch_database_open_verbose(path, mode, database, NULL);
\r
174 +notmuch_database_open_verbose (const char *path,
\r
175 + notmuch_database_mode_t mode,
\r
176 + notmuch_database_t **database,
\r
177 + char **status_string)
\r
179 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
\r
180 void *local = talloc_new (NULL);
\r
181 notmuch_database_t *notmuch = NULL;
\r
182 char *notmuch_path, *xapian_path, *incompat_features;
\r
183 + char *message = NULL;
\r
186 unsigned int i, version;
\r
187 static int initialized = 0;
\r
189 if (path == NULL) {
\r
190 - fprintf (stderr, "Error: Cannot open a database for a NULL path.\n");
\r
191 + log_to_string (&message, "Error: Cannot open a database for a NULL path.\n");
\r
192 status = NOTMUCH_STATUS_NULL_POINTER;
\r
196 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {
\r
197 - fprintf (stderr, "Out of memory\n");
\r
198 + log_to_string (&message, "Out of memory\n");
\r
199 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
203 err = stat (notmuch_path, &st);
\r
205 - fprintf (stderr, "Error opening database at %s: %s\n",
\r
206 + log_to_string (&message, "Error opening database at %s: %s\n",
\r
207 notmuch_path, strerror (errno));
\r
208 status = NOTMUCH_STATUS_FILE_ERROR;
\r
212 if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
\r
213 - fprintf (stderr, "Out of memory\n");
\r
214 + log_to_string (&message, "Out of memory\n");
\r
215 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
218 @@ -830,7 +880,7 @@ notmuch_database_open (const char *path,
\r
219 * means a dramatically incompatible change. */
\r
220 version = notmuch_database_get_version (notmuch);
\r
221 if (version > NOTMUCH_DATABASE_VERSION) {
\r
223 + log_to_string (&message,
\r
224 "Error: Notmuch database at %s\n"
\r
225 " has a newer database format version (%u) than supported by this\n"
\r
226 " version of notmuch (%u).\n",
\r
227 @@ -849,7 +899,7 @@ notmuch_database_open (const char *path,
\r
228 version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',
\r
229 &incompat_features);
\r
230 if (incompat_features) {
\r
232 + log_to_string (&message,
\r
233 "Error: Notmuch database at %s\n"
\r
234 " requires features (%s)\n"
\r
235 " not supported by this version of notmuch.\n",
\r
236 @@ -899,7 +949,7 @@ notmuch_database_open (const char *path,
\r
237 notmuch->query_parser->add_prefix (prefix->name, prefix->prefix);
\r
239 } catch (const Xapian::Error &error) {
\r
240 - fprintf (stderr, "A Xapian exception occurred opening database: %s\n",
\r
241 + log_to_string (&message, "A Xapian exception occurred opening database: %s\n",
\r
242 error.get_msg().c_str());
\r
243 notmuch_database_destroy (notmuch);
\r
245 @@ -909,6 +959,9 @@ notmuch_database_open (const char *path,
\r
247 talloc_free (local);
\r
249 + if (status_string && message)
\r
250 + *status_string = strdup (message);
\r
253 *database = notmuch;
\r
255 diff --git a/lib/notmuch.h b/lib/notmuch.h
\r
256 index 220839b..0dfac8f 100644
\r
257 --- a/lib/notmuch.h
\r
258 +++ b/lib/notmuch.h
\r
259 @@ -230,6 +230,16 @@ notmuch_status_t
\r
260 notmuch_database_create (const char *path, notmuch_database_t **database);
\r
263 + * Like notmuch_database_create, except optionally return an error
\r
264 + * message. This message is allocated by malloc and should be freed by
\r
268 +notmuch_database_create_verbose (const char *path,
\r
269 + notmuch_database_t **database,
\r
270 + char **error_message);
\r
273 * Database open mode for notmuch_database_open.
\r
276 @@ -279,6 +289,17 @@ notmuch_status_t
\r
277 notmuch_database_open (const char *path,
\r
278 notmuch_database_mode_t mode,
\r
279 notmuch_database_t **database);
\r
281 + * Like notmuch_database_open, except optionally return an error
\r
282 + * message. This message is allocated by malloc and should be freed by
\r
287 +notmuch_database_open_verbose (const char *path,
\r
288 + notmuch_database_mode_t mode,
\r
289 + notmuch_database_t **database,
\r
290 + char **error_message);
\r
293 * Commit changes and close the given notmuch database.
\r
294 diff --git a/notmuch-new.c b/notmuch-new.c
\r
295 index ddf42c1..93b70bf 100644
\r
296 --- a/notmuch-new.c
\r
297 +++ b/notmuch-new.c
\r
298 @@ -985,9 +985,13 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
\r
299 return EXIT_FAILURE;
\r
300 add_files_state.total_files = count;
\r
302 - if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
\r
304 + char *status_string = NULL;
\r
305 + if (notmuch_database_open_verbose (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
\r
306 + ¬much, &status_string)) {
\r
307 + if (status_string) fputs (status_string, stderr);
\r
309 return EXIT_FAILURE;
\r
312 if (notmuch_database_needs_upgrade (notmuch)) {
\r
313 time_t now = time (NULL);
\r
314 diff --git a/notmuch-search.c b/notmuch-search.c
\r
315 index 14b9f01..250e567 100644
\r
316 --- a/notmuch-search.c
\r
317 +++ b/notmuch-search.c
\r
318 @@ -545,6 +545,7 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar
\r
322 + char *status_string = NULL;
\r
324 switch (ctx->format_sel) {
\r
325 case NOTMUCH_FORMAT_TEXT:
\r
326 @@ -570,9 +571,12 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar
\r
328 notmuch_exit_if_unsupported_format ();
\r
330 - if (notmuch_database_open (notmuch_config_get_database_path (config),
\r
331 - NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))
\r
332 + if (notmuch_database_open_verbose (
\r
333 + notmuch_config_get_database_path (config),
\r
334 + NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {
\r
335 + if (status_string) fputs (status_string, stderr);
\r
336 return EXIT_FAILURE;
\r
339 query_str = query_string_from_args (ctx->notmuch, argc, argv);
\r
340 if (query_str == NULL) {
\r
341 diff --git a/test/symbol-test.cc b/test/symbol-test.cc
\r
342 index 3e96c03..9f8eea7 100644
\r
343 --- a/test/symbol-test.cc
\r
344 +++ b/test/symbol-test.cc
\r
348 notmuch_database_t *notmuch;
\r
349 - notmuch_database_open("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much);
\r
350 + char *message = NULL;
\r
352 + if (notmuch_database_open_verbose ("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much, &message))
\r
353 + if (message) fputs (message, stderr);
\r
357 (void) new Xapian::WritableDatabase("./nonexistant", Xapian::DB_OPEN);
\r