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

Side by Side Diff: chrome/test/chromedriver/chrome/console_logger.cc

Issue 14591005: C++ readability review from original change https://chromiumcodereview.appspot.com/14263024/ (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Update to head. Created 7 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 (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/test/chromedriver/chrome/console_logger.h" 5 #include "chrome/test/chromedriver/chrome/console_logger.h"
6 6
7 #include <sstream> 7 #include <sstream>
8 8
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "base/values.h" 11 #include "base/values.h"
12 #include "chrome/test/chromedriver/chrome/devtools_client.h" 12 #include "chrome/test/chromedriver/chrome/devtools_client.h"
13 #include "chrome/test/chromedriver/chrome/log.h" 13 #include "chrome/test/chromedriver/chrome/log.h"
14 #include "chrome/test/chromedriver/chrome/status.h" 14 #include "chrome/test/chromedriver/chrome/status.h"
15 15
16 namespace { 16 namespace {
17 17
18 bool ConsoleLevelToLogLevel(const std::string& name, Log::Level *out_level) { 18 bool ConsoleLevelToLogLevel(const std::string& name, Log::Level *out_level) {
19 const char* kConsoleLevelNames[] = { 19 const char* kConsoleLevelNames[] = {
jdennett 2013/05/13 17:56:49 This is named as a constant, but isn't const; it p
klm 2013/05/13 20:35:57 Done.
20 "debug", "log", "warning", "error" 20 "debug", "log", "warning", "error"
21 }; 21 };
22 22
23 for (size_t i = 0; i < arraysize(kConsoleLevelNames); ++i) { 23 for (size_t i = 0; i < arraysize(kConsoleLevelNames); ++i) {
24 if (name == kConsoleLevelNames[i]) { 24 if (name == kConsoleLevelNames[i]) {
25 CHECK(Log::kDebug + i <= Log::kError); 25 CHECK(Log::kDebug + i <= Log::kError);
jdennett 2013/05/13 17:56:49 CHECK_LE(Log::kDebug + i, Log::kError);
klm 2013/05/13 20:35:57 Done.
26 *out_level = static_cast<Log::Level>(Log::kDebug + i); 26 *out_level = static_cast<Log::Level>(Log::kDebug + i);
27 return true; 27 return true;
28 } 28 }
29 } 29 }
30 return false; 30 return false;
31 } 31 }
32 32
33 } // namespace 33 } // namespace
34 34
35 ConsoleLogger::ConsoleLogger(Log* log) 35 ConsoleLogger::ConsoleLogger(Log* log)
36 : log_(log) {} 36 : log_(log) {}
37 37
38 Status ConsoleLogger::OnConnected(DevToolsClient* client) { 38 Status ConsoleLogger::OnConnected(DevToolsClient* client) {
39 base::DictionaryValue params; 39 base::DictionaryValue params;
40 return client->SendCommand("Console.enable", params); 40 return client->SendCommand("Console.enable", params);
41 } 41 }
42 42
43 void ConsoleLogger::OnEvent( 43 void ConsoleLogger::OnEvent(
44 DevToolsClient* client, 44 DevToolsClient* client,
45 const std::string& method, 45 const std::string& method,
46 const base::DictionaryValue& params) { 46 const base::DictionaryValue& params) {
47 if (!StartsWithASCII(method, "Console.messageAdded", true)) 47 if (!StartsWithASCII(method, "Console.messageAdded", true))
48 return; 48 return;
49 49
50 // If the event has proper structure and fields, log formatted. 50 // If the event has proper structure and fields, log formatted.
51 // Else it's a weird message that we don't know how to format, log full JSON. 51 // Else it's a weird message that we don't know how to format, log full JSON.
52 const base::DictionaryValue *message_dict = NULL; 52 const base::DictionaryValue *message_dict = NULL;
jdennett 2013/05/13 17:56:49 Our style guide prefers nullptr over NULL for new
klm 2013/05/13 20:35:57 I have codesearched for it specifically, and seems
kkania 2013/05/13 21:30:22 Yep, use NULL
53 if (params.GetDictionary("message", &message_dict)) { 53 if (params.GetDictionary("message", &message_dict)) {
54 std::ostringstream message; 54 std::ostringstream message;
jdennett 2013/05/13 17:56:49 Google's style guide disallows stringstreams, pref
klm 2013/05/13 20:35:57 When I codesearched for how Chromium constructs st
kkania 2013/05/13 21:30:22 Yes, we don't have StrCat and I don't think there'
klm 2013/05/14 03:34:47 Done.
55 std::string origin; 55 std::string origin;
56 if (message_dict->GetString("url", &origin) && !origin.empty()) { 56 if (message_dict->GetString("url", &origin) && !origin.empty()) {
57 message << origin; 57 message << origin;
58 } else if (message_dict->GetString("source", &origin) && !origin.empty()) { 58 } else if (message_dict->GetString("source", &origin) && !origin.empty()) {
59 message << origin; 59 message << origin;
60 } else { 60 } else {
61 message << "unknown"; 61 message << "unknown";
62 } 62 }
63 63
64 int line = -1; 64 int line = -1;
(...skipping 22 matching lines...) Expand all
87 } 87 }
88 } 88 }
89 } 89 }
90 } 90 }
91 91
92 // Don't know how to format, log full JSON. 92 // Don't know how to format, log full JSON.
93 std::string message_json; 93 std::string message_json;
94 base::JSONWriter::Write(&params, &message_json); 94 base::JSONWriter::Write(&params, &message_json);
95 log_->AddEntry(Log::kWarning, message_json); 95 log_->AddEntry(Log::kWarning, message_json);
96 } 96 }
97
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698