[Patch v4 4/9] lib: add "verbose" versions of notmuch_database_{open, create}

2015-03-21 Thread Tomi Ollila
On Sat, Mar 14 2015, David Bremner  wrote:

> The compatibility wrapper ensures that clients calling
> notmuch_database_open will receive consistent output for now.
>
> The stdargs based infrastucture will be used in following commits for

infrastructure :D

> a more general logging mechanism.
>
> The changes to notmuch-{new,search} and test/symbol-test are just to
> make the test suite pass.
> ---
>  lib/database.cc | 96 
> -
>  lib/notmuch.h   | 21 
>  notmuch-new.c   |  8 +++--
>  notmuch-search.c|  8 +++--
>  test/symbol-test.cc |  6 +++-
>  5 files changed, 119 insertions(+), 20 deletions(-)
>
> diff --git a/lib/database.cc b/lib/database.cc
> index 3974e2e..b774edc 100644
> --- a/lib/database.cc
> +++ b/lib/database.cc
> @@ -349,6 +349,35 @@ notmuch_status_to_string (notmuch_status_t status)
>  }
>  
>  static void
> +vlog_to_string (void *ctx,
> +char **status_string,
> +const char *format,
> +va_list va_args)
> +{
> +if (!status_string)
> + return;

Noticed just before sending: (! status_string) -- and one more deep down
below... 

> +
> +if (*status_string)
> + talloc_free (*status_string);
> +
> +*status_string = talloc_vasprintf (ctx, format, va_args);
> +}
> +
> +static void
> +log_to_string (char **str,
> +const char *format,
> +...)
> +{
> +va_list va_args;
> +
> +va_start (va_args, format);
> +
> +vlog_to_string (NULL, str, format, va_args);
> +
> +va_end (va_args);
> +}


To me it looks a bit peculiar log_to_string () not taking `ctx` as first
argument. I'd suggest

1) add that ctx to first arg and tediously add NULL to all callers

or 

2) rename the function so something less similar and call that instead
(even leading _ could "mark" the function to be less generic interface to
vlog_to_string) .


Otherwise this (and rest of the series) looks pretty good to me.

Tomi


> +static void
>  find_doc_ids_for_term (notmuch_database_t *notmuch,
>  const char *term,
>  Xapian::PostingIterator *begin,
> @@ -608,28 +637,37 @@ parse_references (void *ctx,
>  notmuch_status_t
>  notmuch_database_create (const char *path, notmuch_database_t **database)
>  {
> +return notmuch_database_create_verbose (path, database, NULL);
> +}
> +
> +notmuch_status_t
> +notmuch_database_create_verbose (const char *path,
> +  notmuch_database_t **database,
> +  char **status_string)
> +{
>  notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
>  notmuch_database_t *notmuch = NULL;
>  char *notmuch_path = NULL;
> +char *message = NULL;
>  struct stat st;
>  int err;
>  
>  if (path == NULL) {
> - fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
> + log_to_string (, "Error: Cannot create a database for a NULL 
> path.\n");
>   status = NOTMUCH_STATUS_NULL_POINTER;
>   goto DONE;
>  }
>  
>  err = stat (path, );
>  if (err) {
> - fprintf (stderr, "Error: Cannot create database at %s: %s.\n",
> + log_to_string (, "Error: Cannot create database at %s: %s.\n",
>path, strerror (errno));
>   status = NOTMUCH_STATUS_FILE_ERROR;
>   goto DONE;
>  }
>  
>  if (! S_ISDIR (st.st_mode)) {
> - fprintf (stderr, "Error: Cannot create database at %s: Not a 
> directory.\n",
> + log_to_string (, "Error: Cannot create database at %s: Not a 
> directory.\n",
>path);
>   status = NOTMUCH_STATUS_FILE_ERROR;
>   goto DONE;
> @@ -640,15 +678,15 @@ notmuch_database_create (const char *path, 
> notmuch_database_t **database)
>  err = mkdir (notmuch_path, 0755);
>  
>  if (err) {
> - fprintf (stderr, "Error: Cannot create directory %s: %s.\n",
> + log_to_string (, "Error: Cannot create directory %s: %s.\n",
>notmuch_path, strerror (errno));
>   status = NOTMUCH_STATUS_FILE_ERROR;
>   goto DONE;
>  }
>  
> -status = notmuch_database_open (path,
> - NOTMUCH_DATABASE_MODE_READ_WRITE,
> - );
> +status = notmuch_database_open_verbose (path,
> + NOTMUCH_DATABASE_MODE_READ_WRITE,
> + , );
>  if (status)
>   goto DONE;
>  
> @@ -667,6 +705,8 @@ notmuch_database_create (const char *path, 
> notmuch_database_t **database)
>  if (notmuch_path)
>   talloc_free (notmuch_path);
>  
> +if (message)
> + *status_string = strdup(message);

strdup (message);

>  if (database)
>   *database = notmuch;
>  else
> @@ -767,37 +807,55 @@ notmuch_database_open (const char *path,
>  notmuch_database_mode_t mode,
>  notmuch_database_t **database)
>  {
> +char *status_string = NULL;
> +  

Re: [Patch v4 4/9] lib: add verbose versions of notmuch_database_{open, create}

2015-03-21 Thread Tomi Ollila
On Sat, Mar 14 2015, David Bremner da...@tethera.net wrote:

 The compatibility wrapper ensures that clients calling
 notmuch_database_open will receive consistent output for now.

 The stdargs based infrastucture will be used in following commits for

infrastructure :D

 a more general logging mechanism.

 The changes to notmuch-{new,search} and test/symbol-test are just to
 make the test suite pass.
 ---
  lib/database.cc | 96 
 -
  lib/notmuch.h   | 21 
  notmuch-new.c   |  8 +++--
  notmuch-search.c|  8 +++--
  test/symbol-test.cc |  6 +++-
  5 files changed, 119 insertions(+), 20 deletions(-)

 diff --git a/lib/database.cc b/lib/database.cc
 index 3974e2e..b774edc 100644
 --- a/lib/database.cc
 +++ b/lib/database.cc
 @@ -349,6 +349,35 @@ notmuch_status_to_string (notmuch_status_t status)
  }
  
  static void
 +vlog_to_string (void *ctx,
 +char **status_string,
 +const char *format,
 +va_list va_args)
 +{
 +if (!status_string)
 + return;

Noticed just before sending: (! status_string) -- and one more deep down
below... 

 +
 +if (*status_string)
 + talloc_free (*status_string);
 +
 +*status_string = talloc_vasprintf (ctx, format, va_args);
 +}
 +
 +static void
 +log_to_string (char **str,
 +const char *format,
 +...)
 +{
 +va_list va_args;
 +
 +va_start (va_args, format);
 +
 +vlog_to_string (NULL, str, format, va_args);
 +
 +va_end (va_args);
 +}


To me it looks a bit peculiar log_to_string () not taking `ctx` as first
argument. I'd suggest

1) add that ctx to first arg and tediously add NULL to all callers

or 

2) rename the function so something less similar and call that instead
(even leading _ could mark the function to be less generic interface to
vlog_to_string) .


Otherwise this (and rest of the series) looks pretty good to me.

Tomi


 +static void
  find_doc_ids_for_term (notmuch_database_t *notmuch,
  const char *term,
  Xapian::PostingIterator *begin,
 @@ -608,28 +637,37 @@ parse_references (void *ctx,
  notmuch_status_t
  notmuch_database_create (const char *path, notmuch_database_t **database)
  {
 +return notmuch_database_create_verbose (path, database, NULL);
 +}
 +
 +notmuch_status_t
 +notmuch_database_create_verbose (const char *path,
 +  notmuch_database_t **database,
 +  char **status_string)
 +{
  notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
  notmuch_database_t *notmuch = NULL;
  char *notmuch_path = NULL;
 +char *message = NULL;
  struct stat st;
  int err;
  
  if (path == NULL) {
 - fprintf (stderr, Error: Cannot create a database for a NULL path.\n);
 + log_to_string (message, Error: Cannot create a database for a NULL 
 path.\n);
   status = NOTMUCH_STATUS_NULL_POINTER;
   goto DONE;
  }
  
  err = stat (path, st);
  if (err) {
 - fprintf (stderr, Error: Cannot create database at %s: %s.\n,
 + log_to_string (message, Error: Cannot create database at %s: %s.\n,
path, strerror (errno));
   status = NOTMUCH_STATUS_FILE_ERROR;
   goto DONE;
  }
  
  if (! S_ISDIR (st.st_mode)) {
 - fprintf (stderr, Error: Cannot create database at %s: Not a 
 directory.\n,
 + log_to_string (message, Error: Cannot create database at %s: Not a 
 directory.\n,
path);
   status = NOTMUCH_STATUS_FILE_ERROR;
   goto DONE;
 @@ -640,15 +678,15 @@ notmuch_database_create (const char *path, 
 notmuch_database_t **database)
  err = mkdir (notmuch_path, 0755);
  
  if (err) {
 - fprintf (stderr, Error: Cannot create directory %s: %s.\n,
 + log_to_string (message, Error: Cannot create directory %s: %s.\n,
notmuch_path, strerror (errno));
   status = NOTMUCH_STATUS_FILE_ERROR;
   goto DONE;
  }
  
 -status = notmuch_database_open (path,
 - NOTMUCH_DATABASE_MODE_READ_WRITE,
 - notmuch);
 +status = notmuch_database_open_verbose (path,
 + NOTMUCH_DATABASE_MODE_READ_WRITE,
 + notmuch, message);
  if (status)
   goto DONE;
  
 @@ -667,6 +705,8 @@ notmuch_database_create (const char *path, 
 notmuch_database_t **database)
  if (notmuch_path)
   talloc_free (notmuch_path);
  
 +if (message)
 + *status_string = strdup(message);

strdup (message);

  if (database)
   *database = notmuch;
  else
 @@ -767,37 +807,55 @@ notmuch_database_open (const char *path,
  notmuch_database_mode_t mode,
  notmuch_database_t **database)
  {
 +char *status_string = NULL;
 +notmuch_status_t status;
 +
 +status = notmuch_database_open_verbose(path, mode, 

[Patch v4 4/9] lib: add "verbose" versions of notmuch_database_{open,create}

2015-03-14 Thread David Bremner
The compatibility wrapper ensures that clients calling
notmuch_database_open will receive consistent output for now.

The stdargs based infrastucture will be used in following commits for
a more general logging mechanism.

The changes to notmuch-{new,search} and test/symbol-test are just to
make the test suite pass.
---
 lib/database.cc | 96 -
 lib/notmuch.h   | 21 
 notmuch-new.c   |  8 +++--
 notmuch-search.c|  8 +++--
 test/symbol-test.cc |  6 +++-
 5 files changed, 119 insertions(+), 20 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index 3974e2e..b774edc 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -349,6 +349,35 @@ notmuch_status_to_string (notmuch_status_t status)
 }

 static void
+vlog_to_string (void *ctx,
+  char **status_string,
+  const char *format,
+  va_list va_args)
+{
+if (!status_string)
+   return;
+
+if (*status_string)
+   talloc_free (*status_string);
+
+*status_string = talloc_vasprintf (ctx, format, va_args);
+}
+
+static void
+log_to_string (char **str,
+  const char *format,
+  ...)
+{
+va_list va_args;
+
+va_start (va_args, format);
+
+vlog_to_string (NULL, str, format, va_args);
+
+va_end (va_args);
+}
+
+static void
 find_doc_ids_for_term (notmuch_database_t *notmuch,
   const char *term,
   Xapian::PostingIterator *begin,
@@ -608,28 +637,37 @@ parse_references (void *ctx,
 notmuch_status_t
 notmuch_database_create (const char *path, notmuch_database_t **database)
 {
+return notmuch_database_create_verbose (path, database, NULL);
+}
+
+notmuch_status_t
+notmuch_database_create_verbose (const char *path,
+notmuch_database_t **database,
+char **status_string)
+{
 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
 notmuch_database_t *notmuch = NULL;
 char *notmuch_path = NULL;
+char *message = NULL;
 struct stat st;
 int err;

 if (path == NULL) {
-   fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
+   log_to_string (, "Error: Cannot create a database for a NULL 
path.\n");
status = NOTMUCH_STATUS_NULL_POINTER;
goto DONE;
 }

 err = stat (path, );
 if (err) {
-   fprintf (stderr, "Error: Cannot create database at %s: %s.\n",
+   log_to_string (, "Error: Cannot create database at %s: %s.\n",
 path, strerror (errno));
status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
 }

 if (! S_ISDIR (st.st_mode)) {
-   fprintf (stderr, "Error: Cannot create database at %s: Not a 
directory.\n",
+   log_to_string (, "Error: Cannot create database at %s: Not a 
directory.\n",
 path);
status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
@@ -640,15 +678,15 @@ notmuch_database_create (const char *path, 
notmuch_database_t **database)
 err = mkdir (notmuch_path, 0755);

 if (err) {
-   fprintf (stderr, "Error: Cannot create directory %s: %s.\n",
+   log_to_string (, "Error: Cannot create directory %s: %s.\n",
 notmuch_path, strerror (errno));
status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
 }

-status = notmuch_database_open (path,
-   NOTMUCH_DATABASE_MODE_READ_WRITE,
-   );
+status = notmuch_database_open_verbose (path,
+   NOTMUCH_DATABASE_MODE_READ_WRITE,
+   , );
 if (status)
goto DONE;

@@ -667,6 +705,8 @@ notmuch_database_create (const char *path, 
notmuch_database_t **database)
 if (notmuch_path)
talloc_free (notmuch_path);

+if (message)
+   *status_string = strdup(message);
 if (database)
*database = notmuch;
 else
@@ -767,37 +807,55 @@ notmuch_database_open (const char *path,
   notmuch_database_mode_t mode,
   notmuch_database_t **database)
 {
+char *status_string = NULL;
+notmuch_status_t status;
+
+status = notmuch_database_open_verbose(path, mode, database,
+  _string);
+
+if (status_string) fputs(status_string, stderr);
+
+return status;
+}
+
+notmuch_status_t
+notmuch_database_open_verbose (const char *path,
+  notmuch_database_mode_t mode,
+  notmuch_database_t **database,
+  char **status_string)
+{
 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
 void *local = talloc_new (NULL);
 notmuch_database_t *notmuch = NULL;
 char *notmuch_path, *xapian_path, *incompat_features;
+char *message = NULL;
 struct stat st;
 int err;
 unsigned int i, version;
 static 

[Patch v4 4/9] lib: add verbose versions of notmuch_database_{open,create}

2015-03-14 Thread David Bremner
The compatibility wrapper ensures that clients calling
notmuch_database_open will receive consistent output for now.

The stdargs based infrastucture will be used in following commits for
a more general logging mechanism.

The changes to notmuch-{new,search} and test/symbol-test are just to
make the test suite pass.
---
 lib/database.cc | 96 -
 lib/notmuch.h   | 21 
 notmuch-new.c   |  8 +++--
 notmuch-search.c|  8 +++--
 test/symbol-test.cc |  6 +++-
 5 files changed, 119 insertions(+), 20 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index 3974e2e..b774edc 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -349,6 +349,35 @@ notmuch_status_to_string (notmuch_status_t status)
 }
 
 static void
+vlog_to_string (void *ctx,
+  char **status_string,
+  const char *format,
+  va_list va_args)
+{
+if (!status_string)
+   return;
+
+if (*status_string)
+   talloc_free (*status_string);
+
+*status_string = talloc_vasprintf (ctx, format, va_args);
+}
+
+static void
+log_to_string (char **str,
+  const char *format,
+  ...)
+{
+va_list va_args;
+
+va_start (va_args, format);
+
+vlog_to_string (NULL, str, format, va_args);
+
+va_end (va_args);
+}
+
+static void
 find_doc_ids_for_term (notmuch_database_t *notmuch,
   const char *term,
   Xapian::PostingIterator *begin,
@@ -608,28 +637,37 @@ parse_references (void *ctx,
 notmuch_status_t
 notmuch_database_create (const char *path, notmuch_database_t **database)
 {
+return notmuch_database_create_verbose (path, database, NULL);
+}
+
+notmuch_status_t
+notmuch_database_create_verbose (const char *path,
+notmuch_database_t **database,
+char **status_string)
+{
 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
 notmuch_database_t *notmuch = NULL;
 char *notmuch_path = NULL;
+char *message = NULL;
 struct stat st;
 int err;
 
 if (path == NULL) {
-   fprintf (stderr, Error: Cannot create a database for a NULL path.\n);
+   log_to_string (message, Error: Cannot create a database for a NULL 
path.\n);
status = NOTMUCH_STATUS_NULL_POINTER;
goto DONE;
 }
 
 err = stat (path, st);
 if (err) {
-   fprintf (stderr, Error: Cannot create database at %s: %s.\n,
+   log_to_string (message, Error: Cannot create database at %s: %s.\n,
 path, strerror (errno));
status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
 }
 
 if (! S_ISDIR (st.st_mode)) {
-   fprintf (stderr, Error: Cannot create database at %s: Not a 
directory.\n,
+   log_to_string (message, Error: Cannot create database at %s: Not a 
directory.\n,
 path);
status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
@@ -640,15 +678,15 @@ notmuch_database_create (const char *path, 
notmuch_database_t **database)
 err = mkdir (notmuch_path, 0755);
 
 if (err) {
-   fprintf (stderr, Error: Cannot create directory %s: %s.\n,
+   log_to_string (message, Error: Cannot create directory %s: %s.\n,
 notmuch_path, strerror (errno));
status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
 }
 
-status = notmuch_database_open (path,
-   NOTMUCH_DATABASE_MODE_READ_WRITE,
-   notmuch);
+status = notmuch_database_open_verbose (path,
+   NOTMUCH_DATABASE_MODE_READ_WRITE,
+   notmuch, message);
 if (status)
goto DONE;
 
@@ -667,6 +705,8 @@ notmuch_database_create (const char *path, 
notmuch_database_t **database)
 if (notmuch_path)
talloc_free (notmuch_path);
 
+if (message)
+   *status_string = strdup(message);
 if (database)
*database = notmuch;
 else
@@ -767,37 +807,55 @@ notmuch_database_open (const char *path,
   notmuch_database_mode_t mode,
   notmuch_database_t **database)
 {
+char *status_string = NULL;
+notmuch_status_t status;
+
+status = notmuch_database_open_verbose(path, mode, database,
+  status_string);
+
+if (status_string) fputs(status_string, stderr);
+
+return status;
+}
+
+notmuch_status_t
+notmuch_database_open_verbose (const char *path,
+  notmuch_database_mode_t mode,
+  notmuch_database_t **database,
+  char **status_string)
+{
 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
 void *local = talloc_new (NULL);
 notmuch_database_t *notmuch = NULL;
 char *notmuch_path, *xapian_path, *incompat_features;
+char *message = NULL;
 struct stat st;