Skip to content

[lldb] Add TeeLogHandler to log to 2 handlers #90984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lldb/include/lldb/Utility/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ class RotatingLogHandler : public LogHandler {
static char ID;
};

/// A T-style log handler that multiplexes messages to two log handlers.
class TeeLogHandler : public LogHandler {
public:
TeeLogHandler(std::shared_ptr<LogHandler> first_log_handler,
std::shared_ptr<LogHandler> second_log_handler);

void Emit(llvm::StringRef message) override;

bool isA(const void *ClassID) const override { return ClassID == &ID; }
static bool classof(const LogHandler *obj) { return obj->isA(&ID); }

private:
std::shared_ptr<LogHandler> m_first_log_handler;
std::shared_ptr<LogHandler> m_second_log_handler;
static char ID;
};

class Log final {
public:
/// The underlying type of all log channel enums. Declare them as:
Expand Down
14 changes: 14 additions & 0 deletions lldb/source/Utility/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ char LogHandler::ID;
char StreamLogHandler::ID;
char CallbackLogHandler::ID;
char RotatingLogHandler::ID;
char TeeLogHandler::ID;

llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map;

Expand Down Expand Up @@ -438,3 +439,16 @@ void RotatingLogHandler::Dump(llvm::raw_ostream &stream) const {
}
stream.flush();
}

TeeLogHandler::TeeLogHandler(std::shared_ptr<LogHandler> first_log_handler,
std::shared_ptr<LogHandler> second_log_handler)
: m_first_log_handler(first_log_handler),
m_second_log_handler(second_log_handler) {
assert(m_first_log_handler && "first log handler must be valid");
assert(m_second_log_handler && "second log handler must be valid");
}

void TeeLogHandler::Emit(llvm::StringRef message) {
m_first_log_handler->Emit(message);
m_second_log_handler->Emit(message);
}
12 changes: 12 additions & 0 deletions lldb/unittests/Utility/LogTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ TEST(LogHandlerTest, RotatingLogHandler) {
EXPECT_EQ(GetDumpAsString(handler), "bazquxquux");
}

TEST(LogHandlerTest, TeeLogHandler) {
auto handler1 = std::make_shared<RotatingLogHandler>(2);
auto handler2 = std::make_shared<RotatingLogHandler>(2);
TeeLogHandler handler(handler1, handler2);

handler.Emit("foo");
handler.Emit("bar");

EXPECT_EQ(GetDumpAsString(*handler1), "foobar");
EXPECT_EQ(GetDumpAsString(*handler2), "foobar");
}

TEST_F(LogChannelTest, Enable) {
EXPECT_EQ(nullptr, GetLog(TestChannel::FOO));
std::string message;
Expand Down