Browse Source

refactor log system

pull/24/head
Sun 3 years ago
parent
commit
6a0c74181d
  1. 2
      .cargo/config.toml
  2. 10
      Cargo.toml
  3. 23
      src/daemon.rs
  4. 21
      src/lib.rs
  5. 3
      src/rpc.rs
  6. 32
      src/server.rs

2
.cargo/config.toml

@ -1,2 +1,2 @@ @@ -1,2 +1,2 @@
[build]
rustflags = ["--cfg", "tokio_unstable"]
rustflags = ["--cfg", "tokio_unstable", "--cfg", "tracing_unstable"]

10
Cargo.toml

@ -43,18 +43,20 @@ bincode = "1.3" @@ -43,18 +43,20 @@ bincode = "1.3"
blake3 = "1.3"
hex = "0.4"
image = "0.24"
log = "0.4"
once_cell = "1.9"
rand = "0.8"
sha2 = "0.10"
simplelog = "0.11"
sysinfo = "0.23"
serde = { version = "1", features = ["derive"] }
tokio = { version = "1", features = ["full"] }
web3 = { version = "0.18", default-features = false, features = ["http-tls", "signing"] }
tdn = { version = "0.8", default-features = false, features = ["std"] }
tdn_did = { version = "0.8" }
tdn_storage = { version = "0.8" }
tokio = { version = "1", features = ["full"] }
tracing = "0.1"
tracing-appender = "0.2"
tracing-subscriber = "0.3"
web3 = { version = "0.18", default-features = false, features = ["http-tls", "signing"] }
esse_primitives = { version = "0.1", path = "./types/primitives" }
group_types = { version = "0.1", path = "./types/group" }
cloud_types = { version = "0.1", path = "./types/cloud" }

23
src/daemon.rs

@ -1,10 +1,12 @@ @@ -1,10 +1,12 @@
#[macro_use]
extern crate log;
extern crate tracing;
#[macro_use]
extern crate anyhow;
use std::env::args;
use std::path::PathBuf;
use tracing_subscriber::{filter::LevelFilter, prelude::*};
mod account;
mod apps;
@ -24,13 +26,20 @@ mod utils; @@ -24,13 +26,20 @@ mod utils;
#[tokio::main]
async fn main() {
console_subscriber::init();
let console_layer = console_subscriber::spawn();
tracing_subscriber::registry()
.with(console_layer)
.with(
tracing_subscriber::fmt::layer()
.with_level(true)
.with_filter(LevelFilter::DEBUG),
)
.init();
let db_path = args().nth(1).unwrap_or("./.tdn".to_owned());
if std::fs::metadata(&db_path).is_err() {
std::fs::create_dir(&db_path).unwrap();
let db_path = PathBuf::from(&args().nth(1).unwrap_or("./.tdn".to_owned()));
if !db_path.exists() {
tokio::fs::create_dir_all(&db_path).await.unwrap();
}
let _ = server::start(db_path).await;
server::start(db_path).await.unwrap();
}

21
src/lib.rs

@ -1,11 +1,12 @@ @@ -1,11 +1,12 @@
#[macro_use]
extern crate log;
extern crate tracing;
#[macro_use]
extern crate anyhow;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::path::PathBuf;
mod account;
mod apps;
@ -52,5 +53,21 @@ pub extern "C" fn start(db_path: *const c_char) { @@ -52,5 +53,21 @@ pub extern "C" fn start(db_path: *const c_char) {
let s_path = c_str.to_str().unwrap_or("./tdn").to_owned();
let rt = tokio::runtime::Runtime::new().unwrap();
let _ = rt.block_on(server::start(s_path));
let _ = rt.block_on(async {
let db_path = PathBuf::from(&s_path);
if !db_path.exists() {
tokio::fs::create_dir_all(&db_path).await.unwrap();
}
// init log file.
let file_appender = tracing_appender::rolling::daily(&s_path, server::DEFAULT_LOG_FILE);
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
tracing_subscriber::fmt()
.with_writer(non_blocking)
.with_level(true)
.with_max_level(tracing::Level::INFO)
.init();
server::start(db_path).await.unwrap();
});
}

3
src/rpc.rs

@ -379,7 +379,8 @@ fn new_rpc_handler(global: Arc<Global>) -> RpcHandler<Global> { @@ -379,7 +379,8 @@ fn new_rpc_handler(global: Arc<Global>) -> RpcHandler<Global> {
|_params: Vec<RpcParam>, state: Arc<Global>| async move {
let mut results = HandleResult::new();
results.networks.push(NetworkType::NetworkStop);
debug!("Account Offline: {}.", id_to_str(&state.pid().await));
let pid = state.pid().await;
debug!("Account Offline: {}.", id_to_str(&pid));
state.clear().await;
Ok(results)
},

32
src/server.rs

@ -1,5 +1,4 @@ @@ -1,5 +1,4 @@
use once_cell::sync::OnceCell;
use simplelog::{CombinedLogger, Config as LogConfig, LevelFilter};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
@ -27,14 +26,7 @@ pub const DEFAULT_LOG_FILE: &'static str = "esse.log.txt"; @@ -27,14 +26,7 @@ pub const DEFAULT_LOG_FILE: &'static str = "esse.log.txt";
pub static RPC_WS_UID: OnceCell<u64> = OnceCell::new();
pub async fn start(db_path: String) -> Result<()> {
let db_path = PathBuf::from(db_path);
if !db_path.exists() {
tokio::fs::create_dir_all(&db_path).await?;
}
init_log(db_path.clone());
pub async fn start(db_path: PathBuf) -> Result<()> {
let mut config = Config::default();
config.db_path = Some(db_path.clone());
config.p2p_allowlist.append(&mut network_seeds());
@ -267,25 +259,3 @@ async fn handle(handle_result: HandleResult, uid: u64, is_ws: bool, global: &Arc @@ -267,25 +259,3 @@ async fn handle(handle_result: HandleResult, uid: u64, is_ws: bool, global: &Arc
}
}
}
#[inline]
pub fn init_log(mut db_path: PathBuf) {
db_path.push(DEFAULT_LOG_FILE);
#[cfg(debug_assertions)]
CombinedLogger::init(vec![simplelog::TermLogger::new(
LevelFilter::Debug,
LogConfig::default(),
simplelog::TerminalMode::Mixed,
simplelog::ColorChoice::Auto,
)])
.unwrap();
#[cfg(not(debug_assertions))]
CombinedLogger::init(vec![simplelog::WriteLogger::new(
LevelFilter::Info,
LogConfig::default(),
std::fs::File::create(db_path).unwrap(),
)])
.unwrap();
}

Loading…
Cancel
Save