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

Side by Side Diff: chrome/common/ipc_logging.cc

Issue 155905: Separates ipc code from common (http://crbug.com/16829) (Closed)
Patch Set: Fixes reference to 'common_message_traits' it's actually 'common_param_traits' Created 11 years, 5 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 | « chrome/common/ipc_logging.h ('k') | chrome/common/ipc_maybe.h » ('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 (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/common/ipc_logging.h"
6
7 #if defined(OS_POSIX)
8 #ifdef IPC_MESSAGE_LOG_ENABLED
9 // This will cause render_messages.h etc to define ViewMsgLog and friends.
10 #define IPC_MESSAGE_MACROS_LOG_ENABLED
11 #endif
12 #endif
13
14 #include "base/command_line.h"
15 #include "base/logging.h"
16 #include "base/message_loop.h"
17 #include "base/string_util.h"
18 #include "base/thread.h"
19 #include "base/time.h"
20 #include "base/waitable_event.h"
21 #include "base/waitable_event_watcher.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/ipc_sync_message.h"
24 #include "chrome/common/ipc_message_utils.h"
25
26 // This include list should contain all _messages.h header files so that they
27 // can get *MsgLog function etc. This makes ipc logs much more informative.
28 #include "chrome/common/plugin_messages.h"
29 #include "chrome/common/render_messages.h"
30 #include "chrome/common/worker_messages.h"
31 #include "chrome/test/automation/automation_messages.h"
32
33 #if defined(OS_POSIX)
34 #include "base/string_util.h"
35 #include <unistd.h>
36 #endif
37
38 #ifdef IPC_MESSAGE_LOG_ENABLED
39
40 using base::Time;
41
42 // IPC::Logging is allocated as a singleton, so we don't need any kind of
43 // special retention program.
44 template <>
45 struct RunnableMethodTraits<IPC::Logging> {
46 static void RetainCallee(IPC::Logging*) {}
47 static void ReleaseCallee(IPC::Logging*) {}
48 };
49
50 namespace IPC {
51
52 const wchar_t kLoggingEventName[] = L"ChromeIPCLog.%d";
53 const int kLogSendDelayMs = 100;
54
55 // We use a pointer to the function table to avoid any linker dependencies on
56 // all the traits used as IPC message parameters.
57 Logging::LogFunction *Logging::log_function_mapping_;
58
59 Logging::Logging()
60 : logging_event_on_(NULL),
61 logging_event_off_(NULL),
62 enabled_(false),
63 queue_invoke_later_pending_(false),
64 sender_(NULL),
65 main_thread_(MessageLoop::current()),
66 consumer_(NULL) {
67 // Create an event for this browser instance that's set when logging is
68 // enabled, so child processes can know when logging is enabled.
69
70 #if defined(OS_WIN)
71 // On Windows we have a couple of named events which switch logging on and
72 // off.
73 int browser_pid;
74 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
75 std::wstring process_type =
76 parsed_command_line.GetSwitchValue(switches::kProcessType);
77 if (process_type.empty()) {
78 browser_pid = GetCurrentProcessId();
79 } else {
80 std::wstring channel_name =
81 parsed_command_line.GetSwitchValue(switches::kProcessChannelID);
82
83 browser_pid = _wtoi(channel_name.c_str());
84 DCHECK(browser_pid != 0);
85 }
86
87 std::wstring event_name = GetEventName(browser_pid, true);
88 logging_event_on_.reset(new base::WaitableEvent(
89 CreateEvent(NULL, TRUE, FALSE, event_name.c_str())));
90
91 event_name = GetEventName(browser_pid, false);
92 logging_event_off_.reset(new base::WaitableEvent(
93 CreateEvent(NULL, TRUE, FALSE, event_name.c_str())));
94
95 RegisterWaitForEvent(true);
96 #elif defined(OS_POSIX)
97 if (getenv("CHROME_IPC_LOGGING"))
98 enabled_ = true;
99 SetLoggerFunctions(g_log_function_mapping);
100 #endif
101
102 MessageLoop::current()->AddDestructionObserver(this);
103 }
104
105 Logging::~Logging() {
106 }
107
108 Logging* Logging::current() {
109 return Singleton<Logging>::get();
110 }
111
112 void Logging::RegisterWaitForEvent(bool enabled) {
113 watcher_.StopWatching();
114 watcher_.StartWatching(
115 enabled ? logging_event_on_.get() : logging_event_off_.get(), this);
116 }
117
118 void Logging::OnWaitableEventSignaled(base::WaitableEvent* event) {
119 enabled_ = event == logging_event_on_.get();
120 RegisterWaitForEvent(!enabled_);
121 }
122
123 void Logging::WillDestroyCurrentMessageLoop() {
124 watcher_.StopWatching();
125 }
126
127 void Logging::SetLoggerFunctions(LogFunction *functions) {
128 log_function_mapping_ = functions;
129 }
130
131 #if defined(OS_WIN)
132 std::wstring Logging::GetEventName(bool enabled) {
133 return current()->GetEventName(GetCurrentProcessId(), enabled);
134 }
135 #endif
136
137 std::wstring Logging::GetEventName(int browser_pid, bool enabled) {
138 std::wstring result = StringPrintf(kLoggingEventName, browser_pid);
139 result += enabled ? L"on" : L"off";
140 return result;
141 }
142
143 void Logging::SetConsumer(Consumer* consumer) {
144 consumer_ = consumer;
145 }
146
147 void Logging::Enable() {
148 logging_event_off_->Reset();
149 logging_event_on_->Signal();
150 }
151
152 void Logging::Disable() {
153 logging_event_on_->Reset();
154 logging_event_off_->Signal();
155 }
156
157 void Logging::OnSendLogs() {
158 queue_invoke_later_pending_ = false;
159 if (!sender_)
160 return;
161
162 Message* msg = new Message(
163 MSG_ROUTING_CONTROL, IPC_LOGGING_ID, Message::PRIORITY_NORMAL);
164 WriteParam(msg, queued_logs_);
165 queued_logs_.clear();
166 sender_->Send(msg);
167 }
168
169 void Logging::SetIPCSender(IPC::Message::Sender* sender) {
170 sender_ = sender;
171 }
172
173 void Logging::OnReceivedLoggingMessage(const Message& message) {
174 std::vector<LogData> data;
175 void* iter = NULL;
176 if (!ReadParam(&message, &iter, &data))
177 return;
178
179 for (size_t i = 0; i < data.size(); ++i) {
180 Log(data[i]);
181 }
182 }
183
184 void Logging::OnSendMessage(Message* message, const std::string& channel_id) {
185 if (!Enabled())
186 return;
187
188 if (message->is_reply()) {
189 LogData* data = message->sync_log_data();
190 if (!data)
191 return;
192
193 // This is actually the delayed reply to a sync message. Create a string
194 // of the output parameters, add it to the LogData that was earlier stashed
195 // with the reply, and log the result.
196 data->channel = channel_id;
197 GenerateLogData("", *message, data);
198 Log(*data);
199 delete data;
200 message->set_sync_log_data(NULL);
201 } else {
202 // If the time has already been set (i.e. by ChannelProxy), keep that time
203 // instead as it's more accurate.
204 if (!message->sent_time())
205 message->set_sent_time(Time::Now().ToInternalValue());
206 }
207 }
208
209 void Logging::OnPreDispatchMessage(const Message& message) {
210 message.set_received_time(Time::Now().ToInternalValue());
211 }
212
213 void Logging::OnPostDispatchMessage(const Message& message,
214 const std::string& channel_id) {
215 if (!Enabled() ||
216 #if defined(OS_WIN)
217 !message.sent_time() ||
218 #endif
219 message.dont_log())
220 return;
221
222 LogData data;
223 GenerateLogData(channel_id, message, &data);
224
225 if (MessageLoop::current() == main_thread_) {
226 Log(data);
227 } else {
228 main_thread_->PostTask(FROM_HERE, NewRunnableMethod(
229 this, &Logging::Log, data));
230 }
231 }
232
233 void Logging::GetMessageText(uint16 type, std::wstring* name,
234 const Message* message,
235 std::wstring* params) {
236 if (!log_function_mapping_)
237 return;
238
239 int message_class = type >> 12;
240 if (log_function_mapping_[message_class] != NULL) {
241 log_function_mapping_[message_class](type, name, message, params);
242 } else {
243 DLOG(INFO) << "No logger function associated with message class " <<
244 message_class;
245 }
246 }
247
248 void Logging::Log(const LogData& data) {
249 #if defined(OS_WIN)
250 if (consumer_) {
251 // We're in the browser process.
252 consumer_->Log(data);
253 } else {
254 // We're in the renderer or plugin processes.
255 if (sender_) {
256 queued_logs_.push_back(data);
257 if (!queue_invoke_later_pending_) {
258 queue_invoke_later_pending_ = true;
259 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(
260 this, &Logging::OnSendLogs), kLogSendDelayMs);
261 }
262 }
263 }
264 #elif defined(OS_POSIX)
265 // On POSIX, for now, we just dump the log to stderr
266 fprintf(stderr, "ipc %s %d %d %s %s %s\n",
267 data.channel.c_str(),
268 data.routing_id,
269 data.type,
270 WideToUTF8(data.flags).c_str(),
271 WideToUTF8(data.message_name).c_str(),
272 WideToUTF8(data.params).c_str());
273 #endif
274 }
275
276 void GenerateLogData(const std::string& channel, const Message& message,
277 LogData* data) {
278 if (message.is_reply()) {
279 // "data" should already be filled in.
280 std::wstring params;
281 Logging::GetMessageText(data->type, NULL, &message, &params);
282
283 if (!data->params.empty() && !params.empty())
284 data->params += L", ";
285
286 data->flags += L" DR";
287
288 data->params += params;
289 } else {
290 std::wstring flags;
291 if (message.is_sync())
292 flags = L"S";
293
294 if (message.is_reply())
295 flags += L"R";
296
297 if (message.is_reply_error())
298 flags += L"E";
299
300 std::wstring params, message_name;
301 Logging::GetMessageText(message.type(), &message_name, &message, &params);
302
303 data->channel = channel;
304 data->routing_id = message.routing_id();
305 data->type = message.type();
306 data->flags = flags;
307 data->sent = message.sent_time();
308 data->receive = message.received_time();
309 data->dispatch = Time::Now().ToInternalValue();
310 data->params = params;
311 data->message_name = message_name;
312 }
313 }
314
315 }
316
317 #endif // IPC_MESSAGE_LOG_ENABLED
OLDNEW
« no previous file with comments | « chrome/common/ipc_logging.h ('k') | chrome/common/ipc_maybe.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698