src/modules/logwatch.c
author Tero Marttila <terom@fixme.fi>
Wed, 27 May 2009 23:07:00 +0300
branchnew-lib-errors
changeset 216 a10ba529ae39
parent 138 a716c621cb90
permissions -rw-r--r--
initial error code
119
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "logwatch.h"
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
#include "../config.h"
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
     4
#include "../log.h"
119
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
     6
#include <assert.h>
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
     7
119
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
static err_t logwatch_init (struct nexus *nexus, void **ctx_ptr, struct error_info *err)
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
{
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    struct logwatch *ctx;
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    // allocate
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
        RETURN_SET_ERROR(err, ERR_CALLOC);
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    // init
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    TAILQ_INIT(&ctx->sources);
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
    TAILQ_INIT(&ctx->filters);
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    TAILQ_INIT(&ctx->channels);
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    // store
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    ctx->nexus = nexus;
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
    // ok
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
    *ctx_ptr = ctx;
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    return SUCCESS;
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
}
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    30
struct logwatch_chan* logwatch_get_chan (struct logwatch *ctx, struct irc_chan *irc_chan)
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    31
{
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    32
    struct logwatch_chan *chan;
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    33
    
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    34
    // existing?
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    35
    if ((chan = logwatch_chan_lookup(ctx, irc_chan))) {
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    36
        // get ref
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    37
        chan->refcount++;
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    38
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    39
    } else {
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    40
        // create a new one with a ref
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    41
        // XXX: errors...
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    42
        chan = logwatch_chan_create(ctx, irc_chan);
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    43
    }
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    44
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    45
    // ok
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    46
    return chan;
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    47
}
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    48
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
    49
void logwatch_on_line (struct logwatch *ctx, const struct logwatch_source *source, const char *line)
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
    50
{
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
    51
    const struct logwatch_filter *filter;
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
    52
    struct error_info err;
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
    53
    int ret;
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
    54
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
    55
    // apply each filter
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
    56
    TAILQ_FOREACH(filter, &ctx->filters, logwatch_filters) {
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
    57
        // apply it
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
    58
        if ((ret = logwatch_filter_apply(filter, source, line, &err)) < 0)
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    59
            log_warn("logwatch_filter_apply(%s, %s, %s): %s", filter->name, source->name, line, error_msg(&err));
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
    60
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
    61
        // blackhole?
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
    62
        if (ret > 0)
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
    63
            break;
127
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
    64
    }
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
    65
}
94e6c3b4230f implement logwatch_on_line/logwatch_filter_apply, along with irc_*_NOTICE
Tero Marttila <terom@fixme.fi>
parents: 124
diff changeset
    66
