From b548b908e7700f9a6ca410e843f375bb7f7b1737 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Tue, 21 Feb 2023 06:04:39 -0600 Subject: [PATCH] fucking crowdstrike --- src/error.rs | 4 ++++ src/log.rs | 47 +++++++++++++++++++++++++++++++++++++++++++---- src/main.rs | 11 ++++++++++- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/error.rs b/src/error.rs index 56c1f11..19b87e1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,3 +1,4 @@ +use std::io; use thiserror::Error; use windows::Win32::Foundation::{GetLastError, BOOL}; @@ -5,6 +6,9 @@ use windows::Win32::Foundation::{GetLastError, BOOL}; pub enum Error { #[error("windows error: {0}")] WindowsError(String), + + #[error("i/o error: {0}")] + IOError(#[from] io::Error), } impl Error { diff --git a/src/log.rs b/src/log.rs index 28f4563..f226448 100644 --- a/src/log.rs +++ b/src/log.rs @@ -1,19 +1,58 @@ +use crate::error::Error; pub use log::{debug, error, info, set_logger, set_max_level, trace, warn, LevelFilter}; +use std::{ + fs::File, + io, + path::Path, + sync::{Arc, Mutex}, +}; + use log::{Metadata, Record}; -pub struct Log; +pub struct Log { + out: Arc>, +} + +impl Log { + pub fn file>(p: P) -> Result { + let f = File::options().append(true).create(true).open(p)?; + Ok(Self { + out: Arc::new(Mutex::new(f)), + }) + } +} -impl log::Log for Log { +impl Log +where + W: io::Write, +{ + pub fn from_writer(w: W) -> Self { + Self { + out: Arc::new(Mutex::new(w)), + } + } +} + +impl log::Log for Log +where + W: io::Write + Send, +{ fn enabled(&self, _metadata: &Metadata) -> bool { true } fn log(&self, record: &Record) { if self.enabled(record.metadata()) { - println!("{} - {}", record.level(), record.args()); + if let Ok(out) = self.out.lock().as_deref_mut() { + _ = write!(out, "{} - {}\n", record.level(), record.args()); + } } } - fn flush(&self) {} + fn flush(&self) { + if let Ok(out) = self.out.lock().as_deref_mut() { + _ = out.flush(); + } + } } diff --git a/src/main.rs b/src/main.rs index aac5323..5772efe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -244,7 +244,16 @@ fn setup_stdout() -> Result<()> { } fn main() -> Result<()> { - _ = set_logger(&Log {}).map(|()| set_max_level(LevelFilter::Info)); + match Log::file("C:\\Users\\JordanOrelli\\wash.log") { + Ok(f) => { + let target = Box::leak(Box::new(f)); + _ = set_logger(target).map(|()| set_max_level(LevelFilter::Debug)); + } + Err(e) => { + println!("did not open log file: {}", e); + } + } + setup_stdin()?; setup_stdout()?;