Skip to content

Commit

Permalink
Cargo fmt and clippy fix
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenByteDev committed Jul 30, 2023
1 parent 58a9b21 commit 152d00a
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 32 deletions.
7 changes: 4 additions & 3 deletions burnt-sushi/src/blocker.rs
Expand Up @@ -88,7 +88,7 @@ impl SpotifyHookState {

match spotify.process.pid().ok() {
Some(pid) => info!("Found Spotify (PID={pid})"),
None => info!("Found Spotify")
None => info!("Found Spotify"),
}
let syringe = Syringe::for_process(spotify.process);

Expand Down Expand Up @@ -116,7 +116,7 @@ impl SpotifyHookState {
info!("Ejecting previous blocker...");
match syringe.eject(prev_payload) {
Ok(_) => info!("Ejected previous blocker"),
Err(_) => error!("Failed to eject previous blocker")
Err(_) => error!("Failed to eject previous blocker"),
};
}

Expand All @@ -131,7 +131,8 @@ impl SpotifyHookState {
.context("Failed to resolve blocker.")?;

info!("Injecting blocker...");
let payload = syringe.inject(payload_path)
let payload = syringe
.inject(payload_path)
.context("Failed to inject blocker.")?;

debug!("Starting RPC...");
Expand Down
7 changes: 2 additions & 5 deletions burnt-sushi/src/logger/console/log.rs
Expand Up @@ -23,7 +23,7 @@ use winapi::{
},
};

use crate::{APP_NAME_WITH_VERSION, logger::SimpleLog};
use crate::{logger::SimpleLog, APP_NAME_WITH_VERSION};

use super::raw;

Expand All @@ -34,10 +34,7 @@ pub struct Console(ConsoleImpl);
enum ConsoleImpl {
Attach,
Alloc,
Piped {
process: OwnedProcess,
pipe: File,
},
Piped { process: OwnedProcess, pipe: File },
}

unsafe impl Send for Console {}
Expand Down
41 changes: 28 additions & 13 deletions burnt-sushi/src/logger/file.rs
@@ -1,9 +1,10 @@
#![allow(dead_code)]

use std::{
fs::{File, self},
io::{Write, BufWriter, Read, SeekFrom, Seek},
path::PathBuf, time::Instant,
fs::{self, File},
io::{BufWriter, Read, Seek, SeekFrom, Write},
path::PathBuf,
time::Instant,
};

use anyhow::Context;
Expand All @@ -14,15 +15,15 @@ use super::SimpleLog;
pub struct FileLog {
path: PathBuf,
file: Option<BufWriter<File>>,
last_written: Option<Instant>
last_written: Option<Instant>,
}

impl FileLog {
pub fn new(path: impl Into<PathBuf>) -> Self {
Self {
path: path.into(),
file: None,
last_written: None
last_written: None,
}
}

Expand All @@ -34,26 +35,37 @@ impl FileLog {
if let Some(dir) = self.path.parent() {
fs::create_dir_all(dir).context("Failed to create parent directories for log file.")?;
}
let mut file = File::options().create(true).append(true).open(&self.path).context("Failed to open or create log file.")?;

let mut file = File::options()
.create(true)
.append(true)
.open(&self.path)
.context("Failed to open or create log file.")?;

dbg!();
if dbg!(dbg!(file.metadata().unwrap().len()) > 100 * 1024) {
file = File::options().write(true).read(true).open(&self.path).context("Failed to open or create log file.")?;
file = File::options()
.write(true)
.read(true)
.open(&self.path)
.context("Failed to open or create log file.")?;
let mut contents = String::new();
file.read_to_string(&mut contents).context("Failed to read log file.")?;
file.read_to_string(&mut contents)
.context("Failed to read log file.")?;

let mut truncated_contents = String::new();
for (index, _) in contents.match_indices('\n') {
let succeeding = &contents[(index+1)..];
let succeeding = &contents[(index + 1)..];
if succeeding.len() > 10 * 1024 {
continue;
}
truncated_contents.push_str(succeeding);
break;
}
file.seek(SeekFrom::Start(0)).context("Failed to seek in log file.")?;
file.seek(SeekFrom::Start(0))
.context("Failed to seek in log file.")?;
file.set_len(0).context("Failed to clear log file.")?;
file.write_all(truncated_contents.as_bytes()).context("Failed to write log file.")?;
file.write_all(truncated_contents.as_bytes())
.context("Failed to write log file.")?;
}
let writer = BufWriter::new(file);
Ok(self.file.insert(writer))
Expand All @@ -62,7 +74,10 @@ impl FileLog {

impl SimpleLog for FileLog {
fn log(&mut self, message: &str) {
let file = self.open_file().context("Failed to prepare log file.").unwrap();
let file = self
.open_file()
.context("Failed to prepare log file.")
.unwrap();
writeln!(file, "{}", message).unwrap();
file.flush().unwrap();
}
Expand Down
11 changes: 6 additions & 5 deletions burnt-sushi/src/logger/global.rs
@@ -1,11 +1,13 @@
use std::{sync::{Mutex, MutexGuard}, fmt::Debug};
use std::{
fmt::Debug,
sync::{Mutex, MutexGuard},
};

use log::Log;

use crate::APP_NAME;

use super::{SimpleLog, Console, FileLog};

use super::{Console, FileLog, SimpleLog};

static LOGGER: GlobalLoggerHolder = GlobalLoggerHolder(Mutex::new(GlobalLogger::new()));

Expand Down Expand Up @@ -37,12 +39,11 @@ impl GlobalLogger {
pub const fn new() -> Self {
GlobalLogger {
console: None,
file: None
file: None,
}
}
}


impl Log for GlobalLoggerHolder {
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
Expand Down
4 changes: 2 additions & 2 deletions burnt-sushi/src/logger/mod.rs
@@ -1,10 +1,10 @@
pub mod global;
pub mod console;
pub mod file;
pub mod global;
pub mod noop;

mod traits;

pub use traits::*;
pub use console::Console;
pub use file::FileLog;
pub use traits::*;
2 changes: 1 addition & 1 deletion burnt-sushi/src/logger/traits.rs
@@ -1,4 +1,4 @@
use std::{fmt::Debug, any::Any};
use std::{any::Any, fmt::Debug};

pub trait SimpleLog: Any + Debug + Send + Sync {
fn log(&mut self, message: &str);
Expand Down
7 changes: 6 additions & 1 deletion burnt-sushi/src/main.rs
Expand Up @@ -20,7 +20,12 @@ use winapi::{

use std::{env, io, os::windows::prelude::FromRawHandle, time::Duration};

use crate::{args::{ARGS, LogLevel}, blocker::SpotifyAdBlocker, named_mutex::NamedMutex, logger::{Console, FileLog}};
use crate::{
args::{LogLevel, ARGS},
blocker::SpotifyAdBlocker,
logger::{Console, FileLog},
named_mutex::NamedMutex,
};

mod args;
mod blocker;
Expand Down
2 changes: 1 addition & 1 deletion burnt-sushi/src/tray.rs
Expand Up @@ -121,7 +121,7 @@ impl SystemTrayIcon {

fn show_console(&self) {
let mut l = logger::global::get();
if !l.console.is_some() {
if l.console.is_none() {
l.console = Some(Console::piped().unwrap());
}
}
Expand Down
2 changes: 1 addition & 1 deletion shared/src/lib.rs
Expand Up @@ -12,7 +12,7 @@ pub mod rpc {
pub use super::spotify_ad_guard_capnp::*;
}

#[allow(clippy::derive_hash_xor_eq)]
#[allow(clippy::derived_hash_with_manual_eq)]
impl hash::Hash for rpc::blocker_service::FilterHook {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
core::mem::discriminant(self).hash(state)
Expand Down

0 comments on commit 152d00a

Please sign in to comment.