have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
authorTero Marttila <terom@fixme.fi>
Sun, 12 Apr 2009 18:56:51 +0300
changeset 135 9159bd51525f
parent 134 978041c1c04d
child 136 81dbeb5bc38e
have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
src/irc_log.sql
src/modules/irc_log.sql
src/modules/logwatch.c
src/modules/logwatch.h
src/modules/logwatch_filter.c
--- a/src/irc_log.sql	Sun Apr 12 18:35:16 2009 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-
-CREATE TABLE logs (
-    id          serial,
-    network     varchar(32)     NOT NULL,
-    channel     varchar(32)     NOT NULL,
-    ts          timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,       -- UTC time
-    nickname    varchar(32),                    -- source nickname, NULL for "meta" events
-    username    varchar(32),                    -- source username, NULL for "meta" events
-    hostname    varchar(64),                    -- source hostname, NULL for "meta" events
-    type        varchar(16)     NOT NULL,       -- event type
-    target      varchar(32),                    -- optional target
-    message     varchar(512)                    -- optional event data
-);
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/modules/irc_log.sql	Sun Apr 12 18:56:51 2009 +0300
@@ -0,0 +1,14 @@
+
+CREATE TABLE logs (
+    id          serial,
+    network     varchar(32)     NOT NULL,
+    channel     varchar(32)     NOT NULL,
+    ts          timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,       -- UTC time
+    nickname    varchar(32),                    -- source nickname, NULL for "meta" events
+    username    varchar(32),                    -- source username, NULL for "meta" events
+    hostname    varchar(64),                    -- source hostname, NULL for "meta" events
+    type        varchar(16)     NOT NULL,       -- event type
+    target      varchar(32),                    -- optional target
+    message     varchar(512)                    -- optional event data
+);
+
--- a/src/modules/logwatch.c	Sun Apr 12 18:35:16 2009 +0300
+++ b/src/modules/logwatch.c	Sun Apr 12 18:56:51 2009 +0300
@@ -112,18 +112,24 @@
     format      = config_get_string     (option, values, "format"   );
     irc_chan    = config_get_irc_chan   (option, values, "chan"     );
 
-    // lookup name
+    // replace old filter?
+    if ((filter = logwatch_filter_lookup(ctx, name))) {
+        log_info("removing old filter: %s: %p", name, filter);
+        logwatch_filter_destroy(filter);
+    }
+
+    // lookup source
     if (source_name && (source = logwatch_source_lookup(ctx, source_name)) == NULL)
        RETURN_SET_ERROR_STR(err, ERR_MODULE_CONF, "unknown logwatch_source name");
     
-    // lookup the channel
+    // lookup channel
     if ((chan = logwatch_get_chan(ctx, irc_chan)) == NULL)
         RETURN_SET_ERROR_STR(err, ERR_MODULE_CONF, "logwatch_get_chan failed");
     
     log_info("add filter: name=%s, source=%s, pattern=%s, format=%s, chan=%s",
         name, source_name, pattern, format, irc_chan_name(irc_chan));
 
-    // invoke
+    // create a new one
     if ((filter = logwatch_filter(ctx, name, source, pattern, format, chan, err)) == NULL)
         goto error;
 
@@ -146,7 +152,8 @@
 
     CONFIG_OPT(         "filter",       logwatch_conf_filter, 
         "filter lines from source (or all), using the given regular expression (or all lines), and format output "
-        "using the given format expression (or pass through directly), and finally send the output to the given channel",
+        "using the given format expression (or pass through directly), and finally send the output to the given channel. "
+        "If a filter with the same name already exists, it will be replaced.",
 
         CONFIG_PARAM(       "name",    CONFIG_STRING,      "filter name",                              false   ),
         CONFIG_PARAM(       "source",  CONFIG_STRING,      "optional logwatch_source to use",          true    ),
--- a/src/modules/logwatch.h	Sun Apr 12 18:35:16 2009 +0300
+++ b/src/modules/logwatch.h	Sun Apr 12 18:56:51 2009 +0300
@@ -176,6 +176,11 @@
 struct logwatch_chan* logwatch_get_chan (struct logwatch *ctx, struct irc_chan *irc_chan);
 
 /**
+ * Lookup an existing filter by name.
+ */
+struct logwatch_filter* logwatch_filter_lookup (struct logwatch *ctx, const char *name);
+
+/**
  * Add a new filter.
  *
  * The given logwatch_chan should be a new reference.
--- a/src/modules/logwatch_filter.c	Sun Apr 12 18:35:16 2009 +0300
+++ b/src/modules/logwatch_filter.c	Sun Apr 12 18:56:51 2009 +0300
@@ -7,6 +7,19 @@
 
 #include <assert.h>
 
+struct logwatch_filter* logwatch_filter_lookup (struct logwatch *ctx, const char *name)
+{
+    struct logwatch_filter *filter;
+    
+    // inspect each filter
+    TAILQ_FOREACH(filter, &ctx->filters, logwatch_filters)
+        if (strcmp(filter->name, name) == 0)
+            return filter;
+    
+    // not found
+    return NULL;
+}
+
 struct logwatch_filter* logwatch_filter (struct logwatch *ctx, const char *name, const struct logwatch_source *source,
         const char *pattern, const char *format, struct logwatch_chan *chan, struct error_info *err)
 {