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

Side by Side Diff: blimp/common/logging.cc

Issue 1933053003: Used oneof in blimp_message.proto (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "blimp/common/logging.h" 5 #include "blimp/common/logging.h"
6 6
7 #include <iostream> 7 #include <iostream>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 250
251 // No fields are extracted from |message|. 251 // No fields are extracted from |message|.
252 class NullLogExtractor : public LogExtractor { 252 class NullLogExtractor : public LogExtractor {
253 void ExtractFields(const BlimpMessage& message, 253 void ExtractFields(const BlimpMessage& message,
254 LogFields* output) const override {} 254 LogFields* output) const override {}
255 }; 255 };
256 256
257 } // namespace 257 } // namespace
258 258
259 BlimpMessageLogger::BlimpMessageLogger() { 259 BlimpMessageLogger::BlimpMessageLogger() {
260 AddHandler("COMPOSITOR", BlimpMessage::COMPOSITOR, 260 AddHandler("COMPOSITOR", BlimpMessage::kCompositor,
261 base::WrapUnique(new CompositorLogExtractor)); 261 base::WrapUnique(new CompositorLogExtractor));
262 AddHandler("INPUT", BlimpMessage::INPUT, 262 AddHandler("INPUT", BlimpMessage::kInput,
263 base::WrapUnique(new InputLogExtractor)); 263 base::WrapUnique(new InputLogExtractor));
264 AddHandler("NAVIGATION", BlimpMessage::NAVIGATION, 264 AddHandler("NAVIGATION", BlimpMessage::kNavigation,
265 base::WrapUnique(new NavigationLogExtractor)); 265 base::WrapUnique(new NavigationLogExtractor));
266 AddHandler("PROTOCOL_CONTROL", BlimpMessage::PROTOCOL_CONTROL, 266 AddHandler("PROTOCOL_CONTROL", BlimpMessage::kProtocolControl,
267 base::WrapUnique(new ProtocolControlLogExtractor)); 267 base::WrapUnique(new ProtocolControlLogExtractor));
268 AddHandler("RENDER_WIDGET", BlimpMessage::RENDER_WIDGET, 268 AddHandler("RENDER_WIDGET", BlimpMessage::kRenderWidget,
269 base::WrapUnique(new RenderWidgetLogExtractor)); 269 base::WrapUnique(new RenderWidgetLogExtractor));
270 AddHandler("SETTINGS", BlimpMessage::SETTINGS, 270 AddHandler("SETTINGS", BlimpMessage::kSettings,
271 base::WrapUnique(new SettingsLogExtractor)); 271 base::WrapUnique(new SettingsLogExtractor));
272 AddHandler("TAB_CONTROL", BlimpMessage::TAB_CONTROL, 272 AddHandler("TAB_CONTROL", BlimpMessage::kTabControl,
273 base::WrapUnique(new TabControlLogExtractor)); 273 base::WrapUnique(new TabControlLogExtractor));
274 } 274 }
275 275
276 BlimpMessageLogger::~BlimpMessageLogger() {} 276 BlimpMessageLogger::~BlimpMessageLogger() {}
277 277
278 void BlimpMessageLogger::AddHandler(const std::string& type_name, 278 void BlimpMessageLogger::AddHandler(const std::string& type_name,
279 BlimpMessage::Type type, 279 BlimpMessage::FeatureCase type,
Kevin M 2016/05/02 17:47:48 I'm no naming savant but it seems like a mishmash
shaktisahu 2016/05/16 20:19:02 hmm.. I am open for suggestions. ++wez@ By the way
280 std::unique_ptr<LogExtractor> extractor) { 280 std::unique_ptr<LogExtractor> extractor) {
281 DCHECK(extractors_.find(type) == extractors_.end()); 281 DCHECK(extractors_.find(type) == extractors_.end());
282 DCHECK(!type_name.empty()); 282 DCHECK(!type_name.empty());
283 extractors_[type] = make_pair(type_name, std::move(extractor)); 283 extractors_[type] = make_pair(type_name, std::move(extractor));
284 } 284 }
285 285
286 void BlimpMessageLogger::LogMessageToStream(const BlimpMessage& message, 286 void BlimpMessageLogger::LogMessageToStream(const BlimpMessage& message,
287 std::ostream* out) const { 287 std::ostream* out) const {
288 LogFields fields; 288 LogFields fields;
289 289
290 auto extractor = extractors_.find(message.type()); 290 auto extractor = extractors_.find(message.feature_case());
291 if (extractor != extractors_.end()) { 291 if (extractor != extractors_.end()) {
292 // An extractor is registered for |message|. 292 // An extractor is registered for |message|.
293 // Add the human-readable name of |message.type|. 293 // Add the human-readable name of |message.type|.
294 fields.push_back(make_pair("type", extractor->second.first)); 294 fields.push_back(make_pair("type", extractor->second.first));
295 extractor->second.second->ExtractFields(message, &fields); 295 extractor->second.second->ExtractFields(message, &fields);
296 } else { 296 } else {
297 // Don't know the human-readable name of |message.type|. 297 // Don't know the human-readable name of |message.type|.
298 // Just represent it using its numeric form instead. 298 // Just represent it using its numeric form instead.
299 AddField("type", message.type(), &fields); 299 AddField("type", message.feature_case(), &fields);
300 } 300 }
301 301
302 // Append "target_tab_id" (if present) and "byte_size" to the field set. 302 // Append "target_tab_id" (if present) and "byte_size" to the field set.
303 if (message.has_target_tab_id()) { 303 if (message.has_target_tab_id()) {
304 AddField("target_tab_id", message.target_tab_id(), &fields); 304 AddField("target_tab_id", message.target_tab_id(), &fields);
305 } 305 }
306 AddField("byte_size", message.ByteSize(), &fields); 306 AddField("byte_size", message.ByteSize(), &fields);
307 307
308 // Format message using the syntax: 308 // Format message using the syntax:
309 // <BlimpMessage field1=value1 field2="value 2"> 309 // <BlimpMessage field1=value1 field2="value 2">
310 *out << "<BlimpMessage "; 310 *out << "<BlimpMessage ";
311 for (size_t i = 0; i < fields.size(); ++i) { 311 for (size_t i = 0; i < fields.size(); ++i) {
312 *out << fields[i].first << "=" << fields[i].second 312 *out << fields[i].first << "=" << fields[i].second
313 << (i != fields.size() - 1 ? " " : ""); 313 << (i != fields.size() - 1 ? " " : "");
314 } 314 }
315 *out << ">"; 315 *out << ">";
316 } 316 }
317 317
318 std::ostream& operator<<(std::ostream& out, const BlimpMessage& message) { 318 std::ostream& operator<<(std::ostream& out, const BlimpMessage& message) {
319 g_logger.Get().LogMessageToStream(message, &out); 319 g_logger.Get().LogMessageToStream(message, &out);
320 return out; 320 return out;
321 } 321 }
322 322
323 } // namespace blimp 323 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698