[PATCH 3/3] notmuch-tag.c: Add tag logging.

david at tethera.net david at tethera.net
Mon Oct 11 06:26:57 PDT 2010


From: David Bremner <bremner at unb.ca>

logging of tags is enabled by adding a stanza like

[log]
tags = /some/path/you/can/write/to

to your notmuch config.

Note that we intentionally do the logging after the database
transaction is finished.
---
 notmuch-tag.c |   31 +++++++++++++++++++++++++++++++
 1 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/notmuch-tag.c b/notmuch-tag.c
index fd54bc7..f24d1e4 100644
--- a/notmuch-tag.c
+++ b/notmuch-tag.c
@@ -46,6 +46,9 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
     notmuch_message_t *message;
     struct sigaction action;
     int i;
+    int log_fd = -1;
+    notmuch_bool_t enable_log = FALSE;
+    const char *log_path;
 
     /* Setup our handler for SIGINT */
     memset (&action, 0, sizeof (struct sigaction));
@@ -96,6 +99,17 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
     if (config == NULL)
 	return 1;
 
+    log_path = notmuch_config_get_log_path (config, "tags");
+
+    if (log_path != NULL) {
+      enable_log = TRUE;
+
+      log_fd = notmuch_log_open(log_path);
+
+      if (log_fd < 0)
+	return 1;
+    }
+
     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
 				     NOTMUCH_DATABASE_MODE_READ_WRITE);
     if (notmuch == NULL)
@@ -114,10 +128,15 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
 	 notmuch_messages_valid (messages) && !interrupted;
 	 notmuch_messages_move_to_next (messages))
     {
+	const char *message_id = NULL;
 	message = notmuch_messages_get (messages);
 
 	notmuch_message_freeze (message);
 
+	if (enable_log)
+	    message_id = talloc_strdup (ctx,
+					notmuch_message_get_message_id (message));
+
 	for (i = 0; i < remove_tags_count; i++)
 	    notmuch_message_remove_tag (message,
 					argv[remove_tags[i]] + 1);
@@ -128,6 +147,18 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
 	notmuch_message_thaw (message);
 
 	notmuch_message_destroy (message);
+
+	if (enable_log) {
+	    for (i = 0; i < remove_tags_count; i++)
+		notmuch_log_string_pair (ctx, log_fd,
+					 message_id,
+					 argv[remove_tags[i]]);
+
+	    for (i = 0; i < add_tags_count; i++)
+		notmuch_log_string_pair (ctx, log_fd,
+					 message_id,
+					 argv[add_tags[i]]);
+	}
     }
 
     notmuch_query_destroy (query);
-- 
1.7.1



More information about the notmuch mailing list