add: logging system.
This commit is contained in:
parent
70ee19e718
commit
00d7e03e27
|
@ -18,8 +18,12 @@ bench = false
|
|||
|
||||
[features]
|
||||
master_server = []
|
||||
worker_server = []
|
||||
client = []
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4"
|
||||
postgres = "0.17.3"
|
||||
postgres-types = {version = "0.1.1", features = ["derive"]}
|
||||
log = "0.4"
|
||||
simplelog = "0.8.0"
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
#[macro_use] extern crate log;
|
||||
extern crate simplelog;
|
||||
|
||||
use libtrader::initializer::libtrader_init;
|
||||
use libtrader::db::cmd::create_company::create_company;
|
||||
use libtrader::db::cmd::get_company::get_company_from_db;
|
||||
|
@ -7,7 +10,7 @@ use libtrader::ds::server::global_state::GlobalState;
|
|||
|
||||
fn main() {
|
||||
let mut state: GlobalState = match libtrader_init() {
|
||||
Ok(state) => {println!("inited state: {:?}\n", state); state},
|
||||
Ok(state) => {info!("inited state: {:?}\n", state); state},
|
||||
Err(err) => panic!("Failed with error: {}", err),
|
||||
};
|
||||
|
||||
|
@ -18,20 +21,20 @@ fn main() {
|
|||
company.company_name = "TEST".to_string();
|
||||
company.primary_exchange = "TEST".to_string();
|
||||
match create_company(&mut state, company) {
|
||||
Ok(()) => println!("created company"),
|
||||
Err(err) => panic!("Failed to create company with error: {}", err),
|
||||
Ok(()) => info!("created company"),
|
||||
Err(err) => error!("Failed to create company with error: {}", err),
|
||||
}
|
||||
|
||||
match get_company_from_db(&mut state, "TEST".to_string()) {
|
||||
Ok(found_company) => println!("we found it! {:?}", found_company),
|
||||
Err(err) => panic!("we must found the sacred company! err: {}", err),
|
||||
Ok(found_company) => info!("we found it! {:?}", found_company),
|
||||
Err(err) => error!("we must found the sacred company! err: {}", err),
|
||||
}
|
||||
|
||||
match create_stock(&mut state, "test".to_string()) {
|
||||
Ok(()) => println!("created stock table"),
|
||||
Err(err) => panic!("failed to create stock table {}", err),
|
||||
Ok(()) => info!("created stock table"),
|
||||
Err(err) => error!("failed to create stock table {}", err),
|
||||
}
|
||||
|
||||
println!("state: {:?}\n", state);
|
||||
info!("state: {:?}\n", state);
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ pub fn db_gen_connect_str(state: &mut GlobalState, user: &'static str, pass: &'s
|
|||
pub fn db_connect(state: &GlobalState) -> Result<postgres::Client, String> {
|
||||
match postgres::Client::connect(state.db_connect_str.as_str(), postgres::NoTls) {
|
||||
Ok(client) => return Ok(client),
|
||||
Err(error) => return Err(format!("Failed to Init DB with error: {}", error))
|
||||
Err(error) => return Err(format!("DB_FAILED_INIT: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,19 +2,38 @@ use crate::db::config::{*};
|
|||
use crate::db::init::{db_init, db_gen_connect_str};
|
||||
use crate::ds::server::global_state::GlobalState;
|
||||
|
||||
pub fn libtrader_init_log() -> Result<(), String> {
|
||||
use simplelog::*;
|
||||
use std::fs::File;
|
||||
CombinedLogger::init(vec![
|
||||
#[cfg(debug_assertions)]
|
||||
TermLogger::new(LevelFilter::Debug, Config::default(), TerminalMode::Mixed),
|
||||
#[cfg(not(debug_assertions))]
|
||||
TermLogger::new(LevelFilter::Warn, Config::default(), TerminalMode::Mixed),
|
||||
WriteLogger::new(LevelFilter::Info, Config::default(), File::create("log.txt").unwrap())
|
||||
]).unwrap();
|
||||
info!("Started Logger.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn libtrader_init() -> Result<GlobalState, String> {
|
||||
let mut state: GlobalState = GlobalState::default();
|
||||
|
||||
// Initialize log.
|
||||
match libtrader_init_log() {
|
||||
Ok(()) => {},
|
||||
Err(err) => panic!("This should not happen!\n{}", err),
|
||||
};
|
||||
|
||||
// Generate db_connect_str.
|
||||
db_gen_connect_str(&mut state, DB_USER, DB_PASS);
|
||||
|
||||
// Initialize database.
|
||||
match db_init(&mut state) {
|
||||
Ok(()) => {
|
||||
println!("ran db_init");
|
||||
},
|
||||
Ok(()) => info!("Initialized database."),
|
||||
Err(err) => return Err(format!("INIT_DB_FAILED: {}", err))
|
||||
}
|
||||
|
||||
Ok(state)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
#[macro_use] extern crate log;
|
||||
extern crate simplelog;
|
||||
|
||||
pub mod ds;
|
||||
pub mod db;
|
||||
pub mod initializer;
|
||||
|
|
Loading…
Reference in New Issue