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.h
101 lines (75 loc) · 2.33 KB
/
log.h
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
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
namespace irc {
namespace log {
struct ClosureArgs
{
const time_t &time;
const char *const &acct;
const char *const &nick;
const char *const &type;
};
using Closure = std::function<bool (const ClosureArgs &)>;
class Filter : protected std::vector<Closure>
{
public:
virtual bool operator()(const ClosureArgs &args) const = 0;
protected:
template<class... A> Filter(A&&... a): std::vector<Closure>{std::forward<A>(a)...} {}
};
class FilterAny : public Filter
{
bool operator()(const ClosureArgs &args) const override;
public:
template<class... A> FilterAny(A&&... a): Filter(std::forward<A>(a)...) {}
};
class FilterAll : public Filter
{
bool operator()(const ClosureArgs &args) const override;
public:
template<class... A> FilterAll(A&&... a): Filter(std::forward<A>(a)...) {}
};
class FilterNone : public Filter
{
bool operator()(const ClosureArgs &args) const override;
public:
template<class... A> FilterNone(A&&... a): Filter(std::forward<A>(a)...) {}
};
class Log
{
std::string path;
std::ofstream file;
public:
enum Field
{
VERS = 0, // Version/format of this line
TIME = 1, // Epoch time in seconds
ACCT = 2, // NickServ account name
NICK = 3, // Nickname
TYPE = 4, // Event type
_NUM_FIELDS
};
auto &get_path() const { return path; }
auto &get_file() const { return file; }
void operator()(const Msg &msg, const Chan &chan, const User &user);
void flush();
Log(const std::string &path);
~Log() noexcept;
};
std::string get_path(const std::string &name);
// Reading
// returns false if break early - "remain true to the end"
bool for_each(const std::string &name, const Closure &closure);
bool for_each(const std::string &name, const Filter &filter, const Closure &closure);
size_t count(const std::string &name, const Filter &filter);
bool atleast(const std::string &name, const Filter &filter, const size_t &count);
bool exists(const std::string &name, const Filter &filter);
// Writing
bool log(const Msg &msg, const Chan &chan, const User &user);
void init();
} // namespace log
} // namespace irc