mirror of
https://github.com/chromium/crashpad.git
synced 2024-12-25 22:30:49 +08:00
Use thread_local instead of ThreadLocalStorage::Slot.
This also significantly simplifies the implementation, since we don't really need the ThreadLogMessagesMaster class at all. Bug: chromium:1416710 Change-Id: I85849230015f901dfbf084d140e639f14cb872a7 Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/4313281 Reviewed-by: Mark Mentovai <mark@chromium.org> Commit-Queue: Peter Kasting <pkasting@chromium.org>
This commit is contained in:
parent
3e54a2cd1b
commit
322eaa5850
@ -18,71 +18,38 @@
|
||||
|
||||
#include "base/check_op.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/threading/thread_local_storage.h"
|
||||
|
||||
namespace crashpad {
|
||||
|
||||
namespace {
|
||||
|
||||
// While an object of this class exists, it will be set as the log message
|
||||
// handler. A thread may register its thread-specific log message list to
|
||||
// receive messages produced just on that thread.
|
||||
//
|
||||
// Only one object of this class may exist in the program at a time as created
|
||||
// by GetInstance(). There must not be any log message handler in effect when it
|
||||
// is created, and nothing else can be set as a log message handler while an
|
||||
// object of this class exists.
|
||||
class ThreadLogMessagesMaster {
|
||||
public:
|
||||
ThreadLogMessagesMaster(const ThreadLogMessagesMaster&) = delete;
|
||||
ThreadLogMessagesMaster& operator=(const ThreadLogMessagesMaster&) = delete;
|
||||
thread_local std::vector<std::string>* thread_local_log_messages;
|
||||
|
||||
void SetThreadMessageList(std::vector<std::string>* message_list) {
|
||||
DCHECK_EQ(logging::GetLogMessageHandler(), &LogMessageHandler);
|
||||
DCHECK_NE(tls_.Get() != nullptr, message_list != nullptr);
|
||||
tls_.Set(message_list);
|
||||
bool LogMessageHandler(logging::LogSeverity severity,
|
||||
const char* file_path,
|
||||
int line,
|
||||
size_t message_start,
|
||||
const std::string& string) {
|
||||
if (thread_local_log_messages) {
|
||||
thread_local_log_messages->push_back(string);
|
||||
}
|
||||
|
||||
static ThreadLogMessagesMaster* GetInstance() {
|
||||
static auto master = new ThreadLogMessagesMaster();
|
||||
return master;
|
||||
}
|
||||
|
||||
private:
|
||||
ThreadLogMessagesMaster() {
|
||||
DCHECK(!logging::GetLogMessageHandler());
|
||||
logging::SetLogMessageHandler(LogMessageHandler);
|
||||
}
|
||||
|
||||
~ThreadLogMessagesMaster() = delete;
|
||||
|
||||
static bool LogMessageHandler(logging::LogSeverity severity,
|
||||
const char* file_path,
|
||||
int line,
|
||||
size_t message_start,
|
||||
const std::string& string) {
|
||||
std::vector<std::string>* log_messages =
|
||||
reinterpret_cast<std::vector<std::string>*>(GetInstance()->tls_.Get());
|
||||
if (log_messages) {
|
||||
log_messages->push_back(string);
|
||||
}
|
||||
|
||||
// Don’t consume the message. Allow it to be logged as if nothing was set as
|
||||
// the log message handler.
|
||||
return false;
|
||||
}
|
||||
|
||||
base::ThreadLocalStorage::Slot tls_;
|
||||
};
|
||||
// Don’t consume the message. Allow it to be logged as if nothing was set as
|
||||
// the log message handler.
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ThreadLogMessages::ThreadLogMessages() : log_messages_() {
|
||||
ThreadLogMessagesMaster::GetInstance()->SetThreadMessageList(&log_messages_);
|
||||
}
|
||||
|
||||
ThreadLogMessages::~ThreadLogMessages() {
|
||||
ThreadLogMessagesMaster::GetInstance()->SetThreadMessageList(nullptr);
|
||||
ThreadLogMessages::ThreadLogMessages()
|
||||
: log_messages_(),
|
||||
reset_thread_local_log_messages_(&thread_local_log_messages,
|
||||
&log_messages_) {
|
||||
[[maybe_unused]] static bool initialized = [] {
|
||||
DCHECK(!logging::GetLogMessageHandler());
|
||||
logging::SetLogMessageHandler(LogMessageHandler);
|
||||
return true;
|
||||
}();
|
||||
}
|
||||
|
||||
} // namespace crashpad
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "base/auto_reset.h"
|
||||
|
||||
namespace crashpad {
|
||||
|
||||
@ -34,7 +35,7 @@ class ThreadLogMessages {
|
||||
ThreadLogMessages(const ThreadLogMessages&) = delete;
|
||||
ThreadLogMessages& operator=(const ThreadLogMessages&) = delete;
|
||||
|
||||
~ThreadLogMessages();
|
||||
~ThreadLogMessages() = default;
|
||||
|
||||
//! \return The log messages collected on the thread that this object was
|
||||
//! created on since the time it was created.
|
||||
@ -42,6 +43,8 @@ class ThreadLogMessages {
|
||||
|
||||
private:
|
||||
std::vector<std::string> log_messages_;
|
||||
const base::AutoReset<std::vector<std::string>*>
|
||||
reset_thread_local_log_messages_;
|
||||
};
|
||||
|
||||
} // namespace crashpad
|
||||
|
Loading…
x
Reference in New Issue
Block a user