Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows libplugin #521

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions plugins/lib/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
4 changes: 2 additions & 2 deletions plugins/lib/rust/src/bridge.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand Down
40 changes: 33 additions & 7 deletions plugins/lib/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 || {
Expand All @@ -59,6 +84,7 @@ impl Client {
}
}
});
#[cfg(target_family = "unix")]
if ignore_terminate {
let mut signals = Signals::new(&[SIGTERM, SIGUSR1]).unwrap();
thread::spawn(move || {
Expand Down
50 changes: 49 additions & 1 deletion plugins/lib/rust/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ 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},
thread::spawn,
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};
Expand Down Expand Up @@ -51,14 +53,21 @@ 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
.file_name()
.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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -259,3 +302,8 @@ impl Log for Logger {
}
}
}
impl Drop for Logger {
fn drop(&mut self) {
self.flush()
}
}
Loading