annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 /*
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2
2
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
3 Copyright (c) 2013 Carl Byington - 510 Software Group, released under
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 the GPL version 3 or any later version at your choice available at
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 http://www.gnu.org/licenses/gpl-3.0.txt
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7 */
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11 struct ltint
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 bool operator()(const int s1, const int s2) const
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 {
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15 return (unsigned)s1 < (unsigned)s2;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 }
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 };
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 const int buflen = 1024;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22
2
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
23 class CONTEXT {
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 public:
2
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
25 const char * name; // name of this context
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 // input side
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27 int fd; // input fn syslog file
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28 struct stat openfdstat;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29 int len; // bytes in the buffer
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30 char buf[buflen];
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 // output side
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 int fdo; // output tempin wflogs file
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33 time_t open_time; // time when fdo opened
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34 TOKEN *tokp;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35 // our data
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36 int period; // in seconds
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37 int versions; // number to keep
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38 // all strings owned by the string table
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39 const char * output; // output file name pattern
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
40 const char * tempin; // temp wflogs input file name
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41 const char * wflogs; // wflogs command line
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
42 const char * fn; // name of the syslog file
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
43 const char * pattern; // regex to filter lines
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
44 regex_t re; // internal regex to filter lines
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45
2
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
46 CONTEXT(const char *nam);
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
47 ~CONTEXT();
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
48 void set_token(TOKEN &tok) { tokp = &tok; };
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49 void set_period(int p) { period = p; };
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50 void set_versions(int v) { versions = v; };
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51 void set_output(const char * o) { output = o; };
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52 void set_tempin(const char * t) { tempin = t; openo(true); };
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 void set_wflogs(const char * w) { wflogs = w; };
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 void set_file(const char * f) { fn = f; open(true); };
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55 void set_pattern(const char * p) { pattern = p; };
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
56 bool failedo() { return (fdo == -1); };
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57 bool failed() { return (fd == -1); };
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
58 void dump();
2
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
59 void openo(bool msg);
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 void open(bool msg);
2
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
61 bool write(char *p);
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
62 bool read();
2
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
63 void closeo();
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
64 void close();
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
65 void process(char *p);
2
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
66 void check_wflog(); // time to call it?
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
67 void free_all();
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
68 };
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
69 typedef CONTEXT * CONTEXTP;
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
70 typedef list<CONTEXTP> context_list;
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
71
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
72
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
73 class CONFIG {
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
74 public:
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
75 // the only mutable stuff once it has been loaded from the config file
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
76 int reference_count; // protected by the global config_mutex
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
77 // all the rest is constant after loading from the config file
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
78 int generation;
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
79 time_t load_time;
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
80 string_set config_files;
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
81 context_list contexts;
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
82
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
83 CONFIG();
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
84 ~CONFIG();
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
85 void add_context(CONTEXTP con) {contexts.push_back(con);} ;
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
86 void dump();
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
87 bool read();
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
88 void sleep(int duration, time_t &previous);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
89 void free_all();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
90 };
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
91
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
92 void discard(string_set &s);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
93 const char* register_string(string_set &s, const char *name);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
94 const char* register_string(const char *name);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
95 void clear_strings();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
96 bool load_conf(CONFIG &dc, const char *fn);
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
97 void token_init();
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
98
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
99
2
400b1de6e1c6 allow multiple config contexts
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
100 extern const char *token_context;
0
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
101 extern const char *token_file;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
102 extern const char *token_include;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
103 extern const char *token_lbrace;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
104 extern const char *token_output;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
105 extern const char *token_pattern;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
106 extern const char *token_period;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
107 extern const char *token_rbrace;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
108 extern const char *token_semi;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
109 extern const char *token_tempin;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
110 extern const char *token_versions;
0aa1171aebd2 initial version
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
111 extern const char *token_wflogs;