mirror of
https://github.com/chromium/crashpad.git
synced 2025-03-09 22:26:06 +00:00
Add ThreadLogMessages and its test.
This updates mini_chromium to 91ea4908ffd74d9c886bd2f8ccbfae6d31c499af. The last five commits listed here are required to support this change. The mini_chromium update includes: c1745a924c5c Fix paths to atomicops files in base.gyp from 2f02dcc73536 108e9247189c Add #include of <unistd.h> to close_nocancel.cc 6e4f98a9edf8 Add logging::SetLogMessageHandler() 4063fcb8f460 Add base::ThreadLocalStorage 4870f18a33a6 Add base::LazyInstance 0d31b1f3a289 Fix base/memory/aligned_memory.h for MSVC 91ea4908ffd7 base/logging.h: DCHECK() should always reference its condition BUG=crashpad:26 TEST=crashpad_util_test ThreadLogMessages.* R=rsesek@chromium.org Review URL: https://codereview.chromium.org/1041643003
This commit is contained in:
parent
5d0a133ecd
commit
e95224bbe7
2
DEPS
2
DEPS
@ -28,7 +28,7 @@ deps = {
|
||||
'32ca1cd8e010d013a606a752fb49a603a3598071', # svn r2015
|
||||
'crashpad/third_party/mini_chromium/mini_chromium':
|
||||
Var('chromium_git') + '/chromium/mini_chromium@' +
|
||||
'56dd2883170d0df0ec89af0e7862af3f9aaa9be6',
|
||||
'91ea4908ffd74d9c886bd2f8ccbfae6d31c499af',
|
||||
}
|
||||
|
||||
hooks = [
|
||||
|
98
util/thread/thread_log_messages.cc
Normal file
98
util/thread/thread_log_messages.cc
Normal file
@ -0,0 +1,98 @@
|
||||
// Copyright 2015 The Crashpad Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "util/thread/thread_log_messages.h"
|
||||
|
||||
#include "base/lazy_instance.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. 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.
|
||||
//
|
||||
// Practically, the only object of this class that might exist is managed by the
|
||||
// g_master lazy instance, which will create it upon first use.
|
||||
class ThreadLogMessagesMaster {
|
||||
public:
|
||||
ThreadLogMessagesMaster() {
|
||||
DCHECK(!tls_.initialized());
|
||||
CHECK(tls_.Initialize(nullptr));
|
||||
|
||||
DCHECK(!logging::GetLogMessageHandler());
|
||||
logging::SetLogMessageHandler(LogMessageHandler);
|
||||
}
|
||||
|
||||
~ThreadLogMessagesMaster() {
|
||||
DCHECK_EQ(logging::GetLogMessageHandler(), LogMessageHandler);
|
||||
logging::SetLogMessageHandler(nullptr);
|
||||
|
||||
tls_.Free();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private:
|
||||
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>*>(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;
|
||||
}
|
||||
|
||||
static base::ThreadLocalStorage::StaticSlot tls_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(ThreadLogMessagesMaster);
|
||||
};
|
||||
|
||||
// static
|
||||
base::ThreadLocalStorage::StaticSlot ThreadLogMessagesMaster::tls_
|
||||
= TLS_INITIALIZER;
|
||||
|
||||
base::LazyInstance<ThreadLogMessagesMaster>::Leaky g_master =
|
||||
LAZY_INSTANCE_INITIALIZER;
|
||||
|
||||
} // namespace
|
||||
|
||||
ThreadLogMessages::ThreadLogMessages()
|
||||
: log_messages_() {
|
||||
g_master.Get().SetThreadMessageList(&log_messages_);
|
||||
}
|
||||
|
||||
ThreadLogMessages::~ThreadLogMessages() {
|
||||
g_master.Get().SetThreadMessageList(nullptr);
|
||||
}
|
||||
|
||||
} // namespace crashpad
|
48
util/thread/thread_log_messages.h
Normal file
48
util/thread/thread_log_messages.h
Normal file
@ -0,0 +1,48 @@
|
||||
// Copyright 2015 The Crashpad Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef CRASHPAD_UTIL_THREAD_THREAD_LOG_MESSAGES_H_
|
||||
#define CRASHPAD_UTIL_THREAD_THREAD_LOG_MESSAGES_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "base/basictypes.h"
|
||||
|
||||
namespace crashpad {
|
||||
|
||||
//! \brief Captures log messages produced on the current thread during an
|
||||
//! object’s lifetime.
|
||||
//!
|
||||
//! At most one object of this class type may exist on a single thread at a
|
||||
//! time. When using this class, no other part of the program may call
|
||||
//! `logging::SetLogMessageHandler()` at any time.
|
||||
class ThreadLogMessages {
|
||||
public:
|
||||
ThreadLogMessages();
|
||||
~ThreadLogMessages();
|
||||
|
||||
//! \return The log messages collected on the thread that this object was
|
||||
//! created on since the time it was created.
|
||||
const std::vector<std::string>& log_messages() const { return log_messages_; }
|
||||
|
||||
private:
|
||||
std::vector<std::string> log_messages_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(ThreadLogMessages);
|
||||
};
|
||||
|
||||
} // namespace crashpad
|
||||
|
||||
#endif // CRASHPAD_UTIL_THREAD_THREAD_LOG_MESSAGES_H_
|
190
util/thread/thread_log_messages_test.cc
Normal file
190
util/thread/thread_log_messages_test.cc
Normal file
@ -0,0 +1,190 @@
|
||||
// Copyright 2015 The Crashpad Authors. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "util/thread/thread_log_messages.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "base/strings/stringprintf.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "util/test/thread.h"
|
||||
|
||||
namespace crashpad {
|
||||
namespace test {
|
||||
namespace {
|
||||
|
||||
TEST(ThreadLogMessages, Empty) {
|
||||
ThreadLogMessages thread_log_messages;
|
||||
|
||||
const std::vector<std::string>& log_messages =
|
||||
thread_log_messages.log_messages();
|
||||
|
||||
EXPECT_TRUE(log_messages.empty());
|
||||
}
|
||||
|
||||
// For a message formatted like "[preamble] message\n", returns just "message".
|
||||
// If the message is not formatted as expected, a gtest expectation failure will
|
||||
// be recorded and this function will return an empty string.
|
||||
std::string MessageString(const std::string& log_message) {
|
||||
if (log_message.size() < 1) {
|
||||
EXPECT_GE(log_message.size(), 1u);
|
||||
return std::string();
|
||||
}
|
||||
|
||||
const char kStartChar = '[';
|
||||
if (log_message[0] != kStartChar) {
|
||||
EXPECT_EQ(kStartChar, log_message[0]);
|
||||
return std::string();
|
||||
}
|
||||
|
||||
const char kFindString[] = "] ";
|
||||
size_t pos = log_message.find(kFindString);
|
||||
if (pos == std::string::npos) {
|
||||
EXPECT_NE(std::string::npos, pos);
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::string message_string = log_message.substr(pos + strlen(kFindString));
|
||||
if (message_string.size() < 1) {
|
||||
EXPECT_GE(message_string.size(), 1u);
|
||||
return std::string();
|
||||
}
|
||||
|
||||
const char kEndChar = '\n';
|
||||
if (message_string[message_string.size() - 1] != kEndChar) {
|
||||
EXPECT_NE(message_string[message_string.size() - 1], kEndChar);
|
||||
return std::string();
|
||||
}
|
||||
|
||||
message_string.resize(message_string.size() - 1);
|
||||
return message_string;
|
||||
}
|
||||
|
||||
TEST(ThreadLogMessages, Basic) {
|
||||
// Logging must be enabled at least at this level for this test to work.
|
||||
ASSERT_TRUE(LOG_IS_ON(INFO));
|
||||
|
||||
{
|
||||
const char* const kMessages[] = {
|
||||
"An info message",
|
||||
"A warning message",
|
||||
"An error message",
|
||||
};
|
||||
|
||||
ThreadLogMessages thread_log_messages;
|
||||
|
||||
LOG(INFO) << kMessages[0];
|
||||
LOG(WARNING) << kMessages[1];
|
||||
LOG(ERROR) << kMessages[2];
|
||||
|
||||
const std::vector<std::string>& log_messages =
|
||||
thread_log_messages.log_messages();
|
||||
|
||||
EXPECT_EQ(arraysize(kMessages), log_messages.size());
|
||||
for (size_t index = 0; index < arraysize(kMessages); ++index) {
|
||||
EXPECT_EQ(kMessages[index], MessageString(log_messages[index]))
|
||||
<< "index " << index;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const char kMessage[] = "Sample error message";
|
||||
|
||||
ThreadLogMessages thread_log_messages;
|
||||
|
||||
LOG(ERROR) << kMessage;
|
||||
|
||||
const std::vector<std::string>& log_messages =
|
||||
thread_log_messages.log_messages();
|
||||
|
||||
EXPECT_EQ(1u, log_messages.size());
|
||||
EXPECT_EQ(kMessage, MessageString(log_messages[0]));
|
||||
}
|
||||
|
||||
{
|
||||
ThreadLogMessages thread_log_messages;
|
||||
|
||||
LOG(INFO) << "I can't believe I " << "streamed" << " the whole thing.";
|
||||
|
||||
const std::vector<std::string>& log_messages =
|
||||
thread_log_messages.log_messages();
|
||||
|
||||
EXPECT_EQ(1u, log_messages.size());
|
||||
EXPECT_EQ("I can't believe I streamed the whole thing.",
|
||||
MessageString(log_messages[0]));
|
||||
}
|
||||
}
|
||||
|
||||
class LoggingTestThread : public Thread {
|
||||
public:
|
||||
LoggingTestThread() : thread_number_(0), start_(0), count_(0) {}
|
||||
~LoggingTestThread() override {}
|
||||
|
||||
void Initialize(size_t thread_number, int start, int count) {
|
||||
thread_number_ = thread_number;
|
||||
start_ = start;
|
||||
count_ = count;
|
||||
}
|
||||
|
||||
private:
|
||||
void ThreadMain() override {
|
||||
ThreadLogMessages thread_log_messages;
|
||||
|
||||
std::vector<std::string> expected_messages;
|
||||
for (int index = start_; index <= start_ + count_; ++index) {
|
||||
std::string message = base::StringPrintf("message %d", index);
|
||||
expected_messages.push_back(message);
|
||||
LOG(WARNING) << message;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& log_messages =
|
||||
thread_log_messages.log_messages();
|
||||
|
||||
ASSERT_EQ(expected_messages.size(), log_messages.size());
|
||||
for (size_t index = 0; index < log_messages.size(); ++index) {
|
||||
EXPECT_EQ(expected_messages[index], MessageString(log_messages[index]))
|
||||
<< "thread_number_ " << thread_number_ << ", index " << index;
|
||||
}
|
||||
}
|
||||
|
||||
size_t thread_number_;
|
||||
int start_;
|
||||
int count_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(LoggingTestThread);
|
||||
};
|
||||
|
||||
TEST(ThreadLogMessages, Multithreaded) {
|
||||
// Logging must be enabled at least at this level for this test to work.
|
||||
ASSERT_TRUE(LOG_IS_ON(WARNING));
|
||||
|
||||
LoggingTestThread threads[20];
|
||||
int start = 0;
|
||||
for (size_t index = 0; index < arraysize(threads); ++index) {
|
||||
threads[index].Initialize(
|
||||
index, static_cast<int>(start), static_cast<int>(index));
|
||||
start += static_cast<int>(index);
|
||||
|
||||
ASSERT_NO_FATAL_FAILURE(threads[index].Start());
|
||||
}
|
||||
|
||||
for (LoggingTestThread& thread : threads) {
|
||||
thread.Join();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace test
|
||||
} // namespace crashpad
|
@ -135,6 +135,8 @@
|
||||
'synchronization/semaphore_posix.cc',
|
||||
'synchronization/semaphore_win.cc',
|
||||
'synchronization/semaphore.h',
|
||||
'thread/thread_log_messages.cc',
|
||||
'thread/thread_log_messages.h',
|
||||
'win/process_info.cc',
|
||||
'win/process_info.h',
|
||||
'win/process_structs.h',
|
||||
@ -327,6 +329,7 @@
|
||||
'test/multiprocess_posix_test.cc',
|
||||
'test/paths_test.cc',
|
||||
'test/scoped_temp_dir_test.cc',
|
||||
'thread/thread_log_messages_test.cc',
|
||||
'win/process_info_test.cc',
|
||||
'win/time_test.cc',
|
||||
],
|
||||
|
Loading…
x
Reference in New Issue
Block a user