Skip to content

Commit

Permalink
Merge b159633 into 2332a20
Browse files Browse the repository at this point in the history
  • Loading branch information
ssyuan authored Jan 6, 2021
2 parents 2332a20 + b159633 commit 4262209
Show file tree
Hide file tree
Showing 22 changed files with 279 additions and 103 deletions.
98 changes: 95 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions commons/system/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "starcoin-system"
version = "0.9.4"
authors = ["Starcoin Core Dev <dev@starcoin.org>"]
license = "Apache-2.0"
publish = false
edition = "2018"

[dependencies]
systemstat ="0.1.6"
anyhow = "1.0.37"
14 changes: 14 additions & 0 deletions commons/system/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use systemstat::{Platform, System};

pub fn get_free_mem_size() -> Result<u64> {
let sys = System::new();
let free = match sys.memory() {
Ok(mem) => mem.free.as_u64(),
Err(_x) => 0u64,
};
Ok(free)
}
2 changes: 2 additions & 0 deletions config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ publish = false
edition = "2018"

[dependencies]
clap = "2.33.3"
dirs = "3"
anyhow = "1.0.37"
thiserror = "1.0"
Expand All @@ -27,3 +28,4 @@ starcoin-vm-types = { path = "../vm/types" }
network-p2p-types = { path = "../network-p2p/types"}
starcoin-logger = {path = "../commons/logger", package="starcoin-logger"}
diem-temppath = { git = "https://github.com/starcoinorg/diem", rev="a69729b2d54af44d2f779bcf167e3f6d681a9821" }
starcoin-system = {path = "../commons/system", package="starcoin-system"}
5 changes: 4 additions & 1 deletion config/src/account_vault_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ use crate::{BaseConfig, ConfigModule, StarcoinOpt};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use structopt::StructOpt;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, StructOpt)]
#[serde(deny_unknown_fields)]
pub struct AccountVaultConfig {
#[structopt(long = "vault-dir", parse(from_os_str), conflicts_with("vault-dir"))]
/// Account vault dir config.
dir: PathBuf,
#[serde(skip)]
absolute_dir: Option<PathBuf>,
Expand Down
4 changes: 1 addition & 3 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub struct StarcoinOpt {
/// Node network private key file, only work for first init.
pub node_key_file: Option<PathBuf>,

#[structopt(long = "sync-mode", short = "s")]
#[structopt(long = "sync-mode", short = "s", possible_values = &SyncMode::variants(), case_insensitive = false)]
/// Sync mode. Included value(full, fast, light).
pub sync_mode: Option<SyncMode>,

Expand Down Expand Up @@ -444,9 +444,7 @@ impl NodeConfig {
}
}
};

config.after_load(opt, &base)?;
save_config(&config, &config_file_path)?;
Ok(config)
}

Expand Down
33 changes: 19 additions & 14 deletions config/src/logger_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ use crate::{BaseConfig, ConfigModule, StarcoinOpt};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use structopt::StructOpt;

static LOGGER_FILE_NAME: &str = "starcoin.log";

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, StructOpt)]
#[serde(deny_unknown_fields)]
pub struct LoggerConfig {
pub enable_stderr: bool,
pub enable_file: bool,
#[structopt(name = "disable-stderr", long, help = "disable stderr logger")]
pub disable_stderr: bool,
#[structopt(name = "disable-file", long, help = "disable file logger")]
pub disable_file: bool,
#[structopt(name = "max-file-size", long, default_value = "1073741824")]
pub max_file_size: u64,
#[structopt(name = "max-backup", long, default_value = "7")]
pub max_backup: u32,
#[serde(skip)]
log_path: Option<PathBuf>,
Expand All @@ -28,35 +33,35 @@ impl LoggerConfig {
}

pub fn enable_file(&self) -> bool {
self.enable_file && self.log_path.is_some()
(!self.disable_file) && self.log_path.is_some()
}
}

impl ConfigModule for LoggerConfig {
fn default_with_opt(opt: &StarcoinOpt, base: &BaseConfig) -> Result<Self> {
let enable_stderr = !opt.disable_std_log;
let enable_file = !opt.disable_file_log;
let disable_stderr = opt.disable_std_log;
let disable_file = opt.disable_file_log;

Ok(if base.net.is_test() {
Self {
enable_stderr,
enable_file,
disable_stderr,
disable_file,
max_file_size: 10 * 1024 * 1024,
max_backup: 1,
log_path: None,
}
} else if base.net.is_dev() {
Self {
enable_stderr,
enable_file,
disable_stderr,
disable_file,
max_file_size: 10 * 1024 * 1024,
max_backup: 2,
log_path: None,
}
} else {
Self {
enable_stderr,
enable_file,
disable_stderr,
disable_file,
max_file_size: 1024 * 1024 * 1024,
max_backup: 7,
log_path: None,
Expand All @@ -66,8 +71,8 @@ impl ConfigModule for LoggerConfig {

fn after_load(&mut self, opt: &StarcoinOpt, base: &BaseConfig) -> Result<()> {
self.log_path = Some(base.data_dir.join(LOGGER_FILE_NAME));
self.enable_stderr = !opt.disable_std_log;
self.enable_file = !opt.disable_file_log;
self.disable_stderr = opt.disable_std_log;
self.disable_file = opt.disable_file_log;
Ok(())
}
}
14 changes: 8 additions & 6 deletions config/src/metrics_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ use crate::{
};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use structopt::StructOpt;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, StructOpt)]
#[serde(deny_unknown_fields)]
pub struct MetricsConfig {
pub enable_metrics: bool,
#[structopt(name = "disable-metrics", long, help = "disable metrics")]
pub disable_metrics: bool,
#[structopt(name = "address", long, help = "address", default_value = "0.0.0.0")]
pub address: String,
#[structopt(name = "metrics-port", long, default_value = "9101")]
pub port: u16,
}

Expand All @@ -27,16 +31,14 @@ impl ConfigModule for MetricsConfig {
DEFAULT_METRIC_SERVER_PORT
};
Ok(Self {
enable_metrics: !opt.disable_metrics,
disable_metrics: opt.disable_metrics,
address: "0.0.0.0".to_string(),
port,
})
}

fn after_load(&mut self, opt: &StarcoinOpt, _base: &BaseConfig) -> Result<()> {
if opt.disable_metrics {
self.enable_metrics = false;
}
self.disable_metrics = opt.disable_metrics;
Ok(())
}
}
Loading

0 comments on commit 4262209

Please sign in to comment.