2016-04-30 17:41:24 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//! Traces config.
|
2016-07-25 16:09:47 +02:00
|
|
|
use std::str::FromStr;
|
2016-04-30 17:41:24 +02:00
|
|
|
use bloomchain::Config as BloomConfig;
|
2016-05-18 11:34:15 +02:00
|
|
|
use trace::Error;
|
2016-04-30 17:41:24 +02:00
|
|
|
|
|
|
|
/// 3-value enum.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum Switch {
|
|
|
|
/// True.
|
|
|
|
On,
|
|
|
|
/// False.
|
|
|
|
Off,
|
|
|
|
/// Auto.
|
|
|
|
Auto,
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
impl Default for Switch {
|
|
|
|
fn default() -> Self {
|
|
|
|
Switch::Auto
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Switch {
|
|
|
|
type Err = String;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
match s {
|
|
|
|
"on" => Ok(Switch::On),
|
|
|
|
"off" => Ok(Switch::Off),
|
|
|
|
"auto" => Ok(Switch::Auto),
|
|
|
|
other => Err(format!("Invalid switch value: {}", other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-30 17:41:24 +02:00
|
|
|
impl Switch {
|
|
|
|
/// Tries to turn old switch to new value.
|
2016-05-18 11:34:15 +02:00
|
|
|
pub fn turn_to(&self, to: Switch) -> Result<bool, Error> {
|
2016-04-30 17:41:24 +02:00
|
|
|
match (*self, to) {
|
|
|
|
(Switch::On, Switch::On) | (Switch::On, Switch::Auto) | (Switch::Auto, Switch::On) => Ok(true),
|
2016-05-18 11:34:15 +02:00
|
|
|
(Switch::Off, Switch::On) => Err(Error::ResyncRequired),
|
2016-04-30 17:41:24 +02:00
|
|
|
_ => Ok(false),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Traces config.
|
2016-07-25 16:09:47 +02:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2016-04-30 17:41:24 +02:00
|
|
|
pub struct Config {
|
|
|
|
/// Indicates if tracing should be enabled or not.
|
|
|
|
/// If it's None, it will be automatically configured.
|
|
|
|
pub enabled: Switch,
|
|
|
|
/// Traces blooms configuration.
|
|
|
|
pub blooms: BloomConfig,
|
2016-07-31 00:19:27 +02:00
|
|
|
/// Preferef cache-size.
|
|
|
|
pub pref_cache_size: usize,
|
|
|
|
/// Max cache-size.
|
|
|
|
pub max_cache_size: usize,
|
2016-04-30 17:41:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Config {
|
2016-07-25 16:09:47 +02:00
|
|
|
enabled: Switch::default(),
|
2016-04-30 17:41:24 +02:00
|
|
|
blooms: BloomConfig {
|
|
|
|
levels: 3,
|
|
|
|
elements_per_index: 16,
|
2016-06-20 12:42:04 +02:00
|
|
|
},
|
2016-07-31 00:19:27 +02:00
|
|
|
pref_cache_size: 15 * 1024 * 1024,
|
|
|
|
max_cache_size: 20 * 1024 * 1024,
|
2016-04-30 17:41:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Switch;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_switch_parsing() {
|
|
|
|
assert_eq!(Switch::On, "on".parse().unwrap());
|
|
|
|
assert_eq!(Switch::Off, "off".parse().unwrap());
|
|
|
|
assert_eq!(Switch::Auto, "auto".parse().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_switch_default() {
|
|
|
|
assert_eq!(Switch::default(), Switch::Auto);
|
|
|
|
}
|
|
|
|
}
|