Added (unimplemented) log level option

monolith
hheik 2024-08-19 15:40:58 +03:00
parent 6f851a0d7b
commit 160c44e6ef
3 changed files with 53 additions and 16 deletions

View File

@ -1,4 +1,4 @@
use std::path::PathBuf; use std::{path::PathBuf, str::FromStr};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -64,6 +64,26 @@ impl Default for PlaylistParams {
} }
} }
#[derive(Clone, Copy, Debug, Default)]
pub enum LogLevel {
Quiet = 0,
Error = 1,
#[default]
All = 2,
}
impl FromStr for LogLevel {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"quiet" => Ok(Self::Quiet),
"error" => Ok(Self::Error),
"all" => Ok(Self::All),
_ => Err(()),
}
}
}
pub mod server { pub mod server {
use std::{fmt::Debug, path::PathBuf}; use std::{fmt::Debug, path::PathBuf};

View File

@ -45,6 +45,10 @@ pub struct CliArgs {
/// whether unicode symbols are used to improve the overall look of the app /// whether unicode symbols are used to improve the overall look of the app
#[argh(option, default = "true")] #[argh(option, default = "true")]
enhanced_graphics: bool, enhanced_graphics: bool,
/// server-side log level (quiet, error, all)
#[argh(option)]
log_level: Option<String>,
} }
fn main() { fn main() {

View File

@ -4,7 +4,7 @@ use rmp::{
os, os,
protocol::{Message, MessageType}, protocol::{Message, MessageType},
server::ServerError, server::ServerError,
PlaylistParams, ServerState, LogLevel, PlaylistParams, ServerState,
}; };
use std::{ use std::{
fs, fs,
@ -19,34 +19,47 @@ use self::playback::{playback_manager, Playback};
pub mod decoder; pub mod decoder;
pub mod playback; pub mod playback;
pub struct Server { #[derive(Debug, Default)]
pub state: ServerState, pub struct ServerConfig {
pub playback: Playback, pub log_level: LogLevel,
} }
impl Server { impl ServerConfig {
pub fn from_state(state: ServerState) -> Self { pub fn from_args(args: CliArgs) -> Self {
Self { Self {
state, log_level: args
playback: Default::default(), .log_level
.and_then(|level| level.parse().ok())
.unwrap_or_default(),
} }
} }
} }
#[derive(Default)]
pub struct Server {
pub state: ServerState,
pub config: ServerConfig,
pub playback: Playback,
}
pub fn run(args: CliArgs) -> Result<(), ServerError> { pub fn run(args: CliArgs) -> Result<(), ServerError> {
if let Err(err) = os::server::reserve_pid() { if let Err(err) = os::server::reserve_pid() {
let exit_code = handle_error(err); let exit_code = handle_error(err);
std::process::exit(exit_code); std::process::exit(exit_code);
} }
let server = Arc::new(Mutex::new(Server::from_state(ServerState { let server = Arc::new(Mutex::new(Server {
playlist_params: PlaylistParams { state: ServerState {
shuffle: args.shuffle, playlist_params: PlaylistParams {
next: args.next, shuffle: args.shuffle,
repeat: args.repeat, next: args.next,
repeat: args.repeat,
},
player: None,
}, },
player: None, config: ServerConfig::from_args(args),
}))); ..Default::default()
}));
let server_clone = server.clone(); let server_clone = server.clone();
std::thread::Builder::new() std::thread::Builder::new()