forked from nafur/l3pp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsink.h
105 lines (84 loc) · 2.24 KB
/
sink.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
102
103
104
/**
* @file sink.h
*
* Defines classes for log sinks (i.e. outputs)
*/
#pragma once
#include <ostream>
#include <fstream>
namespace l3pp {
/**
* Base class for a logging sink. It can only log some log entry to which some
* formatting is applied (see Formatter).
*/
class Sink {
FormatterPtr formatter;
public:
Sink() : formatter(std::make_shared<Formatter>()) {
}
Sink(FormatterPtr formatter) : formatter(formatter) {
}
/**
* Default destructor.
*/
virtual ~Sink() {}
FormatterPtr getFormatter() const {
return formatter;
}
void setFormatter(FormatterPtr formatter) {
this->formatter = formatter;
}
std::string formatMessage(EntryContext const& context, std::string const& message) const {
return (*formatter)(context, message);
}
/**
* Logs the given message with context info
*/
virtual void log(EntryContext const& context, std::string const& message) const = 0;
};
typedef std::shared_ptr<Sink> SinkPtr;
/**
* Logging sink that wraps an arbitrary `std::ostream`.
* It is meant to be used for streams like `std::cout` or `std::cerr`.
* A StreamSink may be given a log level, which filters out all entries
* below that level. By default is logs all entries.
*/
class StreamSink: public Sink {
/// Filtered loglevel
LogLevel level;
/// Output stream.
mutable std::unique_ptr<std::ostream> os;
LogLevel getLevel() const {
return level;
}
void setLevel(LogLevel level) {
this->level = level;
}
explicit StreamSink(std::ostream& _os) :
level(LogLevel::ALL),
os(new std::ostream(_os.rdbuf())) {}
explicit StreamSink(std::string const& filename) :
level(LogLevel::ALL),
os(new std::ofstream(filename, std::ios::out)) {}
public:
void log(EntryContext const& context, std::string const& message) const override {
if (context.level >= this->level) {
*os << formatMessage(context, message) << std::flush;
}
}
/**
* Create a StreamSink from some output stream.
* @param os Output stream.
*/
static SinkPtr create(std::ostream& os) {
return SinkPtr(new StreamSink(os));
}
/**
* Create a StreamSink from some output file.
* @param filename Filename for output file.
*/
static SinkPtr create(std::string const& filename) {
return SinkPtr(new StreamSink(filename));
}
};
}