Skip to content

Commit

Permalink
migrate from log to tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiyaa committed Sep 2, 2024
1 parent 17c1fe0 commit 3616c84
Show file tree
Hide file tree
Showing 15 changed files with 452 additions and 392 deletions.
666 changes: 392 additions & 274 deletions Cargo.lock

Large diffs are not rendered by default.

28 changes: 6 additions & 22 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ path = "src/bin/client/main.rs"
alphanumeric-sort = "^1"
chrono = "^0"
clap = { version = "^4", features = ["derive"] }
crossbeam = "^0"
dirs-next = "^2"
env_logger = "^0"
globset = "^0"
lazy_static = "^1"
log = "^0"
libc = "^0"
m3u = "^1"
phf = { version = "^0", features = ["macros"], optional = true }
rand = "^0"
ratatui = { version = "^0", default-features = false, features = ["termion"] }
rustyline = "^4"
serde = "^1"
serde_derive = "^1"
Expand All @@ -43,30 +42,16 @@ shellexpand = "^2"
signal-hook = "^0"
skim = "^0"
strfmt = "^0"
symphonia = { version = "^0", features = ["all"] }
termion = "^1"
toml = "^0"
tracing = "^0"
tracing-subscriber = { version = "^0", features = [ "std", "env-filter" ] }
uuid = { version = "^0", features = ["v4"] }
unicode-width = "^0"
unicode-segmentation = "^1"
xdg = "^2"

[dependencies.phf]
version = "^0"
features = ["macros"]
optional = true

[dependencies.symphonia]
version = "^0"
features = ["all"]

[dependencies.ratatui]
version = "^0"
default-features = false
features = ["termion"]

[dependencies.uuid]
features = [ "v4" ]
version = "^0"

