Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5768ce2

Browse files
start of a logging implementation
1 parent 08a6c77 commit 5768ce2

File tree

3 files changed

+92
-7
lines changed

3 files changed

+92
-7
lines changed

‎pirt/Cargo.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ version = "0.1.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
[lib]
8+
crate-type = ["cdylib"]
79

810
[dependencies]
11+
log = "0.4"

‎pirt/src/lib.rs‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1-
#[cfg(test)]
2-
mod tests {
3-
#[test]
4-
fn it_works() {
5-
let result = 2 + 2;
6-
assert_eq!(result, 4);
7-
}
1+
mod logging;
2+
3+
use std::fs;
4+
use std::io::Result;
5+
use std::sync::Once;
6+
7+
use log;
8+
use log::LevelFilter;
9+
10+
static LOGGING_SETUP: Once = Once::new();
11+
12+
/*
13+
fn index_ship() -> Result<()> {
14+
let dir = fs::read_dir("C:\\kptnhook\\ship")?
15+
16+
Ok(())
17+
} */
18+
19+
fn logging_setup() {
820
}
21+
22+
#[no_mangle]
23+
extern "system" fn DllMain(_: *const u8, _: u32, _: *const u8) -> u32 {
24+
// load the other pirts from the ship
25+
LOGGING_SETUP.call_once(logging_setup);
26+
27+
1
28+
}

‎pirt/src/logging.rs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::os::windows::ffi::OsStrExt;
2+
use std::ffi::OsStr;
3+
use std::sync::{ Arc, Mutex };
4+
use std::process;
5+
use std::env;
6+
7+
use log::{ Log, Level, LevelFilter };
8+
9+
#[cfg(windows)]
10+
extern "stdcall" {
11+
fn OutputDebugStringW(chars: *const u16);
12+
}
13+
14+
pub fn output_debug_string(msg: impl AsRef<OsStr>) {
15+
let msg_wide = msg
16+
.as_ref()
17+
.encode_wide()
18+
.chain(Some(0)) // null terminator
19+
.collect::<Vec<_>>();
20+
21+
unsafe { OutputDebugStringW(msg_wide.as_ptr()); }
22+
}
23+
24+
static EARLY_LOGGER: EarlyLogger = EarlyLogger {};
25+
static LOGGER: PirtLogger = PirtLogger {};
26+
27+
fn init() {
28+
log::set_logger(&EarlyLogger);
29+
}
30+
31+
struct PirtLogger;
32+
struct EarlyLogger;
33+
34+
impl Log for PirtLogger {
35+
fn enabled(&self, metadata: &log::Metadata) -> bool {
36+
todo!()
37+
}
38+
39+
fn log(&self, record: &log::Record) {
40+
todo!()
41+
}
42+
43+
fn flush(&self) {
44+
todo!()
45+
}
46+
}
47+
48+
impl Log for EarlyLogger {
49+
fn flush(&self) { }
50+
fn enabled(&self, _: &log::Metadata) -> bool { true }
51+
fn log(&self, record: &log::Record) {
52+
let procname = match env::current_exe() {
53+
Ok(p) => p.file_name()
54+
.map(|f| f.to_string_lossy().into_owned())
55+
.unwrap_or("{no filename}".to_string()),
56+
Err(c) => format!("{{filename err {}}}", c)
57+
};
58+
59+
output_debug_string(
60+
format!("kptnhook<{}@{}> {}: {}", procname, process::id(), record.args(), record.level()));
61+
}
62+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /