-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogdemux.cpp
248 lines (218 loc) · 5 KB
/
logdemux.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright (c) 2012 Roman Neuhauser
// Distributed under the MIT license (see LICENSE file)
// vim: sw=2 sts=2 ts=2 fdm=marker cms=\ //\ %s
// shared_ptr
#include <memory>
#include <iostream>
#include <istream>
#include <ostream>
#include <fstream>
#include <string>
#include <vector>
#define EX_OK 0 /* successful termination */
#define EX_USAGE 64 /* command line usage error */
#define EX_DATAERR 65 /* data format error */
#define EX_NOINPUT 66 /* cannot open input */
#define EX_SOFTWARE 70 /* internal software error */
#include "boost/ref.hpp"
#include "boost/foreach.hpp"
#include "boost/format.hpp"
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/regex.hpp"
#include "iniphile/input.hpp"
#include "iniphile/ast.hpp"
#include "iniphile/output.hpp"
#define foreach BOOST_FOREACH
using std::shared_ptr;
using std::exception;
using std::cin;
using std::cerr;
using std::endl;
using std::ios;
using std::getline;
using std::ostream;
using std::ifstream;
using std::ofstream;
using std::string;
using std::vector;
using boost::cref;
using boost::format;
using namespace boost::gregorian;
using boost::regex;
using boost::smatch;
namespace
{
struct expand_sink // {{{
{
expand_sink(string const &prefix, date const &today)
: prefix(prefix)
, today(today)
{}
private:
string const &prefix;
date const &today;
public:
string
operator()(smatch const &expando) const
{
auto sexpando = expando.str();
if (sexpando == "%D") return to_iso_extended_string(today);
if (sexpando == "%P") return prefix;
return "LOGDEMUX-BUG";
}
}; // }}}
struct logdemux_error
: exception {};
struct missing_rules_order
: logdemux_error {};
class rule
// {{{
{
public:
rule(string const &prefix, string const &match, string const &sink, bool final, ostream &diag) // {{{
: sink(sink)
, prefix(prefix)
, final(final)
, pat(match, regex::perl)
, opened_on()
, os()
, diag(diag)
{
} // }}}
bool
handle(date const &today, string const &line) // {{{
{
if (!regex_search(line, pat))
return false;
if (opened_on != today)
reopen(today);
os << line << endl;
return final;
} // }}}
private:
string const sink;
string const prefix;
bool final;
regex pat;
date opened_on;
ofstream os;
ostream &diag;
void
reopen(date const &today) // {{{
{
if (os.is_open()) os.close();
os.clear();
auto fname = expand(sink, today);
os.open(fname.c_str(), ios::app | ios::binary);
if (os.fail())
diag << "failed to open " << fname << endl;
opened_on = today;
} // }}}
string
expand(string const &fmt, date const &d) const // {{{
{
return regex_replace(
fmt
, regex("%[DP]\\>")
, cref(expand_sink(prefix, d))
);
} // }}}
}; // }}}
class ruleset
// {{{
{
public:
ruleset(iniphile::ast::node const &ini, string const &prefix, ostream &diag) // {{{
{
auto order = iniphile::get(ini, "rules.order", vector<string>());
if (order.empty())
throw missing_rules_order();
foreach (auto const &rname, order)
rules.push_back(shared_ptr<rule>(new rule(
prefix
, iniphile::get(ini, rname + ".match", string(""))
, iniphile::get(ini, rname + ".sink", string(""))
, iniphile::get(ini, rname + ".final", false)
, diag
)));
} // }}}
void
handle(date const &now, string const &line) // {{{
{
foreach (auto &r, rules)
if (r->handle(now, line))
break;
} // }}}
private:
vector<shared_ptr<rule>> rules;
}; // }}}
date
today() // {{{
{
return day_clock::local_day();
} // }}}
template<class Fmt>
int
complain(int exitcode, Fmt msg) // {{{
{
cerr << msg << endl;
return exitcode;
} // }}}
string
tailname(string const &path) // {{{
{
return regex_replace(
path
, regex("^(.*/)?([^/]+)(.exe)?$", regex::perl)
, "$2"
);
} // }}}
}
int
main(int argc, char **argv)
{
string self = argc ? tailname(argv[0]) : "logdemux";
if (argc < 3)
return complain(
EX_USAGE
, format("usage: %1% rules prefix") % self
);
string inipath = argv[1];
string prefix = argv[2];
ifstream input;
input.open(inipath);
if (input.fail())
return complain(
EX_NOINPUT
, format("%1%: rules file '%2%' missing") % self % inipath
);
auto ini = iniphile::parse(inipath, input, cerr);
input.close();
if (!ini)
return complain(
EX_DATAERR
, format("%1%: rules file '%2%' broken") % self % inipath
);
auto cfg(iniphile::normalize(*ini));
try {
ruleset rules(cfg, prefix, cerr);
string line;
while (cin.good()) {
while (getline(cin, line)) {
rules.handle(today(), line);
}
}
return EX_OK;
} catch (missing_rules_order &e) {
return complain(
EX_DATAERR
, format("%1%: no rules.order in '%2%'") % self % inipath
);
} catch (exception &e) {
return complain(
EX_SOFTWARE
, format("%1%: internal error: %2%") % self % e.what()
);
}
// let other exceptions cause std::terminate
}