OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/ipc_logging.h" | 5 #include "chrome/common/ipc_logging.h" |
6 | 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 |
7 #include "base/command_line.h" | 14 #include "base/command_line.h" |
8 #include "base/logging.h" | 15 #include "base/logging.h" |
9 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
10 #include "base/string_util.h" | 17 #include "base/string_util.h" |
11 #include "base/thread.h" | 18 #include "base/thread.h" |
12 #include "base/time.h" | 19 #include "base/time.h" |
| 20 #include "base/waitable_event.h" |
| 21 #include "base/waitable_event_watcher.h" |
13 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
14 #include "chrome/common/ipc_sync_message.h" | 23 #include "chrome/common/ipc_sync_message.h" |
15 #include "chrome/common/ipc_message_utils.h" | 24 #include "chrome/common/ipc_message_utils.h" |
16 #include "chrome/common/render_messages.h" | 25 #include "chrome/common/render_messages.h" |
| 26 #if defined(OS_WIN) |
| 27 // TODO(port): These messages will need to be ported at some point |
17 #include "chrome/common/plugin_messages.h" | 28 #include "chrome/common/plugin_messages.h" |
| 29 #endif |
| 30 |
| 31 #if defined(OS_POSIX) |
| 32 #include "base/string_util.h" |
| 33 #include <unistd.h> |
| 34 #endif |
18 | 35 |
19 #ifdef IPC_MESSAGE_LOG_ENABLED | 36 #ifdef IPC_MESSAGE_LOG_ENABLED |
20 | 37 |
21 using base::Time; | 38 using base::Time; |
22 | 39 |
23 // IPC::Logging is allocated as a singleton, so we don't need any kind of | 40 // IPC::Logging is allocated as a singleton, so we don't need any kind of |
24 // special retention program. | 41 // special retention program. |
25 template <> | 42 template <> |
26 struct RunnableMethodTraits<IPC::Logging> { | 43 struct RunnableMethodTraits<IPC::Logging> { |
27 static void RetainCallee(IPC::Logging*) {} | 44 static void RetainCallee(IPC::Logging*) {} |
28 static void ReleaseCallee(IPC::Logging*) {} | 45 static void ReleaseCallee(IPC::Logging*) {} |
29 }; | 46 }; |
30 | 47 |
31 namespace IPC { | 48 namespace IPC { |
32 | 49 |
33 const wchar_t kLoggingEventName[] = L"ChromeIPCLog.%d"; | 50 const wchar_t kLoggingEventName[] = L"ChromeIPCLog.%d"; |
34 const int kLogSendDelayMs = 100; | 51 const int kLogSendDelayMs = 100; |
35 | 52 |
36 // We use a pointer to the function table to avoid any linker dependencies on | 53 // We use a pointer to the function table to avoid any linker dependencies on |
37 // all the traits used as IPC message parameters. | 54 // all the traits used as IPC message parameters. |
38 Logging::LogFunction *Logging::log_function_mapping_; | 55 Logging::LogFunction *Logging::log_function_mapping_; |
39 | 56 |
40 Logging::Logging() | 57 Logging::Logging() |
41 : logging_event_on_(NULL), | 58 : logging_event_on_(NULL), |
42 logging_event_off_(NULL), | 59 logging_event_off_(NULL), |
43 enabled_(false), | 60 enabled_(false), |
44 sender_(NULL), | |
45 consumer_(NULL), | |
46 queue_invoke_later_pending_(false), | 61 queue_invoke_later_pending_(false), |
47 main_thread_(MessageLoop::current()) { | 62 main_thread_(MessageLoop::current()) { |
48 // Create an event for this browser instance that's set when logging is | 63 // Create an event for this browser instance that's set when logging is |
49 // enabled, so child processes can know when logging is enabled. | 64 // enabled, so child processes can know when logging is enabled. |
| 65 |
| 66 #if defined(OS_WIN) |
| 67 // On Windows we have a couple of named events which switch logging on and |
| 68 // off. |
50 int browser_pid; | 69 int browser_pid; |
51 | |
52 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); | 70 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); |
53 std::wstring process_type = | 71 std::wstring process_type = |
54 parsed_command_line.GetSwitchValue(switches::kProcessType); | 72 parsed_command_line.GetSwitchValue(switches::kProcessType); |
55 if (process_type.empty()) { | 73 if (process_type.empty()) { |
56 browser_pid = GetCurrentProcessId(); | 74 browser_pid = GetCurrentProcessId(); |
57 } else { | 75 } else { |
58 std::wstring channel_name = | 76 std::wstring channel_name = |
59 parsed_command_line.GetSwitchValue(switches::kProcessChannelID); | 77 parsed_command_line.GetSwitchValue(switches::kProcessChannelID); |
60 | 78 |
61 browser_pid = _wtoi(channel_name.c_str()); | 79 browser_pid = _wtoi(channel_name.c_str()); |
62 DCHECK(browser_pid != 0); | 80 DCHECK(browser_pid != 0); |
63 } | 81 } |
64 | 82 |
65 std::wstring event_name = GetEventName(browser_pid, true); | 83 std::wstring event_name = GetEventName(browser_pid, true); |
66 logging_event_on_ = CreateEvent(NULL, TRUE, FALSE, event_name.c_str()); | 84 logging_event_on_ = new base::WaitableEvent( |
| 85 CreateEvent(NULL, TRUE, FALSE, event_name.c_str())); |
67 | 86 |
68 event_name = GetEventName(browser_pid, false); | 87 event_name = GetEventName(browser_pid, false); |
69 logging_event_off_ = CreateEvent(NULL, TRUE, FALSE, event_name.c_str()); | 88 logging_event_off_ = new base::WaitableEvent( |
| 89 CreateEvent(NULL, TRUE, FALSE, event_name.c_str())); |
70 | 90 |
71 RegisterWaitForEvent(true); | 91 RegisterWaitForEvent(true); |
| 92 #elif defined(OS_POSIX) |
| 93 if (getenv("CHROME_IPC_LOGGING")) |
| 94 enabled_ = true; |
| 95 SetLoggerFunctions(g_log_function_mapping); |
| 96 #endif |
72 } | 97 } |
73 | 98 |
74 Logging::~Logging() { | 99 Logging::~Logging() { |
75 watcher_.StopWatching(); | 100 watcher_.StopWatching(); |
76 CloseHandle(logging_event_on_); | |
77 CloseHandle(logging_event_off_); | |
78 } | 101 } |
79 | 102 |
80 Logging* Logging::current() { | 103 Logging* Logging::current() { |
81 return Singleton<Logging>::get(); | 104 return Singleton<Logging>::get(); |
82 } | 105 } |
83 | 106 |
84 void Logging::RegisterWaitForEvent(bool enabled) { | 107 void Logging::RegisterWaitForEvent(bool enabled) { |
85 watcher_.StopWatching(); | 108 watcher_.StopWatching(); |
86 watcher_.StartWatching( | 109 watcher_.StartWatching( |
87 enabled ? logging_event_on_ : logging_event_off_, this); | 110 enabled ? logging_event_on_.get() : logging_event_off_.get(), this); |
88 } | 111 } |
89 | 112 |
90 void Logging::OnObjectSignaled(HANDLE object) { | 113 void Logging::OnWaitableEventSignaled(base::WaitableEvent* event) { |
91 enabled_ = object == logging_event_on_; | 114 enabled_ = event == logging_event_on_.get(); |
92 RegisterWaitForEvent(!enabled_); | 115 RegisterWaitForEvent(!enabled_); |
93 } | 116 } |
94 | 117 |
95 void Logging::SetLoggerFunctions(LogFunction *functions) { | 118 void Logging::SetLoggerFunctions(LogFunction *functions) { |
96 log_function_mapping_ = functions; | 119 log_function_mapping_ = functions; |
97 } | 120 } |
98 | 121 |
| 122 #if defined(OS_WIN) |
99 std::wstring Logging::GetEventName(bool enabled) { | 123 std::wstring Logging::GetEventName(bool enabled) { |
100 return current()->GetEventName(GetCurrentProcessId(), enabled); | 124 return current()->GetEventName(GetCurrentProcessId(), enabled); |
101 } | 125 } |
| 126 #endif |
102 | 127 |
103 std::wstring Logging::GetEventName(int browser_pid, bool enabled) { | 128 std::wstring Logging::GetEventName(int browser_pid, bool enabled) { |
104 std::wstring result = StringPrintf(kLoggingEventName, browser_pid); | 129 std::wstring result = StringPrintf(kLoggingEventName, browser_pid); |
105 result += enabled ? L"on" : L"off"; | 130 result += enabled ? L"on" : L"off"; |
106 return result; | 131 return result; |
107 } | 132 } |
108 | 133 |
109 void Logging::SetConsumer(Consumer* consumer) { | 134 void Logging::SetConsumer(Consumer* consumer) { |
110 consumer_ = consumer; | 135 consumer_ = consumer; |
111 } | 136 } |
112 | 137 |
113 void Logging::Enable() { | 138 void Logging::Enable() { |
114 ResetEvent(logging_event_off_); | 139 logging_event_off_->Reset(); |
115 SetEvent(logging_event_on_); | 140 logging_event_on_->Signal(); |
116 } | 141 } |
117 | 142 |
118 void Logging::Disable() { | 143 void Logging::Disable() { |
119 ResetEvent(logging_event_on_); | 144 logging_event_on_->Reset(); |
120 SetEvent(logging_event_off_); | 145 logging_event_off_->Signal(); |
121 } | |
122 | |
123 inline bool Logging::Enabled() const { | |
124 return enabled_; | |
125 } | 146 } |
126 | 147 |
127 void Logging::OnSendLogs() { | 148 void Logging::OnSendLogs() { |
128 queue_invoke_later_pending_ = false; | 149 queue_invoke_later_pending_ = false; |
129 if (!sender_) | 150 if (!sender_) |
130 return; | 151 return; |
131 | 152 |
132 Message* msg = new Message( | 153 Message* msg = new Message( |
133 MSG_ROUTING_CONTROL, IPC_LOGGING_ID, Message::PRIORITY_NORMAL); | 154 MSG_ROUTING_CONTROL, IPC_LOGGING_ID, Message::PRIORITY_NORMAL); |
134 WriteParam(msg, queued_logs_); | 155 WriteParam(msg, queued_logs_); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 message->set_sent_time(Time::Now().ToInternalValue()); | 196 message->set_sent_time(Time::Now().ToInternalValue()); |
176 } | 197 } |
177 } | 198 } |
178 | 199 |
179 void Logging::OnPreDispatchMessage(const Message& message) { | 200 void Logging::OnPreDispatchMessage(const Message& message) { |
180 message.set_received_time(Time::Now().ToInternalValue()); | 201 message.set_received_time(Time::Now().ToInternalValue()); |
181 } | 202 } |
182 | 203 |
183 void Logging::OnPostDispatchMessage(const Message& message, | 204 void Logging::OnPostDispatchMessage(const Message& message, |
184 const std::wstring& channel_id) { | 205 const std::wstring& channel_id) { |
185 if (!Enabled() || !message.sent_time() || message.dont_log()) | 206 if (!Enabled() || message.dont_log()) |
186 return; | 207 return; |
187 | 208 |
188 LogData data; | 209 LogData data; |
189 GenerateLogData(channel_id, message, &data); | 210 GenerateLogData(channel_id, message, &data); |
190 | 211 |
191 if (MessageLoop::current() == main_thread_) { | 212 if (MessageLoop::current() == main_thread_) { |
192 Log(data); | 213 Log(data); |
193 } else { | 214 } else { |
194 main_thread_->PostTask(FROM_HERE, NewRunnableMethod( | 215 main_thread_->PostTask(FROM_HERE, NewRunnableMethod( |
195 this, &Logging::Log, data)); | 216 this, &Logging::Log, data)); |
196 } | 217 } |
197 } | 218 } |
198 | 219 |
199 void Logging::GetMessageText(uint16 type, std::wstring* name, | 220 void Logging::GetMessageText(uint16 type, std::wstring* name, |
200 const Message* message, | 221 const Message* message, |
201 std::wstring* params) { | 222 std::wstring* params) { |
202 if (!log_function_mapping_) | 223 if (!log_function_mapping_) |
203 return; | 224 return; |
204 | 225 |
205 int message_class = type >> 12; | 226 int message_class = type >> 12; |
206 if (log_function_mapping_[message_class] != NULL) { | 227 if (log_function_mapping_[message_class] != NULL) { |
207 log_function_mapping_[message_class](type, name, message, params); | 228 log_function_mapping_[message_class](type, name, message, params); |
208 } else { | 229 } else { |
209 DLOG(INFO) << "No logger function associated with message class " << | 230 DLOG(INFO) << "No logger function associated with message class " << |
210 message_class; | 231 message_class; |
211 } | 232 } |
212 } | 233 } |
213 | 234 |
214 void Logging::Log(const LogData& data) { | 235 void Logging::Log(const LogData& data) { |
| 236 #if defined(OS_WIN) |
215 if (consumer_) { | 237 if (consumer_) { |
216 // We're in the browser process. | 238 // We're in the browser process. |
217 consumer_->Log(data); | 239 consumer_->Log(data); |
218 } else { | 240 } else { |
219 // We're in the renderer or plugin processes. | 241 // We're in the renderer or plugin processes. |
220 if (sender_) { | 242 if (sender_) { |
221 queued_logs_.push_back(data); | 243 queued_logs_.push_back(data); |
222 if (!queue_invoke_later_pending_) { | 244 if (!queue_invoke_later_pending_) { |
223 queue_invoke_later_pending_ = true; | 245 queue_invoke_later_pending_ = true; |
224 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod( | 246 MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod( |
225 this, &Logging::OnSendLogs), kLogSendDelayMs); | 247 this, &Logging::OnSendLogs), kLogSendDelayMs); |
226 } | 248 } |
227 } | 249 } |
228 } | 250 } |
| 251 #elif defined(OS_POSIX) |
| 252 // On POSIX, for now, we just dump the log to stderr |
| 253 fprintf(stderr, "ipc %s %d %s %s %s\n", |
| 254 WideToUTF8(data.channel).c_str(), |
| 255 data.type, |
| 256 WideToUTF8(data.flags).c_str(), |
| 257 WideToUTF8(data.message_name).c_str(), |
| 258 WideToUTF8(data.params).c_str()); |
| 259 #endif |
229 } | 260 } |
230 | 261 |
231 void GenerateLogData(const std::wstring& channel, const Message& message, | 262 void GenerateLogData(const std::wstring& channel, const Message& message, |
232 LogData* data) { | 263 LogData* data) { |
233 if (message.is_reply()) { | 264 if (message.is_reply()) { |
234 // "data" should already be filled in. | 265 // "data" should already be filled in. |
235 std::wstring params; | 266 std::wstring params; |
236 Logging::GetMessageText(data->type, NULL, &message, ¶ms); | 267 Logging::GetMessageText(data->type, NULL, &message, ¶ms); |
237 | 268 |
238 if (!data->params.empty() && !params.empty()) | 269 if (!data->params.empty() && !params.empty()) |
239 data->params += L", "; | 270 data->params += L", "; |
240 | 271 |
241 data->flags += L" DR"; | 272 data->flags += L" DR"; |
242 | 273 |
243 data->params += params; | 274 data->params += params; |
244 } else { | 275 } else { |
245 std::wstring flags; | 276 std::wstring flags; |
246 if (message.is_sync()) | 277 if (message.is_sync()) |
247 flags = L"S"; | 278 flags = L"S"; |
248 | 279 |
249 if (message.is_reply()) | 280 if (message.is_reply()) |
250 flags += L"R"; | 281 flags += L"R"; |
251 | 282 |
252 if (message.is_reply_error()) | 283 if (message.is_reply_error()) |
253 flags += L"E"; | 284 flags += L"E"; |
254 | 285 |
255 std::wstring params; | 286 std::wstring params, message_name; |
256 Logging::GetMessageText(message.type(), NULL, &message, ¶ms); | 287 Logging::GetMessageText(message.type(), &message_name, &message, ¶ms); |
257 | 288 |
258 data->channel = channel; | 289 data->channel = channel; |
259 data->type = message.type(); | 290 data->type = message.type(); |
260 data->flags = flags; | 291 data->flags = flags; |
261 data->sent = message.sent_time(); | 292 data->sent = message.sent_time(); |
262 data->receive = message.received_time(); | 293 data->receive = message.received_time(); |
263 data->dispatch = Time::Now().ToInternalValue(); | 294 data->dispatch = Time::Now().ToInternalValue(); |
264 data->params = params; | 295 data->params = params; |
| 296 data->message_name = message_name; |
265 } | 297 } |
266 } | 298 } |
267 | 299 |
268 } | 300 } |
269 | 301 |
270 #endif // IPC_MESSAGE_LOG_ENABLED | 302 #endif // IPC_MESSAGE_LOG_ENABLED |
OLD | NEW |