[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"))'.dependencies]
cpal = { features = [ "jack" ], version = "^0" }
# pipewire = { optional = true, version = "^0" }
Expand All @@ -77,7 +62,6 @@ cpal = "^0"
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
cpal = "^0"


[features]
default = [ "devicons" ]
devicons = [ "phf" ]
8 changes: 7 additions & 1 deletion src/bin/client/run/run_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ pub fn run_ui(

while context.quit == QuitType::DoNot {
// do the ui
if let Ok(area) = backend.terminal_ref().size() {
if let Ok(size) = backend.terminal_ref().size() {
let area = Rect {
x: 0,
y: 0,
width: size.width,
height: size.height,
};
// pre-calculate some ui attributes
calculate_ui_context(context, area);

Expand Down
10 changes: 3 additions & 7 deletions src/bin/server/audio/device.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use log::{debug, log_enabled, Level};

pub fn get_default_host(host_id: cpal::HostId) -> cpal::Host {
if log_enabled!(Level::Debug) {
debug!("Available audio systems:");
for host in cpal::available_hosts() {
debug!("host: {:?}", host);
}
tracing::debug!("Available audio systems:");
for host in cpal::available_hosts() {
tracing::debug!("host: {:?}", host);
}
cpal::host_from_id(
cpal::available_hosts()
Expand Down
2 changes: 1 addition & 1 deletion src/bin/server/audio/symphonia/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl PacketDecoder {
Err(SymphoniaError::IoError(_)) => Ok(vec![]),
Err(SymphoniaError::DecodeError(_)) => Ok(vec![]),
Err(err) => {
log::error!("Unhandled symphonia error: {}", err);
tracing::error!("Unhandled symphonia error: {}", err);
Err(DiziError::from(err))
}
}
Expand Down
21 changes: 9 additions & 12 deletions src/bin/server/audio/symphonia/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use dizi::error::{DiziError, DiziErrorKind, DiziResult};
use dizi::player::{PlayerState, PlayerStatus};
use dizi::playlist::PlaylistType;
use dizi::song::Song;
use log::{debug, log_enabled, Level};

use crate::audio::device::get_default_host;
use crate::audio::request::PlayerRequest;
Expand Down Expand Up @@ -84,9 +83,7 @@ impl SymphoniaPlayer {
}

fn play(&mut self, song: &Song) -> DiziResult {
if log_enabled!(Level::Debug) {
debug!("Song: {:#?}", song);
}
tracing::debug!("Song: {:#?}", song);

self.player_stream_req().send(PlayerRequest::Play {
song: song.clone(),
Expand Down Expand Up @@ -138,7 +135,7 @@ impl AudioPlayer for SymphoniaPlayer {
let song = playlist.entry_mut(entry.song_index);
if !song.metadata_loaded() {
if let Err(err) = song.load_metadata() {
log::error!("Failed to load metadata: {}", err);
tracing::error!("Failed to load metadata: {}", err);
song.set_metadata_loaded(true);
}
}
Expand Down Expand Up @@ -171,7 +168,7 @@ impl AudioPlayer for SymphoniaPlayer {
let song = playlist.entry_mut(entry.song_index);
if !song.metadata_loaded() {
if let Err(err) = song.load_metadata() {
log::error!("Failed to load metadata: {}", err);
tracing::error!("Failed to load metadata: {}", err);
song.set_metadata_loaded(true);
}
}
Expand All @@ -196,7 +193,7 @@ impl AudioPlayer for SymphoniaPlayer {
let song = playlist.entry_mut(entry.song_index);
if !song.metadata_loaded() {
if let Err(err) = song.load_metadata() {
log::error!("Failed to load metadata: {}", err);
tracing::error!("Failed to load metadata: {}", err);
song.set_metadata_loaded(true);
}
}
Expand All @@ -210,7 +207,7 @@ impl AudioPlayer for SymphoniaPlayer {
let song = playlist.entry_mut(entry.song_index);
if !song.metadata_loaded() {
if let Err(err) = song.load_metadata() {
log::error!("Failed to load metadata: {}", err);
tracing::error!("Failed to load metadata: {}", err);
song.set_metadata_loaded(true);
}
}
Expand All @@ -236,7 +233,7 @@ impl AudioPlayer for SymphoniaPlayer {
let song = playlist.entry_mut(song_entry.song_index);
if !song.metadata_loaded() {
if let Err(err) = song.load_metadata() {
log::error!("Failed to load metadata: {}", err);
tracing::error!("Failed to load metadata: {}", err);
song.set_metadata_loaded(true);
}
}
Expand All @@ -254,7 +251,7 @@ impl AudioPlayer for SymphoniaPlayer {
let song = playlist.entry_mut(song_entry.song_index);
if !song.metadata_loaded() {
if let Err(err) = song.load_metadata() {
log::error!("Failed to load metadata: {}", err);
tracing::error!("Failed to load metadata: {}", err);
song.set_metadata_loaded(true);
}
}
Expand All @@ -278,7 +275,7 @@ impl AudioPlayer for SymphoniaPlayer {
let song = playlist.entry_mut(song_entry.song_index);
if !song.metadata_loaded() {
if let Err(err) = song.load_metadata() {
log::error!("Failed to load metadata: {}", err);
tracing::error!("Failed to load metadata: {}", err);
song.set_metadata_loaded(true);
}
}
Expand All @@ -296,7 +293,7 @@ impl AudioPlayer for SymphoniaPlayer {
let song = playlist.entry_mut(song_entry.song_index);
if !song.metadata_loaded() {
if let Err(err) = song.load_metadata() {
log::error!("Failed to load metadata: {}", err);
tracing::error!("Failed to load metadata: {}", err);
song.set_metadata_loaded(true);
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/bin/server/audio/symphonia/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::sync::mpsc;
use std::thread;
use std::time::Duration;

use log::{debug, log_enabled, Level};
use symphonia::core::codecs::{DecoderOptions, CODEC_TYPE_NULL};
use symphonia::core::formats::FormatOptions;
use symphonia::core::io::MediaSourceStream;
Expand Down Expand Up @@ -255,9 +254,7 @@ impl PlayerStream {
// Store the track identifier, it will be used to filter packets.
let track_id = track.id;

if log_enabled!(Level::Debug) {
debug!("track: {:#?}", track);
}
tracing::debug!("track: {:#?}", track);

// Use the default options for the decoder.
let dec_opts: DecoderOptions = Default::default();
Expand All @@ -283,9 +280,7 @@ impl PlayerStream {
buffer_size: cpal::BufferSize::Default,
};

if log_enabled!(Level::Debug) {
debug!("audio_config: {:#?}", audio_config);
}
tracing::debug!("audio_config: {:#?}", audio_config);

let stream_tx = self.event_poller.stream_tx.clone();

Expand Down
20 changes: 8 additions & 12 deletions src/bin/server/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use std::sync::mpsc;
use std::thread;
use std::time;

use log::{debug, log_enabled, Level};

use dizi::request::client::ClientRequest;
use dizi::response::server::ServerBroadcastEvent;

Expand Down Expand Up @@ -109,16 +107,14 @@ impl Events {
}

pub fn broadcast_event(&mut self, event: ServerBroadcastEvent) {
if log_enabled!(Level::Debug) {
match &event {
ServerBroadcastEvent::PlayerState { .. } => {}
event => {
debug!(
"Server broadcast: {:#?} to {} clients",
event,
self.server_broadcast_listeners.len()
);
}
match &event {
ServerBroadcastEvent::PlayerState { .. } => {}
event => {
tracing::debug!(
"Server broadcast: {:#?} to {} clients",
event,
self.server_broadcast_listeners.len()
);
}
}
for (_, server_tx) in self.server_broadcast_listeners.iter() {
Expand Down
18 changes: 10 additions & 8 deletions src/bin/server/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ use std::path::PathBuf;

use clap::Parser;

use lazy_static::lazy_static;
use log::{debug, log_enabled, Level};

use dizi::error::DiziResult;
use lazy_static::lazy_static;
use tracing_subscriber::{prelude::*, EnvFilter};

use crate::config::{AppConfig, TomlConfigFile};

Expand Down Expand Up @@ -74,15 +73,18 @@ fn run_server(args: CommandArgs) -> DiziResult {

let config = AppConfig::get_config(CONFIG_FILE);

if log_enabled!(Level::Debug) {
debug!("{:#?}", config);
}
let env_filter = EnvFilter::from_default_env();
let fmt_layer = tracing_subscriber::fmt::layer();
tracing_subscriber::registry()
.with(env_filter)
.with(fmt_layer)
.init();

tracing::debug!("{:#?}", config);
server::serve(config)
}

fn main() {
env_logger::init();

let args = CommandArgs::parse();
let res = run_server(args);

Expand Down
24 changes: 6 additions & 18 deletions src/bin/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::os::unix::net::UnixListener;
use std::path::Path;
use std::thread;

use log::{debug, log_enabled, Level};

use dizi::error::DiziResult;
use dizi::response::server::ServerBroadcastEvent;

Expand Down Expand Up @@ -39,17 +37,13 @@ pub fn serve(config: AppConfig) -> DiziResult {
Err(_) => return Ok(()),
};

if log_enabled!(Level::Debug) {
debug!("Server Event: {:?}", event);
}
tracing::debug!("Server Event: {:?}", event);

match event {
AppEvent::Client { uuid, request } => {
let res = server_util::process_client_request(&mut context, &uuid, request);
if let Err(err) = res {
if log_enabled!(Level::Debug) {
debug!("Error: {:?}", err);
}
tracing::debug!("Error: {:?}", err);
context
.events
.broadcast_event(ServerBroadcastEvent::ServerError {
Expand All @@ -59,10 +53,8 @@ pub fn serve(config: AppConfig) -> DiziResult {
}
AppEvent::Server(event) => {
let res = server_util::process_server_event(&mut context, event);
if log_enabled!(Level::Debug) {
if let Err(err) = res {
debug!("Error: {:?}", err);
}
if let Err(err) = res {
tracing::debug!("Error: {:?}", err);
}
}
}
Expand All @@ -71,19 +63,15 @@ pub fn serve(config: AppConfig) -> DiziResult {
let playlist_path = context.config_ref().server_ref().playlist_ref();
let playlist = context.player_ref().playlist_ref().file_playlist_ref();

if log_enabled!(Level::Debug) {
debug!("Saving playlist to '{}'", playlist_path.to_string_lossy());
}
tracing::debug!("Saving playlist to '{}'", playlist_path.to_string_lossy());

let mut file = std::fs::File::create(playlist_path)?;
let mut writer = m3u::Writer::new(&mut file);
for song in playlist.iter() {
let entry = m3u::Entry::Path(song.file_path().to_path_buf());
writer.write_entry(&entry)?;
}
if log_enabled!(Level::Debug) {
debug!("Playlist saved!");
}
tracing::debug!("Playlist saved!");

context
.events
Expand Down
10 changes: 2 additions & 8 deletions src/bin/server/server_commands/player.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::path::Path;

use log::{debug, log_enabled, Level};

use dizi::error::DiziResult;
use dizi::player::PlayerStatus;

Expand Down Expand Up @@ -47,9 +45,7 @@ pub fn player_volume_increase(context: &mut AppContext, amount: usize) -> DiziRe
};
player_set_volume(context, volume)?;

if log_enabled!(Level::Debug) {
debug!("volume is now: {}", volume);
}
tracing::debug!("volume is now: {volume}");
Ok(volume)
}

Expand All @@ -59,9 +55,7 @@ pub fn player_volume_decrease(context: &mut AppContext, amount: usize) -> DiziRe
let volume = if amount > volume { 0 } else { volume - amount };
player_set_volume(context, volume)?;

if log_enabled!(Level::Debug) {
debug!("volume is now: {}", volume);
}
tracing::debug!("volume is now: {volume}");
Ok(volume)
}

Expand Down
Loading

0 comments on commit 3616c84

Please sign in to comment.