diff --git a/plugins/lib/rust/Cargo.toml b/plugins/lib/rust/Cargo.toml index 4c7e5da55..857bad2e5 100644 --- a/plugins/lib/rust/Cargo.toml +++ b/plugins/lib/rust/Cargo.toml @@ -21,3 +21,11 @@ serde_json = "1" [build-dependencies] protobuf-codegen-pure = "2.3" + +[dependencies.windows] +version = "0.48.0" +features = [ + "Win32_System_Diagnostics_ToolHelp", + "Win32_Foundation", + "Win32_System_Console", +] diff --git a/plugins/lib/rust/src/bridge.rs b/plugins/lib/rust/src/bridge.rs index baa248d06..51ec2d1cd 100644 --- a/plugins/lib/rust/src/bridge.rs +++ b/plugins/lib/rust/src/bridge.rs @@ -1,4 +1,4 @@ -// This file is generated by rust-protobuf 2.25.2. Do not edit +// This file is generated by rust-protobuf 2.28.0. Do not edit // @generated // https://github.com/rust-lang/rust-clippy/issues/702 @@ -21,7 +21,7 @@ /// Generated files are compatible only with the same version /// of protobuf runtime. -// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_25_2; +// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_28_0; #[derive(PartialEq,Clone,Default)] pub struct Record { diff --git a/plugins/lib/rust/src/lib.rs b/plugins/lib/rust/src/lib.rs index e1878bbc1..c3cd0af92 100644 --- a/plugins/lib/rust/src/lib.rs +++ b/plugins/lib/rust/src/lib.rs @@ -6,18 +6,25 @@ use crossbeam::channel::{select, tick}; use log::{debug, info}; use parking_lot::Mutex; use protobuf::Message; -use signal_hook::{ - consts::{SIGTERM, SIGUSR1}, - iterator::Signals, -}; +use signal_hook::consts::SIGTERM; use std::{ fs::File, io::{BufReader, BufWriter, Error, Read, Write}, - os::unix::prelude::FromRawFd, sync::Arc, thread, time::Duration, }; + +#[cfg(target_family = "unix")] +use signal_hook::{consts::SIGUSR1, iterator::Signals}; +#[cfg(target_family = "unix")] +use std::os::unix::prelude::FromRawFd; + +#[cfg(target_family = "windows")] +use std::os::windows::prelude::{FromRawHandle, RawHandle}; +#[cfg(target_family = "windows")] +use windows::Win32::System::Console::{GetStdHandle, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE}; + #[derive(Clone)] pub enum EncodeType { Protobuf, @@ -40,10 +47,28 @@ const WRITE_PIPE_FD: i32 = 4; impl Client { pub fn new(ignore_terminate: bool) -> Self { let writer = Arc::new(Mutex::new(BufWriter::with_capacity(512 * 1024, unsafe { - File::from_raw_fd(WRITE_PIPE_FD) + #[cfg(target_family = "unix")] + { + File::from_raw_fd(WRITE_PIPE_FD) + } + + #[cfg(target_family = "windows")] + { + let raw_handle = GetStdHandle(STD_OUTPUT_HANDLE).unwrap(); + File::from_raw_handle(raw_handle.0 as _) + } }))); let reader = Arc::new(Mutex::new(BufReader::new(unsafe { - File::from_raw_fd(READ_PIPE_FD) + #[cfg(target_family = "unix")] + { + File::from_raw_fd(READ_PIPE_FD) + } + + #[cfg(target_family = "windows")] + { + let raw_handle = GetStdHandle(STD_INPUT_HANDLE).unwrap(); + File::from_raw_handle(raw_handle.0 as _) + } }))); let writer_c = writer.clone(); thread::spawn(move || { @@ -59,6 +84,7 @@ impl Client { } } }); + #[cfg(target_family = "unix")] if ignore_terminate { let mut signals = Signals::new(&[SIGTERM, SIGUSR1]).unwrap(); thread::spawn(move || { diff --git a/plugins/lib/rust/src/logger.rs b/plugins/lib/rust/src/logger.rs index 35fdc3f5f..510dabcdd 100644 --- a/plugins/lib/rust/src/logger.rs +++ b/plugins/lib/rust/src/logger.rs @@ -3,7 +3,6 @@ use std::{ ffi::{OsStr, OsString}, fs::{create_dir_all, read_dir, remove_file, rename, File, OpenOptions}, io::{BufReader, Read, Write}, - os::unix::fs::OpenOptionsExt, path::{Path, PathBuf}, process, sync::{Mutex, MutexGuard}, @@ -11,6 +10,9 @@ use std::{ time::{SystemTime, UNIX_EPOCH}, }; +#[cfg(target_family = "unix")] +use std::os::unix::fs::OpenOptionsExt; + use chrono::{DateTime, Local}; use crossbeam::channel::{bounded, Sender}; use flate2::{bufread::GzEncoder, Compression}; @@ -51,7 +53,12 @@ pub struct Logger { } impl Logger { pub fn new(config: Config) -> Self { + #[cfg(target_family = "unix")] let dir = config.path.parent().unwrap_or(Path::new("/tmp")).to_owned(); + + #[cfg(target_family = "windows")] + let dir = config.path.parent().unwrap_or(Path::new(".")).to_owned(); + let _ = create_dir_all(&dir); let filename = config .path @@ -59,6 +66,8 @@ impl Logger { .unwrap_or(OsStr::new(&format!("{}.log", process::id()))) .to_owned(); let mut size = 0; + + #[cfg(target_family = "unix")] let file = match OpenOptions::new() .write(true) .append(true) @@ -75,6 +84,24 @@ impl Logger { None } }; + + #[cfg(target_family = "windows")] + let file = match OpenOptions::new() + .write(true) + .append(true) + .create(true) + .open(dir.join(&filename)) + { + Ok(f) => { + size = f.metadata().unwrap().len(); + Some(f) + } + Err(err) => { + println!("create file failed: {}", err); + None + } + }; + set_max_level(if config.remote_level > config.file_level { config.remote_level } else { @@ -166,6 +193,7 @@ impl Logger { if let Err(_) = rename(&file_path, self.dir.join(new_name)) { **file = None; } else { + #[cfg(target_family = "unix")] match OpenOptions::new() .write(true) .truncate(true) @@ -180,6 +208,21 @@ impl Logger { **file = None; } }; + + #[cfg(target_family = "windows")] + match OpenOptions::new() + .write(true) + .truncate(true) + .create(true) + .open(&file_path) + { + Ok(f) => { + **file = Some(f); + } + Err(_) => { + **file = None; + } + }; } } **size = 0; @@ -259,3 +302,8 @@ impl Log for Logger { } } } +impl Drop for Logger { + fn drop(&mut self) { + self.flush() + } +} \ No newline at end of file