improve logging substantially
- remove simple_logger - add fern logging - remove no longer applicable gen_log.rs - add address based logging - every spawned task that handles a client logs their address - main task/runtime has address 0.0.0.0:0 - add pretty colored logging - remove /var/log requirement
This commit is contained in:
parent
a965957bcb
commit
f2c4d46ffe
|
@ -37,7 +37,7 @@ bytes = "*"
|
|||
#postgres = { version = "0.4.0" }
|
||||
postgres-types = { version = "0.2.1", features = ["derive"] }
|
||||
log = "0.4"
|
||||
simplelog = "0.8.0"
|
||||
fern = { version = "0.6.0", features = ["colored"] }
|
||||
enum_primitive = "*"
|
||||
os_type="2.2"
|
||||
ring="*"
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
extern crate log;
|
||||
extern crate simplelog;
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
use libtrader::client::initializer::libtrader_init_client;
|
||||
#[cfg(feature = "server")]
|
||||
use libtrader::server::initializer::libtrader_init_server;
|
||||
use libtrader::server::initializer::{libtrader_init_server, IP};
|
||||
|
||||
fn main() {
|
||||
#[cfg(feature = "server")]
|
||||
|
@ -19,9 +18,13 @@ fn main() {
|
|||
|
||||
// Spawn server
|
||||
rt.block_on(async move {
|
||||
libtrader_init_server()
|
||||
.await
|
||||
.expect("failed running server");
|
||||
IP.scope("0.0.0.0:0000".parse().unwrap(), async move {
|
||||
// for main task logging
|
||||
libtrader_init_server()
|
||||
.await
|
||||
.expect("failed running server");
|
||||
})
|
||||
.await;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@ use tokio_rustls::webpki::DNSNameRef;
|
|||
use tokio_rustls::TlsConnector;
|
||||
|
||||
use crate::common::misc::gen_tls_client_config::gen_tls_client_config;
|
||||
use crate::common::misc::path_exists::path_exists;
|
||||
|
||||
use rand::distributions::Alphanumeric;
|
||||
use rand::{thread_rng, Rng};
|
||||
|
@ -29,33 +28,58 @@ use rand::{thread_rng, Rng};
|
|||
/// ```
|
||||
///
|
||||
fn libtrader_init_log() -> io::Result<()> {
|
||||
info!("Started Logger.");
|
||||
#[cfg(not(debug_assertions))]
|
||||
gen_log();
|
||||
use fern::colors::{Color, ColoredLevelConfig};
|
||||
|
||||
let mut dispatch = fern::Dispatch::new().format(|out, message, record| {
|
||||
// configure colors for the whole line
|
||||
let colors_line = ColoredLevelConfig::new()
|
||||
.error(Color::Red)
|
||||
.warn(Color::White)
|
||||
// we actually don't need to specify the color for debug and info, they are white by default
|
||||
.info(Color::Green)
|
||||
.debug(Color::Yellow)
|
||||
// depending on the terminals color scheme, this is the same as the background color
|
||||
.trace(Color::BrightBlack);
|
||||
|
||||
// configure colors for the name of the level.
|
||||
// since almost all of them are the same as the color for the whole line, we
|
||||
// just clone `colors_line` and overwrite our changes
|
||||
let colors_level = colors_line.clone().info(Color::Green);
|
||||
|
||||
out.finish(format_args!(
|
||||
"{color_line}{date}[{target}][{level}{color_line}] {message}\x1B[0m",
|
||||
color_line = format_args!(
|
||||
"\x1B[{}m",
|
||||
colors_level.get_color(&record.level()).to_fg_str()
|
||||
),
|
||||
date = chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
|
||||
target = record.target(),
|
||||
level = record.level(),
|
||||
message = message
|
||||
))
|
||||
});
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
use simplelog::*;
|
||||
use std::fs::File;
|
||||
|
||||
if !path_exists("log") {
|
||||
std::fs::create_dir("log")?;
|
||||
}
|
||||
CombinedLogger::init(vec![
|
||||
#[cfg(debug_assertions)]
|
||||
TermLogger::new(LevelFilter::Warn, Config::default(), TerminalMode::Mixed),
|
||||
#[cfg(not(debug_assertions))]
|
||||
TermLogger::new(LevelFilter::Warn, Config::default(), TerminalMode::Mixed),
|
||||
WriteLogger::new(
|
||||
LevelFilter::Info,
|
||||
Config::default(),
|
||||
File::create(format!("log/log-{}.txt", chrono::Utc::now().to_rfc2822())).unwrap(),
|
||||
),
|
||||
])
|
||||
.unwrap();
|
||||
};
|
||||
|
||||
Ok(())
|
||||
dispatch = dispatch
|
||||
.level(log::LevelFilter::Debug)
|
||||
.chain(std::io::stdout());
|
||||
}
|
||||
#[cfg(not(debug_assertions))]
|
||||
{
|
||||
dispatch = dispatch
|
||||
.level(log::LevelFilter::Warn)
|
||||
.chain(std::io::stdout())
|
||||
.chain(fern::log_file(format!(
|
||||
"log/log-{}.log",
|
||||
chrono::Utc::now().to_rfc2822()
|
||||
))?);
|
||||
}
|
||||
dispatch.apply().map_err(|err| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("LIBTRADER_INIT_SERVER_LOG_FAILED: {}", err),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Client Initialization of the library.
|
||||
|
@ -69,10 +93,8 @@ fn libtrader_init_log() -> io::Result<()> {
|
|||
/// ```
|
||||
#[tokio::main]
|
||||
pub async fn libtrader_init_client() -> std::io::Result<()> {
|
||||
match libtrader_init_log() {
|
||||
Ok(()) => {}
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
// Initialize log.
|
||||
libtrader_init_log()?;
|
||||
|
||||
let addr = ("0.0.0.0", 4000)
|
||||
.to_socket_addrs()?
|
||||
|
|
|
@ -1,65 +0,0 @@
|
|||
use crate::common::misc::path_exists::path_exists;
|
||||
|
||||
/// Generates the CombinedLogger for simplelog.rs
|
||||
///
|
||||
/// Used in libtrader_init_log() Loggers are generated base on platform/configuration.
|
||||
/// Linux will use /var/log/papertrader/.
|
||||
/// macOS will use /var/log/papertrader/.
|
||||
/// Windows & other OSes will output to a file in the current directory.
|
||||
pub fn gen_log() {
|
||||
/*
|
||||
* Linux will use /var/log/papertrader/.
|
||||
* macOS will use /var/log/papertrader/.
|
||||
* Windows & other oses will output to a file in the current directory.
|
||||
* */
|
||||
use simplelog::*;
|
||||
use std::fs::File;
|
||||
|
||||
match os_type::current_platform().os_type {
|
||||
os_type::OSType::Unknown => {
|
||||
if !path_exists("log") {
|
||||
match std::fs::create_dir("log") {
|
||||
Ok(()) => {}
|
||||
Err(err) => panic!("GEN_LOG_FAILED_DIR_CREATION: {}", err),
|
||||
};
|
||||
}
|
||||
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(format!("log/log-{}.txt", chrono::Utc::now().to_rfc2822()))
|
||||
.unwrap(),
|
||||
),
|
||||
])
|
||||
.unwrap();
|
||||
}
|
||||
_ => {
|
||||
if !path_exists("/var/log/papertrader/") {
|
||||
match std::fs::create_dir("/var/log/papertrader/") {
|
||||
Ok(()) => {}
|
||||
Err(err) => panic!("GEN_LOG_FAILED_DIR_CREATION: {}", err),
|
||||
};
|
||||
}
|
||||
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(format!(
|
||||
"/var/log/papertrader/log-{}.txt",
|
||||
chrono::Utc::now().to_rfc2822()
|
||||
))
|
||||
.unwrap(),
|
||||
),
|
||||
])
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
pub mod assert_msg;
|
||||
pub mod gen_log;
|
||||
pub mod gen_tls_client_config;
|
||||
pub mod gen_tls_server_config;
|
||||
pub mod lookup_ipv4;
|
||||
|
|
|
@ -28,7 +28,6 @@ extern crate json;
|
|||
extern crate bincode;
|
||||
extern crate crypto;
|
||||
extern crate os_type;
|
||||
extern crate simplelog;
|
||||
|
||||
pub mod common;
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ use argh::FromArgs;
|
|||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio_rustls::TlsAcceptor;
|
||||
|
@ -9,8 +10,6 @@ use tokio_rustls::TlsAcceptor;
|
|||
use std::net::ToSocketAddrs;
|
||||
|
||||
use crate::common::misc::gen_tls_server_config::gen_tls_server_config;
|
||||
use crate::common::misc::path_exists::path_exists;
|
||||
use crate::common::misc::return_flags::ReturnFlags;
|
||||
|
||||
use crate::server::db::config::{DB_ACC_PASS, DB_ACC_USER};
|
||||
use crate::server::db::initializer::db_connect;
|
||||
|
@ -32,15 +31,16 @@ struct Options {
|
|||
key: PathBuf,
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
use crate::common::misc::gen_log::gen_log;
|
||||
tokio::task_local! {
|
||||
pub static IP: std::net::SocketAddr;
|
||||
}
|
||||
|
||||
/// Initializes global logger.
|
||||
/// Initializes global and local logger.
|
||||
///
|
||||
/// Private function used by libtrader_init() to initialize the logger. Log destinations are
|
||||
/// platfrom dependent.
|
||||
/// On unix systems: /var/log/papertrader/
|
||||
/// On windows/unkown systems: $(pwd)/log/
|
||||
/// On windows/unknown systems: $(pwd)/log/
|
||||
///
|
||||
/// Returns: nothing on success, on error contains the reason of failure.
|
||||
///
|
||||
|
@ -52,37 +52,59 @@ use crate::common::misc::gen_log::gen_log;
|
|||
/// };
|
||||
/// ```
|
||||
///
|
||||
fn libtrader_init_log() -> Result<(), ReturnFlags> {
|
||||
info!("Started Logger.");
|
||||
#[cfg(not(debug_assertions))]
|
||||
gen_log();
|
||||
fn libtrader_init_log() -> std::io::Result<()> {
|
||||
use fern::colors::{Color, ColoredLevelConfig};
|
||||
|
||||
let mut dispatch = fern::Dispatch::new().format(|out, message, record| {
|
||||
// configure colors for the whole line
|
||||
let colors_line = ColoredLevelConfig::new()
|
||||
.error(Color::Red)
|
||||
.warn(Color::White)
|
||||
// we actually don't need to specify the color for debug and info, they are white by default
|
||||
.info(Color::Green)
|
||||
.debug(Color::Yellow)
|
||||
// depending on the terminals color scheme, this is the same as the background color
|
||||
.trace(Color::BrightBlack);
|
||||
|
||||
// configure colors for the name of the level.
|
||||
// since almost all of them are the same as the color for the whole line, we
|
||||
// just clone `colors_line` and overwrite our changes
|
||||
let colors_level = colors_line.clone().info(Color::Green);
|
||||
|
||||
out.finish(format_args!(
|
||||
"{color_line}{date}[{addr}][{level}{color_line}] {message}\x1B[0m",
|
||||
color_line = format_args!(
|
||||
"\x1B[{}m",
|
||||
colors_level.get_color(&record.level()).to_fg_str()
|
||||
),
|
||||
date = chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
|
||||
addr = IP.get(),
|
||||
level = record.level(),
|
||||
message = message
|
||||
))
|
||||
});
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
use simplelog::*;
|
||||
use std::fs::File;
|
||||
|
||||
if !path_exists("log") {
|
||||
match std::fs::create_dir("log") {
|
||||
Ok(()) => {}
|
||||
Err(_err) => return Err(ReturnFlags::CommonGenLogDirCreationFailed),
|
||||
};
|
||||
}
|
||||
CombinedLogger::init(vec![
|
||||
#[cfg(debug_assertions)]
|
||||
TermLogger::new(LevelFilter::Debug, Config::default(), TerminalMode::Mixed),
|
||||
#[cfg(not(debug_assertions))]
|
||||
TermLogger::new(LevelFilter::Debug, Config::default(), TerminalMode::Mixed),
|
||||
WriteLogger::new(
|
||||
LevelFilter::Info,
|
||||
Config::default(),
|
||||
File::create(format!("log/log-{}.txt", chrono::Utc::now().to_rfc2822())).unwrap(),
|
||||
),
|
||||
])
|
||||
.unwrap();
|
||||
};
|
||||
|
||||
Ok(())
|
||||
dispatch = dispatch
|
||||
.level(log::LevelFilter::Debug)
|
||||
.chain(std::io::stdout());
|
||||
}
|
||||
#[cfg(not(debug_assertions))]
|
||||
{
|
||||
dispatch = dispatch
|
||||
.level(log::LevelFilter::Warn)
|
||||
.chain(std::io::stdout())
|
||||
.chain(fern::log_file(format!(
|
||||
"log/log-{}.log",
|
||||
chrono::Utc::now().to_rfc2822()
|
||||
))?);
|
||||
}
|
||||
dispatch.apply().map_err(|err| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("LIBTRADER_INIT_SERVER_LOG_FAILED: {}", err),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Server Initialization of the library.
|
||||
|
@ -96,11 +118,7 @@ fn libtrader_init_log() -> Result<(), ReturnFlags> {
|
|||
/// ```
|
||||
pub async fn libtrader_init_server() -> std::io::Result<()> {
|
||||
// Initialize log.
|
||||
//#[cfg(not(test))] // wot dis
|
||||
match libtrader_init_log() {
|
||||
Ok(_) => {}
|
||||
Err(_) => {} // TODO: handle this case
|
||||
};
|
||||
libtrader_init_log()?;
|
||||
|
||||
// Initialize SQL connection
|
||||
let sql_shared_conn = Arc::new(db_connect(DB_ACC_USER, DB_ACC_PASS).await.map_err(|err| {
|
||||
|
@ -125,7 +143,7 @@ pub async fn libtrader_init_server() -> std::io::Result<()> {
|
|||
let listener = TcpListener::bind(&addr).await?;
|
||||
|
||||
loop {
|
||||
let (socket, _) = listener.accept().await?; // socket, peer_addr
|
||||
let (socket, peer_addr) = listener.accept().await?; // socket, peer_addr
|
||||
let acceptor = acceptor.clone();
|
||||
let sql_conn = sql_shared_conn.clone();
|
||||
|
||||
|
@ -148,9 +166,12 @@ pub async fn libtrader_init_server() -> std::io::Result<()> {
|
|||
};
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Err(err) = fut.await {
|
||||
eprintln!("{:?}", err);
|
||||
}
|
||||
IP.scope(peer_addr, async move {
|
||||
if let Err(err) = fut.await {
|
||||
eprintln!("{:?}", err);
|
||||
}
|
||||
})
|
||||
.await;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,6 @@ pub async fn register(
|
|||
tls_connection: &mut TlsStream<TcpStream>,
|
||||
message: &Message,
|
||||
) -> std::io::Result<()> {
|
||||
println!("hello");
|
||||
/* assert recieved message */
|
||||
if !assert_msg(
|
||||
message,
|
||||
|
|
Loading…
Reference in New Issue