1 // Copyright (c) 2012 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.
5 #include "chrome/test/chromedriver/logging.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
10 #include "chrome/test/chromedriver/capabilities.h"
11 #include "chrome/test/chromedriver/chrome/console_logger.h"
12 #include "chrome/test/chromedriver/chrome/performance_logger.h"
13 #include "chrome/test/chromedriver/chrome/status.h"
18 // Map between WebDriverLog::WebDriverLevel and its name in WD wire protocol.
19 // Array indices are the WebDriverLog::WebDriverLevel enum values.
20 const char* kWebDriverLevelNames
[] = {
21 "ALL", "DEBUG", "INFO", "WARNING", "SEVERE", "OFF"
24 // Map between Log::Level and WebDriverLog::WebDriverLevel.
25 // Array indices are the Log::Level enum values.
26 WebDriverLog::WebDriverLevel kLogLevelToWebDriverLevels
[] = {
27 WebDriverLog::kWdDebug
, // kDebug
28 WebDriverLog::kWdInfo
, // kLog
29 WebDriverLog::kWdWarning
, // kWarning
30 WebDriverLog::kWdSevere
// kError
33 WebDriverLog::WebDriverLevel
LogLevelToWebDriverLevel(Log::Level level
) {
34 const int index
= level
- Log::kDebug
;
36 CHECK(static_cast<size_t>(index
) < arraysize(kLogLevelToWebDriverLevels
));
37 return kLogLevelToWebDriverLevels
[index
];
40 std::string
GetWebDriverLevelName(
41 const WebDriverLog::WebDriverLevel level
) {
42 const int index
= level
- WebDriverLog::kWdAll
;
44 CHECK(static_cast<size_t>(index
) < arraysize(kWebDriverLevelNames
));
45 return kWebDriverLevelNames
[index
];
50 bool WebDriverLog::NameToLevel(
51 const std::string
& name
, WebDriverLog::WebDriverLevel
* out_level
) {
52 for (size_t i
= 0; i
< arraysize(kWebDriverLevelNames
); ++i
) {
53 if (name
== kWebDriverLevelNames
[i
]) {
54 CHECK(WebDriverLog::kWdAll
+ i
<= WebDriverLog::kWdOff
);
56 static_cast<WebDriverLog::WebDriverLevel
>(WebDriverLog::kWdAll
+ i
);
63 WebDriverLog::WebDriverLog(
64 const std::string
& type
, WebDriverLog::WebDriverLevel min_wd_level
)
66 min_wd_level_(min_wd_level
),
67 entries_(new base::ListValue()) {
68 VLOG(1) << "Log(" << type_
<< ", " << min_wd_level_
<< ")";
71 WebDriverLog::~WebDriverLog() {
72 VLOG(1) << "Log type '" << type_
<< "' lost "
73 << entries_
->GetSize() << " entries on destruction";
76 const std::string
& WebDriverLog::GetType() {
80 void WebDriverLog::AddEntry(const base::Time
& time
,
82 const std::string
& message
) {
83 const WebDriverLog::WebDriverLevel wd_level
= LogLevelToWebDriverLevel(level
);
84 if (wd_level
< min_wd_level_
)
86 scoped_ptr
<base::DictionaryValue
> log_entry_dict(new base::DictionaryValue());
87 log_entry_dict
->SetDouble("timestamp", static_cast<int64
>(time
.ToJsTime()));
88 log_entry_dict
->SetString("level", GetWebDriverLevelName(wd_level
));
89 log_entry_dict
->SetString("message", message
);
90 entries_
->Append(log_entry_dict
.release());
93 scoped_ptr
<base::ListValue
> WebDriverLog::GetAndClearEntries() {
94 scoped_ptr
<base::ListValue
> ret(entries_
.release());
95 entries_
.reset(new base::ListValue());
99 Status
CreateLogs(const Capabilities
& capabilities
,
100 ScopedVector
<WebDriverLog
>* out_devtools_logs
,
101 ScopedVector
<DevToolsEventListener
>* out_listeners
) {
102 ScopedVector
<WebDriverLog
> devtools_logs
;
103 ScopedVector
<DevToolsEventListener
> listeners
;
104 WebDriverLog::WebDriverLevel browser_log_level
= WebDriverLog::kWdInfo
;
106 if (capabilities
.logging_prefs
) {
107 for (DictionaryValue::Iterator
pref(*capabilities
.logging_prefs
);
108 !pref
.IsAtEnd(); pref
.Advance()) {
109 const std::string type
= pref
.key();
110 std::string level_name
;
111 if (!pref
.value().GetAsString(&level_name
)) {
112 return Status(kUnknownError
,
113 "logging level must be a string for log type: " + type
);
115 WebDriverLog::WebDriverLevel level
= WebDriverLog::kWdOff
;
116 if (!WebDriverLog::NameToLevel(level_name
, &level
)) {
117 return Status(kUnknownError
,
118 "invalid log level \"" + level_name
+
119 "\" for type: " + type
);
121 if ("performance" == type
) {
122 if (WebDriverLog::kWdOff
!= level
) {
123 WebDriverLog
* log
= new WebDriverLog(type
, WebDriverLog::kWdAll
);
124 devtools_logs
.push_back(log
);
125 listeners
.push_back(new PerformanceLogger(log
));
127 } else if ("browser" == type
) {
128 browser_log_level
= level
;
130 // Driver "should" ignore unrecognized log types, per Selenium tests.
131 // For example the Java client passes the "client" log type in the caps,
132 // which the server should never provide.
133 LOG(WARNING
) << "Ignoring unrecognized log type: LoggingPrefs." << type
;
137 // Create "browser" log -- should always exist.
138 WebDriverLog
* browser_log
= new WebDriverLog("browser", browser_log_level
);
139 devtools_logs
.push_back(browser_log
);
140 // If the level is OFF, don't even bother listening for DevTools events.
141 if (browser_log_level
!= WebDriverLog::kWdOff
)
142 listeners
.push_back(new ConsoleLogger(browser_log
));
144 out_devtools_logs
->swap(devtools_logs
);
145 out_listeners
->swap(listeners
);