From 6d384f77da6f4e0643ae2400f80837bdc7a3badb Mon Sep 17 00:00:00 2001 From: jake Date: Wed, 18 Dec 2019 19:31:25 -0800 Subject: [PATCH] add proper logging --- Cargo.toml | 2 ++ src/common/client.rs | 18 +++++++++-------- src/common/clientpool.rs | 3 ++- src/common/leveltable.rs | 3 --- src/common/mainloop.rs | 3 ++- src/common/network.rs | 4 ++-- src/main.rs | 42 ++++++++++++++++++++++++++++++++++++---- 7 files changed, 56 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ffab8f9..e22fdf8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,4 +29,6 @@ chrono = "*" serde = "*" serde_json = "*" ron = "*" +log = "*" +fern = { version = "0.5", features = ["colored"] } diff --git a/src/common/client.rs b/src/common/client.rs index 0f28e01..19871b1 100644 --- a/src/common/client.rs +++ b/src/common/client.rs @@ -1,3 +1,5 @@ +use log::{info, trace, warn}; + use libpso::crypto::{PSOCipher, NullCipher}; use crate::common::serverstate::{SendServerPacket, RecvServerPacket, ClientId}; @@ -54,7 +56,7 @@ impl Client where self.send_buffer.drain(..len); }, Err(err) => { - println!("[client] error sending data to {:?}: {:?}", self.socket, err); + warn!("[client] error sending data to {:?}: {:?}", self.socket, err); } } } @@ -95,25 +97,25 @@ impl Client where let pkt_data = self.incoming_data.drain(..pkt_len).collect::>(); - //println!("[recv: buf] {:?}", pkt_data); + trace!("[recv buf from {:?}] {:?}", self.id, pkt_data); let pkt = R::from_bytes(&pkt_data[..pkt_size]) .map_err(|err| -> PacketNetworkError { err.into() })?; - println!("[recv] {:?}", pkt); + trace!("[recv from {:?}] {:?}", self.id, pkt); result.push(pkt); } Ok(result) } pub fn send_pkt(&mut self, pkt: S) { - println!("[send] {:?}", pkt); + trace!("[send to {:?}] {:?}", self.id, pkt); let buf = pkt.as_bytes(); - /*if buf.len() < 1024*2 { - println!("[send: buf] {:?}", buf); + if buf.len() < 1024*2 { + trace!("[send: buf] {:?}", buf); } else { - println!("[send: buf] [...large buffer...]"); - }*/ + trace!("[send: buf] [...large buffer...]"); + } let mut cbuf = self.cipher_out.encrypt(&buf).unwrap(); self.send_buffer.append(&mut cbuf); self.send_data(); diff --git a/src/common/clientpool.rs b/src/common/clientpool.rs index 0ef60ca..0d5dda2 100644 --- a/src/common/clientpool.rs +++ b/src/common/clientpool.rs @@ -6,6 +6,7 @@ use std::net::{SocketAddr, Ipv4Addr}; use std::sync::mpsc::TryRecvError; use mio::tcp::TcpListener; use mio::{Events, Poll, Token, Ready, PollOpt}; +use log::warn; use crate::common::client::Client; use crate::common::serverstate::{SendServerPacket, RecvServerPacket, ClientId}; @@ -163,7 +164,7 @@ impl ClientPool where match client_read(&self.sender, client) { Ok(()) =>{}, Err(err) => { - println!("pkt err: {:?}", err); + warn!("pkt err: {:?}", err); match err { PacketNetworkError::ClientDisconnected => { self.poll.deregister(&client.socket).unwrap(); diff --git a/src/common/leveltable.rs b/src/common/leveltable.rs index 665910f..325d6d0 100644 --- a/src/common/leveltable.rs +++ b/src/common/leveltable.rs @@ -83,9 +83,6 @@ impl CharacterLevelTable { .filter(|stat| { stat.exp <= exp }) - .inspect(|k| { - println!("{:?}", k) - }) .fold((0, CharacterStats::default()), |acc, &k| { (acc.0 + 1, CharacterStats { hp: acc.1.hp + k.hp as u16, diff --git a/src/common/mainloop.rs b/src/common/mainloop.rs index a2a0991..1e7b0f1 100644 --- a/src/common/mainloop.rs +++ b/src/common/mainloop.rs @@ -1,4 +1,5 @@ use std::thread; +use log::warn; use mio::{Events, Poll, Token, Ready, PollOpt}; use mio_extras::channel::{channel, Sender, Receiver}; @@ -39,7 +40,7 @@ fn recv_from_clientpool(state: &mut STATE, }, Err(err) => { // TODO: break? - println!("[handler error]: {:?} {:?}", client_id, err); + warn!("[handler error]: {:?} {:?}", client_id, err); } } } diff --git a/src/common/network.rs b/src/common/network.rs index e73a111..bd91b1d 100644 --- a/src/common/network.rs +++ b/src/common/network.rs @@ -1,4 +1,5 @@ use std::io::{Read}; +use log::trace; use libpso::crypto::{PSOCipher, CipherError}; use libpso::PacketParseError; @@ -61,7 +62,6 @@ pub fn recv_packet(socket: &mut T, cipher: &mut dyn PSOCipher) -> Resul offset += diff; } - //println!("[encbuf]: {:X?}", data_buf); let mut dec_data_buf = cipher.decrypt(&data_buf.to_vec())?; let mut full_buf = Vec::new(); @@ -69,6 +69,6 @@ pub fn recv_packet(socket: &mut T, cipher: &mut dyn PSOCipher) -> Resul full_buf.append(&mut dec_data_buf); full_buf = full_buf[..pkt_size].to_vec(); - println!("[recv: buf]: {:X?}", full_buf); + trace!("[recv: buf]: {:X?}", full_buf); Ok(full_buf) } diff --git a/src/main.rs b/src/main.rs index a7a6bd4..bae329e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ mod ship; use std::thread; use std::time::SystemTime; +use log::{info}; use patch::patch::{PatchServerState, generate_patch_tree, load_config}; use login::login::LoginServerState; @@ -18,11 +19,44 @@ use login::character::CharacterServerState; use ship::ship::ShipServerState; use entity::account::{UserAccount, UserSettings}; use entity::gateway::{EntityGateway, InMemoryGateway}; +use entity::item::ItemLocation; use libpso::{utf8_to_array, utf8_to_utf16_array}; //use crate::utf8_to_utf16_array; +use libpso::item::*; + +fn setup_logger() { + let colors = fern::colors::ColoredLevelConfig::new() + .error(fern::colors::Color::Red) + .warn(fern::colors::Color::Yellow) + .info(fern::colors::Color::Green) + .debug(fern::colors::Color::White) + .trace(fern::colors::Color::BrightBlack); + let stdio = fern::Dispatch::new() + .level(log::LevelFilter::Debug) + .format(move |out, message, record| { + out.finish(format_args!( + "\x1B[{}m[{}][{}][{}] {}\x1B[0m", + colors.get_color(&record.level()).to_fg_str(), + chrono::Local::now().format("%Y-%m-%d %H:%M:%S"), + record.target(), + record.level(), + message, + )) + }) + .chain(std::io::stdout()); + let fileout = fern::Dispatch::new() + .level(log::LevelFilter::Trace) + .chain(fern::log_file(format!("elseware-{}.log", chrono::Local::now().format("%Y-%m-%d %H:%M:%S"))).unwrap()); + fern::Dispatch::new() + .chain(stdio) + .chain(fileout) + .apply().unwrap(); +} + fn main() { + setup_logger(); let mut entity_gateway = InMemoryGateway::new(); let fake_user = UserAccount { @@ -47,7 +81,7 @@ fn main() { entity_gateway.set_character(&character); let patch_thread = thread::spawn(|| { - println!("[patch] starting server"); + info!("[patch] starting server"); let patch_config = load_config(); let (patch_file_tree, patch_file_lookup) = generate_patch_tree(patch_config.path.as_str()); let patch_state = PatchServerState::new(patch_file_tree, patch_file_lookup); @@ -55,19 +89,19 @@ fn main() { }); let thread_entity_gateway = entity_gateway.clone(); let auth_thread = thread::spawn(|| { - println!("[auth] starting server"); + info!("[auth] starting server"); let auth_state = LoginServerState::new(thread_entity_gateway); common::mainloop::mainloop(auth_state, login::login::LOGIN_PORT); }); let thread_entity_gateway = entity_gateway.clone(); let char_thread = thread::spawn(|| { - println!("[character] starting server"); + info!("[character] starting server"); let char_state = CharacterServerState::new(thread_entity_gateway); common::mainloop::mainloop(char_state, login::character::CHARACTER_PORT); }); let thread_entity_gateway = entity_gateway.clone(); let ship_thread = thread::spawn(|| { - println!("[ship] starting server"); + info!("[ship] starting server"); let ship_state = ShipServerState::new(thread_entity_gateway); common::mainloop::mainloop(ship_state, ship::ship::SHIP_PORT); });