Implement logging

next
Sayan Nandan 4 years ago
parent 432e19dd71
commit 59a2b42615
No known key found for this signature in database
GPG Key ID: C31EFD7DDA12AEE0

86
Cargo.lock generated

@ -35,6 +35,12 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "bincode"
version = "1.3.1"
@ -69,6 +75,17 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "chrono"
version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b"
dependencies = [
"num-integer",
"num-traits",
"time",
]
[[package]]
name = "clap"
version = "2.33.3"
@ -100,6 +117,19 @@ version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6035b7b9244bf9637cd7ef80b5e1c54404bef92cccd34738c85c45f04ae8b244"
[[package]]
name = "env_logger"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -136,7 +166,7 @@ checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
dependencies = [
"cfg-if",
"libc",
"wasi",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
@ -148,6 +178,15 @@ dependencies = [
"libc",
]
[[package]]
name = "humantime"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
dependencies = [
"quick-error",
]
[[package]]
name = "instant"
version = "0.1.6"
@ -293,6 +332,25 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "num-integer"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.13.0"
@ -350,6 +408,12 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "quick-error"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quote"
version = "1.0.7"
@ -507,9 +571,12 @@ version = "0.4.1"
dependencies = [
"bincode",
"bytes",
"chrono",
"clap",
"env_logger",
"lazy_static",
"libtdb",
"log",
"parking_lot",
"serde",
"serde_derive",
@ -544,6 +611,17 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "time"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
dependencies = [
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"winapi 0.3.9",
]
[[package]]
name = "tokio"
version = "0.2.22"
@ -625,6 +703,12 @@ version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]]
name = "winapi"
version = "0.2.8"

@ -24,7 +24,7 @@ pub mod terminal {
use std::fmt;
use std::io::Write;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
/// Write to stdout with
/// Write to stdout with
pub fn write_with_col<T: fmt::Display>(item: T, color: Option<Color>) -> fmt::Result {
let mut stdout = StandardStream::stdout(ColorChoice::Always);
if let Err(_) = stdout.set_color(ColorSpec::new().set_fg(color)) {

@ -14,8 +14,11 @@ bincode = "1.3.1"
parking_lot = "0.11.0"
lazy_static = "1.4.0"
serde_derive = "1.0.115"
chrono = "0.4.15"
serde = {version = "1.0.115", features= ["derive"]}
toml = "0.5.6"
clap = {version = "2.33.3", features=["yaml"]}
env_logger = "0.7.1"
log = "0.4.11"
[dev-dependencies]
tokio = { version = "0.2", features = ["test-util"] }

@ -69,9 +69,8 @@ impl Shared {
}
// Kick in BGSAVE
match diskstore::flush_data(&self.table.read().get_ref()) {
Ok(_) => terminal::write_info("info: BGSAVE completed successfully\n").unwrap(),
Err(e) => terminal::write_error(format!("error: BGSAVE failed with error: '{}'\n", e))
.unwrap(),
Ok(_) => log::info!("BGSAVE completed successfully"),
Err(e) => log::error!("BGSAVE failed with error: '{}'", e),
}
Some(Instant::now() + Duration::from_secs(120))
}

@ -188,7 +188,7 @@ pub async fn run(listener: TcpListener, sig: impl Future) {
tokio::select! {
_ = server.run() => {}
_ = sig => {
println!("Shutting down...")
log::info!("Signalling all workers to shut down");
}
}
let Listener {
@ -199,17 +199,17 @@ pub async fn run(listener: TcpListener, sig: impl Future) {
..
} = server;
if let Ok(_) = db.flush_db() {
terminal::write_success("Successfully saved data to disk\n").unwrap();
log::info!("Successfully saved data to disk");
()
} else {
terminal::write_error("Failed to flush data to disk\n").unwrap();
log::error!("Failed to flush data to disk");
loop {
// Keep looping until we successfully write the in-memory table to disk
terminal::write_warning("Press enter to try again...").unwrap();
log::warn!("Press enter to try again...");
io::stdout().flush().unwrap();
io::stdin().read(&mut [0]).unwrap();
if let Ok(_) = db.flush_db() {
terminal::write_success("Successfully saved data to disk\n").unwrap();
log::info!("Successfully saved data to disk");
break;
} else {
continue;

@ -21,15 +21,19 @@
use tokio::net::TcpListener;
mod config;
use chrono::Local;
use std::env;
mod coredb;
mod dbnet;
mod diskstore;
mod kvengine;
use std::io::Write;
mod protocol;
mod queryengine;
mod resp;
use coredb::CoreDB;
use dbnet::run;
use env_logger::*;
use tokio::signal;
#[cfg(test)]
mod tests;
@ -48,36 +52,38 @@ static TEXT: &'static str = "
";
#[tokio::main]
async fn main() {
Builder::new()
.parse_filters(&env::var("TDB_LOG").unwrap_or("info".to_owned()))
.init();
// Start the server which asynchronously waits for a CTRL+C signal
// which will safely shut down the server
run(check_args_or_connect().await, signal::ctrl_c()).await;
}
use libtdb::util::terminal;
/// This function checks the command line arguments and binds to an appropriate
/// port and host, as per the supplied configuration options
async fn check_args_or_connect() -> TcpListener {
use libtdb::util::terminal;
let cfg = config::get_config_file_or_return_cfg();
match cfg {
let binding = match cfg {
Ok(config::ConfigType::Custom(cfg)) => {
if cfg.is_artful() {
println!("{}\n{}", TEXT, MSG);
} else {
terminal::write_info("TerrabaseDB v0.4.1 | Protocol: Terrapipe 1.0").unwrap();
}
terminal::write_info("info: Using settings from config file\n").unwrap();
log::info!("info: Using settings from config file");
TcpListener::bind(cfg.get_host_port_tuple()).await.unwrap()
}
Ok(config::ConfigType::Def(cfg)) => {
println!("{}\n{}", TEXT, MSG);
terminal::write_info("info: No configuration file supplied. Using default settings\n")
.unwrap();
log::info!("info: No configuration file supplied. Using default settings");
TcpListener::bind(cfg.get_host_port_tuple()).await.unwrap()
}
Err(e) => {
terminal::write_error(e).unwrap();
std::process::exit(0x100);
}
}
};
binding
}

Loading…
Cancel
Save