Bug 1482829 - Convert logging::Level into mozprofile::preferences::Pref. r=whimboo

In order to facilitate the use of Log.jsm's Logger#manageLevelFromPref,
geckodriver needs to ensure that the input value for the
marionette.log.level preference conforms to the variants in the
Log.Level enum.

This patch implements the Into<T> conversion from geckodriver's
logging::Level into mozprofile::preferences::Pref by way of a new
function to_gecko(), that ensures the preference value is correctly
formatted.

Logger#manageLevelFromPref expects a string value such as "Info",
which exactly matches Log.Level's own properties.  It is in other
words case sensitive, and this ensures that Marionette no longer
has to case convert the input data.
This commit is contained in:
Andreas Tolfsen 2018-08-14 15:38:43 +01:00
Родитель 8f1386a045
Коммит 658ee95d04
2 изменённых файлов: 59 добавлений и 13 удалений

Просмотреть файл

@ -27,14 +27,16 @@
//! [`init`]: fn.init.html
//! [`init_with_level`]: fn.init_with_level.html
use chrono;
use log;
use std::fmt;
use std::io;
use std::io::Write;
use std::str;
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
use chrono;
use log;
use mozprofile::preferences::Pref;
static MAX_LOG_LEVEL: AtomicUsize = ATOMIC_USIZE_INIT;
const LOGGED_TARGETS: &'static [&'static str] = &[
"geckodriver",
@ -122,6 +124,21 @@ impl Into<log::Level> for Level {
}
}
impl Into<Pref> for Level {
fn into(self) -> Pref {
use self::Level::*;
Pref::new(match self {
Fatal => "Fatal",
Error => "Error",
Warn => "Warn",
Info => "Info",
Config => "Config",
Debug => "Debug",
Trace => "Trace",
})
}
}
impl From<log::Level> for Level {
fn from(log_level: log::Level) -> Level {
use log::Level::*;
@ -195,11 +212,14 @@ fn format_ts(ts: chrono::DateTime<chrono::Local>) -> String {
#[cfg(test)]
mod tests {
use super::{format_ts, init_with_level, max_level, set_max_level, Level};
use chrono;
use log;
use std::str::FromStr;
use std::sync::Mutex;
use chrono;
use log;
use mozprofile::preferences::{Pref, PrefValue};
lazy_static! {
static ref LEVEL_MUTEX: Mutex<()> = Mutex::new(());
}
@ -246,6 +266,24 @@ mod tests {
assert_eq!(Into::<log::Level>::into(Level::Trace), log::Level::Trace);
}
#[test]
fn test_level_into_pref() {
let tests = [
(Level::Fatal, "Fatal"),
(Level::Error, "Error"),
(Level::Warn, "Warn"),
(Level::Info, "Info"),
(Level::Config, "Config"),
(Level::Debug, "Debug"),
(Level::Trace, "Trace"),
];
for &(lvl, s) in tests.iter() {
let expected = Pref { value: PrefValue::String(s.to_string()), sticky: false };
assert_eq!(Into::<Pref>::into(lvl), expected);
}
}
#[test]
fn test_level_from_str() {
assert_eq!(Level::from_str("fatal"), Ok(Level::Fatal));

Просмотреть файл

@ -486,12 +486,19 @@ impl MarionetteHandler {
Ok(())
}
pub fn set_prefs(&self, port: u16, profile: &mut Profile, custom_profile: bool,
extra_prefs: Vec<(String, Pref)>)
-> WebDriverResult<()> {
let prefs = try!(profile.user_prefs()
.map_err(|_| WebDriverError::new(ErrorStatus::UnknownError,
"Unable to read profile preferences file")));
pub fn set_prefs(
&self,
port: u16,
profile: &mut Profile,
custom_profile: bool,
extra_prefs: Vec<(String, Pref)>,
) -> WebDriverResult<()> {
let prefs = profile.user_prefs().map_err(|_| {
WebDriverError::new(
ErrorStatus::UnknownError,
"Unable to read profile preferences file",
)
})?;
for &(ref name, ref value) in prefs::DEFAULT.iter() {
if !custom_profile || !prefs.contains_key(name) {
@ -509,11 +516,12 @@ impl MarionetteHandler {
prefs.insert("marionette.debugging.clicktostart", Pref::new(true));
}
prefs.insert("marionette.log.level", Pref::new(logging::max_level().to_string()));
prefs.insert("marionette.log.level", logging::max_level().into());
prefs.insert("marionette.port", Pref::new(port));
prefs.write().map_err(|_| WebDriverError::new(ErrorStatus::UnknownError,
"Unable to write Firefox profile"))
prefs.write().map_err(|_| {
WebDriverError::new(ErrorStatus::UnknownError, "Unable to write Firefox profile")
})
}
}