File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -112,6 +112,23 @@ class RotatingLogHandler : public LogHandler {
112
112
static char ID;
113
113
};
114
114
115
+ // / A T-style log handler that multiplexes messages to two log handlers.
116
+ class TeeLogHandler : public LogHandler {
117
+ public:
118
+ TeeLogHandler (std::shared_ptr<LogHandler> first_log_handler,
119
+ std::shared_ptr<LogHandler> second_log_handler);
120
+
121
+ void Emit (llvm::StringRef message) override ;
122
+
123
+ bool isA (const void *ClassID) const override { return ClassID == &ID; }
124
+ static bool classof (const LogHandler *obj) { return obj->isA (&ID); }
125
+
126
+ private:
127
+ std::shared_ptr<LogHandler> m_first_log_handler;
128
+ std::shared_ptr<LogHandler> m_second_log_handler;
129
+ static char ID;
130
+ };
131
+
115
132
class Log final {
116
133
public:
117
134
// / The underlying type of all log channel enums. Declare them as:
Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ char LogHandler::ID;
39
39
char StreamLogHandler::ID;
40
40
char CallbackLogHandler::ID;
41
41
char RotatingLogHandler::ID;
42
+ char TeeLogHandler::ID;
42
43
43
44
llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map;
44
45
@@ -438,3 +439,16 @@ void RotatingLogHandler::Dump(llvm::raw_ostream &stream) const {
438
439
}
439
440
stream.flush ();
440
441
}
442
+
443
+ TeeLogHandler::TeeLogHandler (std::shared_ptr<LogHandler> first_log_handler,
444
+ std::shared_ptr<LogHandler> second_log_handler)
445
+ : m_first_log_handler(first_log_handler),
446
+ m_second_log_handler(second_log_handler) {
447
+ assert (m_first_log_handler && " first log handler must be valid" );
448
+ assert (m_second_log_handler && " second log handler must be valid" );
449
+ }
450
+
451
+ void TeeLogHandler::Emit (llvm::StringRef message) {
452
+ m_first_log_handler->Emit (message);
453
+ m_second_log_handler->Emit (message);
454
+ }
Original file line number Diff line number Diff line change @@ -200,6 +200,18 @@ TEST(LogHandlerTest, RotatingLogHandler) {
200
200
EXPECT_EQ (GetDumpAsString (handler), " bazquxquux" );
201
201
}
202
202
203
+ TEST (LogHandlerTest, TeeLogHandler) {
204
+ auto handler1 = std::make_shared<RotatingLogHandler>(2 );
205
+ auto handler2 = std::make_shared<RotatingLogHandler>(2 );
206
+ TeeLogHandler handler (handler1, handler2);
207
+
208
+ handler.Emit (" foo" );
209
+ handler.Emit (" bar" );
210
+
211
+ EXPECT_EQ (GetDumpAsString (*handler1), " foobar" );
212
+ EXPECT_EQ (GetDumpAsString (*handler2), " foobar" );
213
+ }
214
+
203
215
TEST_F (LogChannelTest, Enable) {
204
216
EXPECT_EQ (nullptr , GetLog (TestChannel::FOO));
205
217
std::string message;
You can’t perform that action at this time.
0 commit comments