chore: Implementing Logging
Using simplelog to output to the terminal and to a log file
This commit is contained in:
parent
3411c0e4a0
commit
3de79b08f1
2 changed files with 53 additions and 9 deletions
|
@ -18,3 +18,5 @@ derive_more = "0.99.17"
|
||||||
id3 = "1.5.1"
|
id3 = "1.5.1"
|
||||||
rodio = "0.16.0"
|
rodio = "0.16.0"
|
||||||
tungstenite = "0.18.0"
|
tungstenite = "0.18.0"
|
||||||
|
log = "0.4.17"
|
||||||
|
simplelog = "0.12.0"
|
||||||
|
|
60
src/main.rs
60
src/main.rs
|
@ -1,3 +1,6 @@
|
||||||
|
use log::{debug, error, info, trace, warn, LevelFilter};
|
||||||
|
use simplelog::*;
|
||||||
|
use std::fs::File;
|
||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
@ -29,27 +32,33 @@ struct Cli {
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
configuration_file: Option<String>,
|
configuration_file: Option<String>,
|
||||||
|
|
||||||
|
#[arg(long, default_value = "SousaLog.txt")]
|
||||||
|
log_file: String,
|
||||||
|
|
||||||
/// Specify a specific database file
|
/// Specify a specific database file
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
database_file: Option<String>,
|
database_file: Option<String>,
|
||||||
|
|
||||||
/// Start the server without a front end
|
|
||||||
#[arg(long)]
|
|
||||||
no_webserver: bool,
|
|
||||||
|
|
||||||
/// Run the database in memory alone
|
/// Run the database in memory alone
|
||||||
#[arg(long)]
|
#[arg(long, default_value = "false")]
|
||||||
no_save: bool,
|
no_save: bool,
|
||||||
|
|
||||||
/// Delete an existing database file (wherever it looks on startup)
|
/// Delete an existing database file (wherever it looks on startup)
|
||||||
|
/// TODO: actually make this a thing
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
reset_database: bool,
|
reset_database: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
|
|
||||||
// settings = confy settings
|
// settings = confy settings
|
||||||
|
|
||||||
|
let log_file: String;
|
||||||
|
|
||||||
|
log_file = "testing_log.txt".to_string();
|
||||||
|
|
||||||
|
init_logger(log_file);
|
||||||
|
|
||||||
let music_dir: String;
|
let music_dir: String;
|
||||||
if cli.root_directory.is_some() {
|
if cli.root_directory.is_some() {
|
||||||
music_dir = cli.root_directory.clone().unwrap();
|
music_dir = cli.root_directory.clone().unwrap();
|
||||||
|
@ -57,14 +66,18 @@ fn main() {
|
||||||
music_dir = String::from(dirs_next::audio_dir().unwrap().to_str().unwrap());
|
music_dir = String::from(dirs_next::audio_dir().unwrap().to_str().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
let music_scanner = file_operations::MusicScanner::new(music_dir);
|
let music_scanner = file_operations::MusicScanner::new(music_dir.clone());
|
||||||
|
|
||||||
let db_path: PathBuf = ["/", "home", "nixolas", "RustedBeats.db"].iter().collect();
|
let db_path: PathBuf = ["/", "home", "nixolas", "RustedBeats.db"].iter().collect();
|
||||||
|
|
||||||
let dbo = db_operations::DBObject::new(&db_path, false).unwrap();
|
info!("Opening database in memory mode: {}", cli.no_save);
|
||||||
|
info!("Database file path is: {}", &db_path.to_string_lossy());
|
||||||
|
let dbo = db_operations::DBObject::new(&db_path, cli.no_save).unwrap();
|
||||||
|
|
||||||
|
info!("Starting file scan with root set to: {}", music_dir);
|
||||||
for file_batch in music_scanner {
|
for file_batch in music_scanner {
|
||||||
for filepath in file_batch {
|
for filepath in file_batch {
|
||||||
|
debug!("checking file: {}", filepath.to_string_lossy());
|
||||||
if filepath.to_string_lossy().ends_with(".wav") {
|
if filepath.to_string_lossy().ends_with(".wav") {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
|
@ -87,18 +100,26 @@ fn main() {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
info!("Creating music player");
|
||||||
let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap();
|
let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap();
|
||||||
let mut music_player = MusicPlayer::new(test_file[0].clone(), &stream_handle);
|
let mut music_player = MusicPlayer::new(test_file[0].clone(), &stream_handle);
|
||||||
|
|
||||||
|
info!("Opening Tcp Listener");
|
||||||
let tcp_listener = TcpListener::bind("127.0.0.1:9001").unwrap();
|
let tcp_listener = TcpListener::bind("127.0.0.1:9001").unwrap();
|
||||||
tcp_listener.set_nonblocking(true).unwrap();
|
tcp_listener.set_nonblocking(true).unwrap();
|
||||||
|
|
||||||
let mut sockets = Vec::<WebSocket<TcpStream>>::new();
|
let mut sockets = Vec::<WebSocket<TcpStream>>::new();
|
||||||
|
info!(
|
||||||
|
"Socket listening on: {}",
|
||||||
|
tcp_listener.local_addr().unwrap()
|
||||||
|
);
|
||||||
println!("Listening on {}", tcp_listener.local_addr().unwrap());
|
println!("Listening on {}", tcp_listener.local_addr().unwrap());
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Ok((stream, addr)) = tcp_listener.accept() {
|
if let Ok((stream, addr)) = tcp_listener.accept() {
|
||||||
stream.set_nonblocking(true).unwrap();
|
stream.set_nonblocking(true).unwrap();
|
||||||
|
|
||||||
|
info!("New socket connected from: {}", addr);
|
||||||
println!("New socket connected from: {}", addr);
|
println!("New socket connected from: {}", addr);
|
||||||
|
|
||||||
match accept(stream) {
|
match accept(stream) {
|
||||||
|
@ -142,7 +163,10 @@ fn main() {
|
||||||
sockets.remove(i);
|
sockets.remove(i);
|
||||||
} else if error.to_string().ends_with("(os error 11)") {
|
} else if error.to_string().ends_with("(os error 11)") {
|
||||||
continue;
|
continue;
|
||||||
} else if error.to_string().ends_with("Trying to work with closed connection") {
|
} else if error
|
||||||
|
.to_string()
|
||||||
|
.ends_with("Trying to work with closed connection")
|
||||||
|
{
|
||||||
sockets.remove(i);
|
sockets.remove(i);
|
||||||
} else {
|
} else {
|
||||||
println!("There was an IO error: {}", error.to_string());
|
println!("There was an IO error: {}", error.to_string());
|
||||||
|
@ -256,3 +280,21 @@ fn write_to_socket(
|
||||||
.into(),
|
.into(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn init_logger(output_file: String) {
|
||||||
|
// TODO: configure the log levels to something appropriate
|
||||||
|
CombinedLogger::init(vec![
|
||||||
|
TermLogger::new(
|
||||||
|
LevelFilter::Info,
|
||||||
|
Config::default(),
|
||||||
|
TerminalMode::Mixed,
|
||||||
|
ColorChoice::Auto,
|
||||||
|
),
|
||||||
|
WriteLogger::new(
|
||||||
|
LevelFilter::Info,
|
||||||
|
Config::default(),
|
||||||
|
File::create("my_rust_binary.log").unwrap(),
|
||||||
|
),
|
||||||
|
])
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue