This repository has been archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.cpp
272 lines (224 loc) · 5.1 KB
/
log.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
// libircbot irc::bot::
#include "ircbot/bot.h"
using namespace irc::bot;
// SPQF
#include "log.h"
///////////////////////////////////////////////////////////////////////////////
//
// log::
//
void irc::log::init()
{
const auto &opts(get_opts());
if(!opts.get<bool>("logging"))
return;
const int ret(mkdir(opts["logdir"].c_str(),0777));
if(ret && errno != EEXIST)
throw Internal("Failed to create specified logfile directory [") << opts["logdir"] << "]";
}
bool irc::log::log(const Msg &msg,
const Chan &chan,
const User &user)
try
{
const auto &opts(get_opts());
if(!opts.get<bool>("logging"))
return false;
Log lf(get_path(chan.get_name()));
lf(msg,chan,user);
return true;
}
catch(const Internal &e)
{
std::cerr << e << std::endl;
return false;
}
bool irc::log::exists(const std::string &name,
const Filter &filter)
{
return !for_each(name,[&filter]
(const ClosureArgs &a)
{
return !filter(a);
});
}
bool irc::log::atleast(const std::string &name,
const Filter &filter,
const size_t &count)
{
size_t ret(0);
return !count || !for_each(name,[&filter,&ret,&count]
(const ClosureArgs &a)
{
ret += filter(a);
return ret < count;
});
}
size_t irc::log::count(const std::string &name,
const Filter &filter)
{
size_t ret(0);
for_each(name,[&filter,&ret]
(const ClosureArgs &a)
{
ret += filter(a);
return true;
});
return ret;
}
bool irc::log::for_each(const std::string &name,
const Filter &filter,
const Closure &closure)
{
return for_each(name,[&filter,&closure]
(const ClosureArgs &a)
{
if(!filter(a))
return true;
return closure(a);
});
}
bool irc::log::for_each(const std::string &name,
const Closure &closure)
try
{
std::ifstream file;
file.exceptions(std::ios_base::badbit);
file.open(get_path(name),std::ios_base::in);
while(1)
{
char buf[64] alignas(16);
file.getline(buf,sizeof(buf));
const size_t len = strlen(buf);
std::replace(buf,buf+len,' ','\0');
if(!len)
return true;
size_t i(0);
const char *ptr(buf), *const end(buf + len);
std::array<const char *, Log::_NUM_FIELDS> field;
while(ptr < end)
{
field[i++] = ptr;
ptr += strlen(ptr) + 1;
}
while(i < field.size())
field[i++] = nullptr;
if(__builtin_expect((!field[Log::VERS] || *field[Log::VERS] != '0'),0))
throw Assertive("Log file corruption detected. I'm afraid I must abort...");
const ClosureArgs args
{
atoll(field[Log::TIME]),
field[Log::ACCT],
field[Log::NICK],
field[Log::TYPE],
};
if(!closure(args))
return false;
}
}
catch(const std::ios_base::failure &e)
{
throw Exception(e.what());
}
std::string irc::log::get_path(const std::string &name)
{
const auto &opts(get_opts());
return opts["logdir"] + "/" + name;
}
///////////////////////////////////////////////////////////////////////////////
//
// log::Log
//
irc::log::Log::Log(const std::string &path)
try:
path(path)
{
file.exceptions(std::ios_base::badbit|std::ios_base::failbit);
file.open(path,std::ios_base::app);
}
catch(const std::exception &e)
{
throw Internal("Failed opening Log [") << path << "]";
}
irc::log::Log::~Log()
noexcept try
{
flush();
file.close();
}
catch(const std::exception &e)
{
std::cerr << "Failed closing Log [" << get_path() << "]" << std::endl;
return;
}
void irc::log::Log::flush()
try
{
file.flush();
}
catch(const std::ios_base::failure &f)
{
throw Internal("Failed flushing log [") << get_path() << "]: " << f.what();
}
void irc::log::Log::operator()(const Msg &msg,
const Chan &chan,
const User &user)
try
{
static const uint VERSION(0);
const time_t time(std::time(nullptr));
const std::string &acct(user.is_logged_in()? user.get_acct() : "*");
const std::string &nick(user.get_nick());
const std::string &type(!msg.get_code()? msg.get_name().substr(0,3): lex_cast(msg.get_code()));
file << VERSION
<< ' '
<< time
<< ' '
<< acct
<< ' '
<< nick
<< ' '
<< type
<< '\n';
}
catch(const std::ios_base::failure &f)
{
throw Internal("Failed appending log [") << get_path() << "]: " << f.what();
}
///////////////////////////////////////////////////////////////////////////////
//
// log:: misc
//
bool irc::log::FilterAny::operator()(const ClosureArgs &args)
const
{
return std::any_of(this->begin(),this->end(),[&args]
(const auto &filter)
{
return filter(args);
});
}
bool irc::log::FilterAll::operator()(const ClosureArgs &args)
const
{
return std::all_of(this->begin(),this->end(),[&args]
(const auto &filter)
{
return filter(args);
});
}
bool irc::log::FilterNone::operator()(const ClosureArgs &args)
const
{
return std::none_of(this->begin(),this->end(),[&args]
(const auto &filter)
{
return filter(args);
});
}