Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: util/thread/thread_log_messages.cc

Issue 1041643003: Add ThreadLogMessages and its test (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Update mini_chromium to 91ea4908ffd7 Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/thread/thread_log_messages.h ('k') | util/thread/thread_log_messages_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/thread/thread_log_messages.h"
16
17 #include "base/lazy_instance.h"
18 #include "base/logging.h"
19 #include "base/threading/thread_local_storage.h"
20
21 namespace crashpad {
22
23 namespace {
24
25 // While an object of this class exists, it will be set as the log message
26 // handler. A thread may register its thread-specific log message list to
27 // receive messages produced just on that thread.
28 //
29 // Only one object of this class may exist in the program at a time. There must
30 // not be any log message handler in effect when it is created, and nothing else
31 // can be set as a log message handler while an object of this class exists.
32 //
33 // Practically, the only object of this class that might exist is managed by the
34 // g_master lazy instance, which will create it upon first use.
35 class ThreadLogMessagesMaster {
36 public:
37 ThreadLogMessagesMaster() {
38 DCHECK(!tls_.initialized());
39 CHECK(tls_.Initialize(nullptr));
40
41 DCHECK(!logging::GetLogMessageHandler());
42 logging::SetLogMessageHandler(LogMessageHandler);
43 }
44
45 ~ThreadLogMessagesMaster() {
46 DCHECK_EQ(logging::GetLogMessageHandler(), LogMessageHandler);
47 logging::SetLogMessageHandler(nullptr);
48
49 tls_.Free();
50 }
51
52 void SetThreadMessageList(std::vector<std::string>* message_list) {
53 DCHECK_EQ(logging::GetLogMessageHandler(), LogMessageHandler);
54 DCHECK_NE(tls_.Get() != nullptr, message_list != nullptr);
55 tls_.Set(message_list);
56 }
57
58 private:
59 static bool LogMessageHandler(logging::LogSeverity severity,
60 const char* file_path,
61 int line,
62 size_t message_start,
63 const std::string& string) {
64 std::vector<std::string>* log_messages =
65 reinterpret_cast<std::vector<std::string>*>(tls_.Get());
66 if (log_messages) {
67 log_messages->push_back(string);
68 }
69
70 // Don’t consume the message. Allow it to be logged as if nothing was set as
71 // the log message handler.
72 return false;
73 }
74
75 static base::ThreadLocalStorage::StaticSlot tls_;
76
77 DISALLOW_COPY_AND_ASSIGN(ThreadLogMessagesMaster);
78 };
79
80 // static
81 base::ThreadLocalStorage::StaticSlot ThreadLogMessagesMaster::tls_
82 = TLS_INITIALIZER;
83
84 base::LazyInstance<ThreadLogMessagesMaster>::Leaky g_master =
85 LAZY_INSTANCE_INITIALIZER;
86
87 } // namespace
88
89 ThreadLogMessages::ThreadLogMessages()
90 : log_messages_() {
91 g_master.Get().SetThreadMessageList(&log_messages_);
92 }
93
94 ThreadLogMessages::~ThreadLogMessages() {
95 g_master.Get().SetThreadMessageList(nullptr);
96 }
97
98 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/thread/thread_log_messages.h ('k') | util/thread/thread_log_messages_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698