-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from qwerty541/feat_file_logger
feat: file logger
- Loading branch information
Showing
6 changed files
with
123 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use logged_stream::DefaultFilter; | ||
use logged_stream::FileLogger; | ||
use logged_stream::LoggedStream; | ||
use logged_stream::LowercaseHexadecimalFormatter; | ||
use std::env; | ||
use std::fs; | ||
use tokio::io::AsyncReadExt; | ||
use tokio::io::AsyncWriteExt; | ||
use tokio::net; | ||
|
||
async fn handle_connection(mut stream: net::TcpStream) { | ||
loop { | ||
let mut read = [0; 1028]; | ||
match stream.read(&mut read).await { | ||
Ok(n) => { | ||
stream.write_all(&read[0..n]).await.unwrap(); | ||
} | ||
Err(err) => panic!("{err}"), | ||
} | ||
} | ||
} | ||
|
||
#[tokio::main(flavor = "multi_thread", worker_threads = 4)] | ||
async fn main() { | ||
env::set_var("RUST_LOG", "debug"); | ||
env_logger::builder() | ||
.default_format() | ||
.format_timestamp_millis() | ||
.init(); | ||
|
||
let listener = net::TcpListener::bind("127.0.0.1:8080").await.unwrap(); | ||
|
||
tokio::spawn(async move { | ||
loop { | ||
match listener.accept().await { | ||
Ok((stream, _addr)) => { | ||
tokio::spawn(handle_connection(stream)); | ||
} | ||
Err(err) => panic!("{err}"), | ||
} | ||
} | ||
}); | ||
|
||
let mut client = LoggedStream::new( | ||
net::TcpStream::connect("127.0.0.1:8080").await.unwrap(), | ||
LowercaseHexadecimalFormatter::new_default(), | ||
DefaultFilter, | ||
FileLogger::new(fs::File::create("./examples/traffic.log").unwrap()), | ||
); | ||
|
||
let send = [0x01, 0x02, 0x03, 0x04]; | ||
client.write_all(&send).await.unwrap(); | ||
let mut response = [0u8; 4]; | ||
client.read_exact(&mut response).await.unwrap(); | ||
|
||
let send = [0x05, 0x06, 0x07, 0x08]; | ||
client.write_all(&send).await.unwrap(); | ||
let mut response = [0u8; 4]; | ||
client.read_exact(&mut response).await.unwrap(); | ||
|
||
let send = [0x09, 0x0a, 0x0b, 0x0c]; | ||
client.write_all(&send).await.unwrap(); | ||
let mut response = [0u8; 4]; | ||
client.read_exact(&mut response).await.unwrap(); | ||
|
||
let send = [0x01, 0x02, 0x03, 0x04]; | ||
client.write_all(&send).await.unwrap(); | ||
let mut response = [0u8; 4]; | ||
client.read_exact(&mut response).await.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters