Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / df / f7d42c284389e963188a2d62ec083dab0a9ca2
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 C6BAD429E49\r
6         for <notmuch@notmuchmail.org>; Sun, 28 Dec 2014 09:38:11 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\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 W1fNrezklmQx for <notmuch@notmuchmail.org>;\r
16         Sun, 28 Dec 2014 09:38:10 -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 5ECA3429E3E\r
21         for <notmuch@notmuchmail.org>; Sun, 28 Dec 2014 09:38: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 1Y5Hn6-0002gB-Tr; Sun, 28 Dec 2014 13:38:08 -0400\r
25 Received: (nullmailer pid 11494 invoked by uid 1000); Sun, 28 Dec 2014\r
26         17:37:40 -0000\r
27 From: David Bremner <david@tethera.net>\r
28 To: notmuch@notmuchmail.org\r
29 Subject: [Patch v3 1/6] lib: add "verbose" versions of\r
30         notmuch_database_{open,create}\r
31 Date: Sun, 28 Dec 2014 18:33:45 +0100\r
32 Message-Id: <1419788030-10567-2-git-send-email-david@tethera.net>\r
33 X-Mailer: git-send-email 2.1.3\r
34 In-Reply-To: <1419788030-10567-1-git-send-email-david@tethera.net>\r
35 References: <1419788030-10567-1-git-send-email-david@tethera.net>\r
36 X-BeenThere: notmuch@notmuchmail.org\r
37 X-Mailman-Version: 2.1.13\r
38 Precedence: list\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: Sun, 28 Dec 2014 17:38:12 -0000\r
49 \r
50 This has the the non-trivial side effect silencing all output to\r
51 stderror from these two routines.\r
52 \r
53 The stdargs based infrastucture will be used in following commits for\r
54 a more general logging mechanism.\r
55 \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
59 ---\r
60  lib/database.cc     | 88 ++++++++++++++++++++++++++++++++++++++++++++---------\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, 111 insertions(+), 20 deletions(-)\r
66 \r
67 diff --git a/lib/database.cc b/lib/database.cc\r
68 index 3601f9d..4f4e871 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
72  }\r
73  \r
74  static void\r
75 +vlog_to_string (void *ctx,\r
76 +              char **status_string,\r
77 +              const char *format,\r
78 +              va_list va_args)\r
79 +{\r
80 +    if (!status_string)\r
81 +       return;\r
82 +\r
83 +    if (*status_string)\r
84 +       talloc_free (*status_string);\r
85 +\r
86 +    *status_string = talloc_vasprintf (ctx, format, va_args);\r
87 +}\r
88 +\r
89 +static void\r
90 +log_to_string (char **str,\r
91 +              const char *format,\r
92 +              ...)\r
93 +{\r
94 +    va_list va_args;\r
95 +\r
96 +    va_start (va_args, format);\r
97 +\r
98 +    vlog_to_string (NULL, str, format, va_args);\r
99 +\r
100 +    va_end (va_args);\r
101 +}\r
102 +\r
103 +static void\r
104  find_doc_ids_for_term (notmuch_database_t *notmuch,\r
105                        const char *term,\r
106                        Xapian::PostingIterator *begin,\r
107 @@ -602,28 +631,37 @@ parse_references (void *ctx,\r
108  notmuch_status_t\r
109  notmuch_database_create (const char *path, notmuch_database_t **database)\r
110  {\r
111 +    return notmuch_database_create_verbose (path, database, NULL);\r
112 +}\r
113 +\r
114 +notmuch_status_t\r
115 +notmuch_database_create_verbose (const char *path,\r
116 +                                notmuch_database_t **database,\r
117 +                                char **status_string)\r
118 +{\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
123      struct stat st;\r
124      int err;\r
125  \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
130         goto DONE;\r
131      }\r
132  \r
133      err = stat (path, &st);\r
134      if (err) {\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
139         goto DONE;\r
140      }\r
141  \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
145                  path);\r
146         status = NOTMUCH_STATUS_FILE_ERROR;\r
147         goto DONE;\r
148 @@ -634,15 +672,15 @@ notmuch_database_create (const char *path, notmuch_database_t **database)\r
149      err = mkdir (notmuch_path, 0755);\r
150  \r
151      if (err) {\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
156         goto DONE;\r
157      }\r
158  \r
159 -    status = notmuch_database_open (path,\r
160 -                                   NOTMUCH_DATABASE_MODE_READ_WRITE,\r
161 -                                   &notmuch);\r
162 +    status = notmuch_database_open_verbose (path,\r
163 +                                           NOTMUCH_DATABASE_MODE_READ_WRITE,\r
164 +                                           &notmuch, &message);\r
165      if (status)\r
166         goto DONE;\r
167  \r
168 @@ -660,6 +698,8 @@ notmuch_database_create (const char *path, notmuch_database_t **database)\r
169      if (notmuch_path)\r
170         talloc_free (notmuch_path);\r
171  \r
172 +    if (message)\r
173 +       *status_string = strdup(message);\r
174      if (database)\r
175         *database = notmuch;\r
176      else\r
177 @@ -760,37 +800,47 @@ notmuch_database_open (const char *path,\r
178                        notmuch_database_mode_t mode,\r
179                        notmuch_database_t **database)\r
180  {\r
181 +    return notmuch_database_open_verbose(path, mode, database, NULL);\r
182 +}\r
183 +\r
184 +notmuch_status_t\r
185 +notmuch_database_open_verbose (const char *path,\r
186 +                              notmuch_database_mode_t mode,\r
187 +                              notmuch_database_t **database,\r
188 +                              char **status_string)\r
189 +{\r
190      notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
191      void *local = talloc_new (NULL);\r
192      notmuch_database_t *notmuch = NULL;\r
193      char *notmuch_path, *xapian_path, *incompat_features;\r
194 +    char *message = NULL;\r
195      struct stat st;\r
196      int err;\r
197      unsigned int i, version;\r
198      static int initialized = 0;\r
199  \r
200      if (path == NULL) {\r
201 -       fprintf (stderr, "Error: Cannot open a database for a NULL path.\n");\r
202 +       log_to_string (&message, "Error: Cannot open a database for a NULL path.\n");\r
203         status = NOTMUCH_STATUS_NULL_POINTER;\r
204         goto DONE;\r
205      }\r
206  \r
207      if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {\r
208 -       fprintf (stderr, "Out of memory\n");\r
209 +       log_to_string (&message, "Out of memory\n");\r
210         status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
211         goto DONE;\r
212      }\r
213  \r
214      err = stat (notmuch_path, &st);\r
215      if (err) {\r
216 -       fprintf (stderr, "Error opening database at %s: %s\n",\r
217 +       log_to_string (&message, "Error opening database at %s: %s\n",\r
218                  notmuch_path, strerror (errno));\r
219         status = NOTMUCH_STATUS_FILE_ERROR;\r
220         goto DONE;\r
221      }\r
222  \r
223      if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {\r
224 -       fprintf (stderr, "Out of memory\n");\r
225 +       log_to_string (&message, "Out of memory\n");\r
226         status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
227         goto DONE;\r
228      }\r
229 @@ -830,7 +880,7 @@ notmuch_database_open (const char *path,\r
230          * means a dramatically incompatible change. */\r
231         version = notmuch_database_get_version (notmuch);\r
232         if (version > NOTMUCH_DATABASE_VERSION) {\r
233 -           fprintf (stderr,\r
234 +           log_to_string (&message,\r
235                      "Error: Notmuch database at %s\n"\r
236                      "       has a newer database format version (%u) than supported by this\n"\r
237                      "       version of notmuch (%u).\n",\r
238 @@ -849,7 +899,7 @@ notmuch_database_open (const char *path,\r
239             version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',\r
240             &incompat_features);\r
241         if (incompat_features) {\r
242 -           fprintf (stderr,\r
243 +           log_to_string (&message,\r
244                      "Error: Notmuch database at %s\n"\r
245                      "       requires features (%s)\n"\r
246                      "       not supported by this version of notmuch.\n",\r
247 @@ -899,7 +949,7 @@ notmuch_database_open (const char *path,\r
248             notmuch->query_parser->add_prefix (prefix->name, prefix->prefix);\r
249         }\r
250      } catch (const Xapian::Error &error) {\r
251 -       fprintf (stderr, "A Xapian exception occurred opening database: %s\n",\r
252 +       log_to_string (&message, "A Xapian exception occurred opening database: %s\n",\r
253                  error.get_msg().c_str());\r
254         notmuch_database_destroy (notmuch);\r
255         notmuch = NULL;\r
256 @@ -909,6 +959,9 @@ notmuch_database_open (const char *path,\r
257    DONE:\r
258      talloc_free (local);\r
259  \r
260 +    if (status_string && message)\r
261 +       *status_string = strdup (message);\r
262 +\r
263      if (database)\r
264         *database = notmuch;\r
265      else\r
266 @@ -1032,13 +1085,18 @@ notmuch_database_compact (const char *path,\r
267      notmuch_database_t *notmuch = NULL;\r
268      struct stat statbuf;\r
269      notmuch_bool_t keep_backup;\r
270 +    char *message = NULL;\r
271  \r
272      local = talloc_new (NULL);\r
273      if (! local)\r
274         return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
275  \r
276 -    ret = notmuch_database_open (path, NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch);\r
277 +    ret = notmuch_database_open_verbose (path,\r
278 +                                        NOTMUCH_DATABASE_MODE_READ_WRITE,\r
279 +                                        &notmuch,\r
280 +                                        &message);\r
281      if (ret) {\r
282 +       if (status_cb) status_cb (message, closure);\r
283         goto DONE;\r
284      }\r
285  \r
286 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
287 index 220839b..0dfac8f 100644\r
288 --- a/lib/notmuch.h\r
289 +++ b/lib/notmuch.h\r
290 @@ -230,6 +230,16 @@ notmuch_status_t\r
291  notmuch_database_create (const char *path, notmuch_database_t **database);\r
292  \r
293  /**\r
294 + * Like notmuch_database_create, except optionally return an error\r
295 + * message. This message is allocated by malloc and should be freed by\r
296 + * the caller.\r
297 + */\r
298 +notmuch_status_t\r
299 +notmuch_database_create_verbose (const char *path,\r
300 +                                notmuch_database_t **database,\r
301 +                                char **error_message);\r
302 +\r
303 +/**\r
304   * Database open mode for notmuch_database_open.\r
305   */\r
306  typedef enum {\r
307 @@ -279,6 +289,17 @@ notmuch_status_t\r
308  notmuch_database_open (const char *path,\r
309                        notmuch_database_mode_t mode,\r
310                        notmuch_database_t **database);\r
311 +/**\r
312 + * Like notmuch_database_open, except optionally return an error\r
313 + * message. This message is allocated by malloc and should be freed by\r
314 + * the caller.\r
315 + */\r
316 +\r
317 +notmuch_status_t\r
318 +notmuch_database_open_verbose (const char *path,\r
319 +                              notmuch_database_mode_t mode,\r
320 +                              notmuch_database_t **database,\r
321 +                              char **error_message);\r
322  \r
323  /**\r
324   * Commit changes and close the given notmuch database.\r
325 diff --git a/notmuch-new.c b/notmuch-new.c\r
326 index ddf42c1..93b70bf 100644\r
327 --- a/notmuch-new.c\r
328 +++ b/notmuch-new.c\r
329 @@ -985,9 +985,13 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
330             return EXIT_FAILURE;\r
331         add_files_state.total_files = count;\r
332      } else {\r
333 -       if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,\r
334 -                                  &notmuch))\r
335 +       char *status_string = NULL;\r
336 +       if (notmuch_database_open_verbose (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,\r
337 +                                          &notmuch, &status_string)) {\r
338 +           if (status_string) fputs (status_string, stderr);\r
339 +\r
340             return EXIT_FAILURE;\r
341 +       }\r
342  \r
343         if (notmuch_database_needs_upgrade (notmuch)) {\r
344             time_t now = time (NULL);\r
345 diff --git a/notmuch-search.c b/notmuch-search.c\r
346 index 14b9f01..250e567 100644\r
347 --- a/notmuch-search.c\r
348 +++ b/notmuch-search.c\r
349 @@ -545,6 +545,7 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar\r
350  {\r
351      char *query_str;\r
352      unsigned int i;\r
353 +    char *status_string = NULL;\r
354  \r
355      switch (ctx->format_sel) {\r
356      case NOTMUCH_FORMAT_TEXT:\r
357 @@ -570,9 +571,12 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar\r
358  \r
359      notmuch_exit_if_unsupported_format ();\r
360  \r
361 -    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
362 -                              NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))\r
363 +    if (notmuch_database_open_verbose (\r
364 +           notmuch_config_get_database_path (config),\r
365 +           NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {\r
366 +       if (status_string) fputs (status_string, stderr);\r
367         return EXIT_FAILURE;\r
368 +    }\r
369  \r
370      query_str = query_string_from_args (ctx->notmuch, argc, argv);\r
371      if (query_str == NULL) {\r
372 diff --git a/test/symbol-test.cc b/test/symbol-test.cc\r
373 index 3e96c03..9f8eea7 100644\r
374 --- a/test/symbol-test.cc\r
375 +++ b/test/symbol-test.cc\r
376 @@ -5,7 +5,11 @@\r
377  \r
378  int main() {\r
379    notmuch_database_t *notmuch;\r
380 -  notmuch_database_open("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch);\r
381 +  char *message = NULL;\r
382 +\r
383 +  if (notmuch_database_open_verbose  ("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch, &message))\r
384 +      if (message) fputs (message, stderr);\r
385 +\r
386  \r
387    try {\r
388      (void) new Xapian::WritableDatabase("./nonexistant", Xapian::DB_OPEN);\r
389 -- \r
390 2.1.3\r
391 \r