133
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    67
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    68
void logwatch_on_error (struct logwatch *ctx, const struct logwatch_source *source, const struct error_info *err)
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    69
{
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    70
    struct logwatch_chan *chan;
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    71
    
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    72
    // notify each relevant channel
133
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    73
    TAILQ_FOREACH(chan, &ctx->channels, logwatch_channels) {
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    74
        // skip irrelevant channels
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    75
        if (!logwatch_chan_has_source(chan, source))
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    76
            continue;
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    77
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    78
        // send an error message
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    79
        logwatch_chan_msg(chan, "!!! Source '%s' failed: %s", source->name, error_msg(err));
e2d0c0c23b39 implement logwatch_chan.c, logwatch_chan_msg, logwatch_on_error
Tero Marttila <terom@fixme.fi>
parents: 132
diff changeset
    80
    }
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    81
}
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    82
119
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
/**
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
 * Add a logwatch_source with a FIFO at the given path
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
 */
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    86
static err_t logwatch_conf_source_fifo (void *_ctx, const struct config_option *option, const struct config_value values[], struct error_info *err)
119
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
{
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
    struct logwatch *ctx = _ctx;
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
    
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    90
    // parse arguments
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    91
    const char *path = config_get_string(option, values, "path");
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
    92
    
119
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
    // open it
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
    if (logwatch_open_fifo(ctx, path, err) == NULL)
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
       return ERROR_CODE(err);
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
    // ok
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
    return SUCCESS;
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
}
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
/**
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   102
 * Add a logwatch_filter for the given bits
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   103
 */
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   104
static err_t logwatch_conf_filter (void *_ctx, const struct config_option *option, const struct config_value values[], struct error_info *err)
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   105
{
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   106
    struct logwatch *ctx = _ctx;
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   107
    struct logwatch_filter *filter;
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   108
    
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   109
    const struct logwatch_source *source = NULL;
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   110
    struct logwatch_chan *chan = NULL;
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   111
    const char *name, *source_name, *pattern, *format;
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   112
    struct irc_chan *irc_chan;
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   113
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   114
    // parse arguments
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   115
    name        = config_get_string     (option, values, "name"     );
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   116
    source_name = config_get_string     (option, values, "source"   );
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   117
    pattern     = config_get_string     (option, values, "pattern"  );
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   118
    format      = config_get_string     (option, values, "format"   );
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   119
    irc_chan    = config_get_irc_chan   (option, values, "chan"     );
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   120
135
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   121
    // replace old filter?
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   122
    if ((filter = logwatch_filter_lookup(ctx, name))) {
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   123
        log_info("removing old filter: %s: %p", name, filter);
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   124
        logwatch_filter_destroy(filter);
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   125
    }
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   126
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   127
    // lookup source
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   128
    if (source_name && (source = logwatch_source_lookup(ctx, source_name)) == NULL)
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   129
       RETURN_SET_ERROR_STR(err, ERR_MODULE_CONF, "unknown logwatch_source name");
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   130
    
135
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   131
    // lookup channel
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   132
    if (irc_chan && (chan = logwatch_get_chan(ctx, irc_chan)) == NULL)
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   133
        RETURN_SET_ERROR_STR(err, ERR_MODULE_CONF, "logwatch_get_chan failed");
134
978041c1c04d update TODO, partially update error.c, rename module_get to modules_get, update config.lua
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
   134
    
978041c1c04d update TODO, partially update error.c, rename module_get to modules_get, update config.lua
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
   135
    log_info("add filter: name=%s, source=%s, pattern=%s, format=%s, chan=%s",
978041c1c04d update TODO, partially update error.c, rename module_get to modules_get, update config.lua
Tero Marttila <terom@fixme.fi>
parents: 133
diff changeset
   136
        name, source_name, pattern, format, irc_chan_name(irc_chan));
130
ffefb6d85ea6 implement logwatch_filter::format using str_format
Tero Marttila <terom@fixme.fi>
parents: 127
diff changeset
   137
135
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   138
    // create a new one
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   139
    if ((filter = logwatch_filter(ctx, name, source, pattern, format, chan, err)) == NULL)
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   140
        goto error;
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   141
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   142
    // ok
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   143
    return SUCCESS;
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   144
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   145
error:
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   146
    return ERROR_CODE(err);    
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   147
}
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   148
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   149
/**
119
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
 * Configuration options
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
 */
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   152
struct config_option logwatch_config_options[] = CONFIG_OPTIONS(
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   153
    CONFIG_OPT(         "source_fifo",  logwatch_conf_source_fifo,
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   154
        "read input lines from the FIFO at the given path",
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   155
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   156
        CONFIG_PARAM(       "path",     CONFIG_STRING,      "filesystem path to FIFO",                  false   )
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   157
    ),
119
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   158
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   159
    CONFIG_OPT(         "filter",       logwatch_conf_filter, 
124
f18d69425c4f fix up lua_module_conf/config_* enough so that logwatch_conf_filter works
Tero Marttila <terom@fixme.fi>
parents: 121
diff changeset
   160
        "filter lines from source (or all), using the given regular expression (or all lines), and format output "
135
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   161
        "using the given format expression (or pass through directly), and finally send the output to the given channel. "
9159bd51525f have logwatch_conf_filter replace any old filter with the same name, and move irc_log.sql to modules/
Tero Marttila <terom@fixme.fi>
parents: 134
diff changeset
   162
        "If a filter with the same name already exists, it will be replaced.",
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   163
138
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   164
        CONFIG_PARAM(       "name",    CONFIG_STRING,       "filter name",                              false   ),
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   165
        CONFIG_PARAM(       "source",  CONFIG_STRING,       "optional logwatch_source to use",          true    ),
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   166
        CONFIG_PARAM(       "pattern", CONFIG_STRING,       "optional regular expression pattern",      true    ),
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   167
        CONFIG_PARAM(       "format",  CONFIG_STRING,       "optional output format",                   true    ),
a716c621cb90 implement blackhole filters for logwatch, and stop applying filters after the first hit
Tero Marttila <terom@fixme.fi>
parents: 135
diff changeset
   168
        CONFIG_PARAM(       "chan",    CONFIG_IRC_CHAN,     "channel to send output to, or NULL to blackhole",  true)
121
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   169
    )
4682ebbc5644 implement logwatch_conf_filter such that it compiles and loads, but not yet tested
Tero Marttila <terom@fixme.fi>
parents: 119
diff changeset
   170
);
119
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   171
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   172
static void logwatch_destroy (void *_ctx)
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   173
{
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   174
    struct logwatch *ctx = _ctx;
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   175
    struct logwatch_filter *filter;
119
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   176
    struct logwatch_source *source;
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   177
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   178
    // flush the filters list
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   179
    while ((filter = TAILQ_FIRST(&ctx->filters)))
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   180
        logwatch_filter_destroy(filter);
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   181
119
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   182
    // flush the sources list
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   183
    while ((source = TAILQ_FIRST(&ctx->sources)))
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   184
        logwatch_source_destroy(source);
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   185
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   186
    // release the context
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   187
    free(ctx);
119
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   188
}
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   189
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   190
/**
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   191
 * The module function table
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   192
 */
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   193
struct module_desc logwatch_module = {
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   194
    .init               = logwatch_init,
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   195
    .config_options     = logwatch_config_options,
132
f2ece471fb07 implement logwatch_source names, and logwatch_chan
Tero Marttila <terom@fixme.fi>
parents: 130
diff changeset
   196
    .unload             = NULL,
119
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   197
    .destroy            = logwatch_destroy
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   198
};
64f50072db9e add logwatch module, that can already open FIFOs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   199