Re: [PATCH v6 4/6] emacs/mua: Generate improved cited text for replies
[notmuch-archives.git] / ca / cb9ae4fa383bc15085d81abcd7f18f6a712bb9
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
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 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
26         22:05:48 -0000\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
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: Sat, 27 Dec 2014 22:06:11 -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     | 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
66 \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
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,7 +672,7 @@ 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 @@ -660,6 +698,8 @@ notmuch_database_create (const char *path, notmuch_database_t **database)\r
158      if (notmuch_path)\r
159         talloc_free (notmuch_path);\r
160  \r
161 +    if (message)\r
162 +       *status_string = strdup(message);\r
163      if (database)\r
164         *database = notmuch;\r
165      else\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
169  {\r
170 +    return notmuch_database_open_verbose(path, mode, database, NULL);\r
171 +}\r
172 +\r
173 +notmuch_status_t\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
178 +{\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
184      struct stat st;\r
185      int err;\r
186      unsigned int i, version;\r
187      static int initialized = 0;\r
188  \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
193         goto DONE;\r
194      }\r
195  \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
200         goto DONE;\r
201      }\r
202  \r
203      err = stat (notmuch_path, &st);\r
204      if (err) {\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
209         goto DONE;\r
210      }\r
211  \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
216         goto DONE;\r
217      }\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
222 -           fprintf (stderr,\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
231 -           fprintf (stderr,\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
238         }\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
244         notmuch = NULL;\r
245 @@ -909,6 +959,9 @@ notmuch_database_open (const char *path,\r
246    DONE:\r
247      talloc_free (local);\r
248  \r
249 +    if (status_string && message)\r
250 +       *status_string = strdup (message);\r
251 +\r
252      if (database)\r
253         *database = notmuch;\r
254      else\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
261  \r
262  /**\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
265 + * the caller.\r
266 + */\r
267 +notmuch_status_t\r
268 +notmuch_database_create_verbose (const char *path,\r
269 +                                notmuch_database_t **database,\r
270 +                                char **error_message);\r
271 +\r
272 +/**\r
273   * Database open mode for notmuch_database_open.\r
274   */\r
275  typedef enum {\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
280 +/**\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
283 + * the caller.\r
284 + */\r
285 +\r
286 +notmuch_status_t\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
291  \r
292  /**\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
301      } else {\r
302 -       if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,\r
303 -                                  &notmuch))\r
304 +       char *status_string = NULL;\r
305 +       if (notmuch_database_open_verbose (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,\r
306 +                                          &notmuch, &status_string)) {\r
307 +           if (status_string) fputs (status_string, stderr);\r
308 +\r
309             return EXIT_FAILURE;\r
310 +       }\r
311  \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
319  {\r
320      char *query_str;\r
321      unsigned int i;\r
322 +    char *status_string = NULL;\r
323  \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
327  \r
328      notmuch_exit_if_unsupported_format ();\r
329  \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
337 +    }\r
338  \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
345 @@ -5,7 +5,11 @@\r
346  \r
347  int main() {\r
348    notmuch_database_t *notmuch;\r
349 -  notmuch_database_open("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch);\r
350 +  char *message = NULL;\r
351 +\r
352 +  if (notmuch_database_open_verbose  ("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch, &message))\r
353 +      if (message) fputs (message, stderr);\r
354 +\r
355  \r
356    try {\r
357      (void) new Xapian::WritableDatabase("./nonexistant", Xapian::DB_OPEN);\r
358 -- \r
359 2.1.3\r
360 \r