view src/wflogs-config.h @ 2:400b1de6e1c6

allow multiple config contexts
author Carl Byington <carl@five-ten-sg.com>
date Fri, 17 May 2013 10:32:12 -0700
parents 0aa1171aebd2
children 37eace15ef87
line wrap: on
line source

/*

Copyright (c) 2013 Carl Byington - 510 Software Group, released under
the GPL version 3 or any later version at your choice available at
http://www.gnu.org/licenses/gpl-3.0.txt

*/



struct ltint
{
  bool operator()(const int s1, const int s2) const
  {
    return (unsigned)s1 < (unsigned)s2;
  }
};


const int buflen = 1024;


class CONTEXT {
public:
    const char *        name;               // name of this context
    // input side
    int                 fd;                 // input fn syslog file
    struct stat         openfdstat;
    int                 len;        // bytes in the buffer
    char                buf[buflen];
    // output side
    int                 fdo;                // output tempin wflogs file
    time_t              open_time;          // time when fdo opened
    TOKEN               *tokp;
    // our data
    int             period;     // in seconds
    int             versions;   // number to keep
    // all strings owned by the string table
    const char *    output;     // output file name pattern
    const char *    tempin;     // temp wflogs input file name
    const char *    wflogs;     // wflogs command line
    const char *    fn;         // name of the syslog file
    const char *    pattern;    // regex to filter lines
    regex_t         re;         // internal regex to filter lines

    CONTEXT(const char *nam);
    ~CONTEXT();
    void    set_token(TOKEN &tok)       { tokp = &tok; };
    void    set_period(int p)           { period = p; };
    void    set_versions(int v)         { versions = v; };
    void    set_output(const char * o)  { output = o; };
    void    set_tempin(const char * t)  { tempin = t; openo(true); };
    void    set_wflogs(const char * w)  { wflogs = w; };
    void    set_file(const char * f)    { fn = f; open(true); };
    void    set_pattern(const char * p) { pattern = p; };
    bool    failedo()                   { return (fdo == -1); };
    bool    failed()                    { return (fd == -1); };
    void    dump();
    void    openo(bool msg);
    void    open(bool msg);
    bool    write(char *p);
    bool    read();
    void    closeo();
    void    close();
    void    process(char *p);
    void    check_wflog();      // time to call it?
    void    free_all();
};
typedef CONTEXT *               CONTEXTP;
typedef list<CONTEXTP>          context_list;


class CONFIG {
public:
    // the only mutable stuff once it has been loaded from the config file
    int                 reference_count;    // protected by the global config_mutex
    // all the rest is constant after loading from the config file
    int                 generation;
    time_t              load_time;
    string_set          config_files;
    context_list        contexts;

    CONFIG();
    ~CONFIG();
    void    add_context(CONTEXTP con)  {contexts.push_back(con);} ;
    void    dump();
    bool    read();
    void    sleep(int duration, time_t &previous);
    void    free_all();
};

void        discard(string_set &s);
const char* register_string(string_set &s, const char *name);
const char* register_string(const char *name);
void        clear_strings();
bool        load_conf(CONFIG &dc, const char *fn);
void        token_init();


extern const char *token_context;
extern const char *token_file;
extern const char *token_include;
extern const char *token_lbrace;
extern const char *token_output;
extern const char *token_pattern;
extern const char *token_period;
extern const char *token_rbrace;
extern const char *token_semi;
extern const char *token_tempin;
extern const char *token_versions;
extern const char *token_wflogs;