Skip to content

Commit 795cd65

Browse files
committed
Faster filtered log skipping.
1 parent 19fe412 commit 795cd65

File tree

12 files changed

+47
-34
lines changed

12 files changed

+47
-34
lines changed

sog/base.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ void sog::init(sog::Sink *newsink) {
3434
sink = new sog::PrettySink(&std::clog);
3535
}
3636

37-
std::unique_ptr<sog::SinkData> sog::_prepare(const Source *s) {
37+
sog::Prepared sog::_prepare(const Source *s) {
3838
return sink->prepare(s);
3939
}
4040

4141
void sog::_submit(SinkData *d, Message m) {
4242
sink->log(d, m);
4343
if (m.source->level == level::FATAL)
44-
abort();
44+
std::terminate();
4545
}

sog/base.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace sog {
5353
struct Source;
5454
struct Message;
5555

56-
using Level = uint8_t;
56+
using Level = uint16_t;
5757
namespace level {
5858
constexpr Level FATAL = 0;
5959
constexpr Level ERROR = 1;
@@ -73,6 +73,19 @@ namespace sog {
7373
virtual ~SinkData();
7474
};
7575

76+
struct Prepared final {
77+
// Data to be passed to the sink.
78+
std::unique_ptr<SinkData> sink_data;
79+
80+
/** Should messages from this sink be logged.
81+
*
82+
* If false log() will never be called for this source.
83+
*
84+
* This can be used to cheaply drop messages that will never be logged.
85+
*/
86+
bool log = true;
87+
};
88+
7689
/** A log source.
7790
*
7891
* A source is a collection of metadata about a "type" of log message. This
@@ -142,7 +155,7 @@ namespace sog {
142155
source(source), values(&*vals.begin()) {}
143156
};
144157

145-
std::unique_ptr<SinkData> _prepare(const Source *s);
158+
Prepared _prepare(const Source *s);
146159
void _submit(SinkData *d, Message m);
147160
}
148161

sog/dynamic.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ void sog::DynamicLog::submit() {
55
message.values = values.data();
66
source.value_count = values.size();
77

8-
std::unique_ptr<sog::SinkData> sd { sog::_prepare(&source) };
9-
sog::_submit(sd.get(), std::move(message));
8+
auto prepare = sog::_prepare(&source);
9+
if (prepare.log)
10+
sog::_submit(prepare.sink_data.get(), std::move(message));
1011
}

sog/journald.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ sog::JournaldSink::JournaldSink() {
8181
}
8282
}
8383

84-
std::unique_ptr<sog::SinkData> sog::JournaldSink::prepare(const sog::Source *source) {
85-
return std::make_unique<Data>(source);
84+
sog::Prepared sog::JournaldSink::prepare(const sog::Source *source) {
85+
return { std::make_unique<Data>(source) };
8686
}
8787

8888
sog::Logged sog::JournaldSink::log(sog::SinkData *sd, sog::Message msg) {

sog/journald.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace sog {
1111
~JournaldSink();
1212
JournaldSink();
1313

14-
std::unique_ptr<SinkData> prepare(const Source *source) override;
14+
Prepared prepare(const Source *source) override;
1515
Logged log(SinkData *sd, Message msg) override;
1616
};
1717
}

sog/json.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ struct Data: public sog::SinkData {
8383

8484
}
8585

86-
std::unique_ptr<sog::SinkData> sog::JsonSink::prepare(const sog::Source *source) {
87-
return std::make_unique<Data>(source);
86+
sog::Prepared sog::JsonSink::prepare(const sog::Source *source) {
87+
return { std::make_unique<Data>(source) };
8888
}
8989

9090
sog::Logged sog::JsonSink::log(sog::SinkData *sd, sog::Message msg) {

sog/json.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace sog {
1212
std::ostream *out;
1313

1414
JsonSink(std::ostream *out): out{out} {}
15-
std::unique_ptr<SinkData> prepare(const Source *source) override;
15+
Prepared prepare(const Source *source) override;
1616
Logged log(SinkData *sd, Message msg) override;
1717
};
1818
}

sog/macro.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@ namespace sog {
118118
}; \
119119
static_assert(::sog::_is_valid(&_sog_source), \
120120
"Invalid template string (check formatting and argument names.)"); \
121-
static std::unique_ptr<::sog::SinkData> _sog_sink_data = \
122-
::sog::_prepare(&_sog_source); \
123-
::sog::_submit(_sog_sink_data.get(), ::sog::Message(&_sog_source, { vals })); \
121+
static sog::Prepared _sog_prepared = ::sog::_prepare(&_sog_source); \
122+
if (!_sog_prepared.log) \
123+
break; \
124+
::sog::_submit(_sog_prepared.sink_data.get(), ::sog::Message(&_sog_source, { vals })); \
124125
} while(false)
125126

126127
#define _sog_KEYS(r, _, i, e, ...) \

sog/pretty.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,15 @@ struct Data: public sog::SinkData {
4444

4545
}
4646

47-
std::unique_ptr<sog::SinkData> sog::PrettySink::prepare(const sog::Source *source) {
47+
sog::Prepared sog::PrettySink::prepare(const sog::Source *source) {
4848
if (source->msg_template.empty())
49-
return nullptr;
49+
return { nullptr, false };
5050

51-
return std::make_unique<Data>(source);
51+
return { std::make_unique<Data>(source) };
5252
}
5353

5454
sog::Logged sog::PrettySink::log(sog::SinkData *sd, sog::Message msg) {
55-
if (!sd)
56-
return {};
57-
55+
assert(sd);
5856
Data *data = dynamic_cast<Data*>(sd);
5957

6058
timeval time;

sog/pretty.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace sog {
1212
std::ostream *out;
1313

1414
PrettySink(std::ostream *out): out{out} {}
15-
std::unique_ptr<SinkData> prepare(const Source *source) override;
15+
Prepared prepare(const Source *source) override;
1616
Logged log(SinkData *sd, Message msg) override;
1717
};
1818
}

sog/sink.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ char sog::level_char(sog::Level l) {
3434
return LEVEL_CHARS[i];
3535
}
3636

37-
std::unique_ptr<sog::SinkData> sog::Sink::prepare(const Source *source) {
38-
return nullptr;
37+
sog::Prepared sog::Sink::prepare(const Source *source) {
38+
return { nullptr };
3939
}

sog/sink.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace sog {
1313
};
1414

1515
/** Sink interface.
16-
*
16+
*
1717
* This is the interface that should be written to for consuming messages
1818
* logged with sog. It is not expected that users will call this interface
1919
* themselves.
@@ -22,47 +22,47 @@ namespace sog {
2222
virtual ~Sink() {}
2323

2424
/** Prepare data for a source.
25-
*
25+
*
2626
* This method will be called once for every source. The value returned
2727
* will be deleted if the source is ever destructed. Note that in normal
2828
* use of sog sources are never destructed but there is an API provided
2929
* for "dynamic" logging in which the destruction of sources is common.
30-
*
30+
*
3131
* @warning This function must be reentrant. It will only be called once
3232
* for any given source but it may be called for multiple sources
3333
* concurrently.
34-
*
34+
*
3535
* @param source The source to prepare for.
3636
* @return Arbitrary data.
3737
*/
38-
virtual std::unique_ptr<SinkData> prepare(const Source *source);
38+
virtual Prepared prepare(const Source *source);
3939

4040
/** Log a message.
41-
*
41+
*
4242
* This message provides the sink data from a previous call to prepare
4343
* and a sog::Message. The msg.source attribute will be the same source
4444
* (both in location and content) as the source passed to the prepare()
4545
* call that produced sink_data.
46-
*
46+
*
4747
* @warning This function must be reentrant. It will be called
4848
* concurrently for the same and different sinks.
49-
*
49+
*
5050
* @param sink_data Data from a previous call to prepare()
5151
* @param msg The message to log.
5252
*/
5353
virtual Logged log(SinkData *sink_data, Message msg) = 0;
5454
};
5555

5656
/** Get the name of a log level.
57-
*
57+
*
5858
* Returns a string_view to a static string describing the log level. Note
5959
* that some log levels return the same string (in particular high log
6060
* levels return TRACE irrespective of their specific value).
6161
*/
6262
std::experimental::string_view level_name(sog::Level level);
6363

6464
/** Get a character representing a log level.
65-
*
65+
*
6666
* Note that the returned charcter isn't necessarily unique for a give log
6767
* level (see sog::level_name()).
6868
*/

0 commit comments

Comments
 (0)