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 9263ed6

Browse files
some more logging
1 parent 692c6c4 commit 9263ed6

File tree

3 files changed

+68
-33
lines changed

3 files changed

+68
-33
lines changed

‎pirt/src/lib.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod procenv;
12
mod logging;
23

34
use std::fs;
@@ -16,13 +17,14 @@ fn index_ship() -> Result<()> {
1617
Ok(())
1718
} */
1819

19-
fn logging_setup() {
20+
fn setup() {
21+
logging::init();
2022
}
2123

2224
#[no_mangle]
2325
extern "system" fn DllMain(_: *const u8, _: u32, _: *const u8) -> u32 {
2426
// load the other pirts from the ship
25-
LOGGING_SETUP.call_once(logging_setup);
27+
LOGGING_SETUP.call_once(setup);
2628

2729
1
2830
}

‎pirt/src/logging.rs‎

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use crate::procenv;
2+
3+
use std::fmt::Display;
14
use std::os::windows::ffi::OsStrExt;
25
use std::ffi::OsStr;
3-
use std::sync::{Arc,Mutex};
6+
use std::sync::RwLock;
47
use std::process;
58
use std::env;
69

@@ -21,20 +24,63 @@ pub fn output_debug_string(msg: impl AsRef<OsStr>) {
2124
unsafe { OutputDebugStringW(msg_wide.as_ptr()); }
2225
}
2326

24-
fn init() -> Result<(), String> {
25-
let early = EarlyLogger::new();
26-
log::set_boxed_logger(Box::new(early))
27-
.map_err(|e| format!("kptnhook error initializing logging: {}", e.to_string()))?;
27+
fn procname_failsafe() -> String {
28+
match procenv::procname() {
29+
Ok(name) => name.unwrap_or("{filename missing}".to_string()),
30+
Err(e) => format!("{{filename err {}}}", e)
31+
}
32+
}
33+
34+
pub(crate) fn init() {
35+
let logger = PirtLogger::default();
36+
if let Err(_) = log::set_boxed_logger(Box::new(logger)) {
37+
log_dbg("attempted to initialize logging twice. please report this error!", Level::Error);
38+
}
39+
}
40+
41+
pub(crate) fn configure(cfg: LoggingCfg) {
42+
43+
}
44+
45+
fn log_dbg(msg: impl Display, level: Level) {
46+
output_debug_string(
47+
format!("kptnhook<{}@{}> {}: {}",
48+
procname_failsafe(),
49+
procenv::pid(),
50+
msg,
51+
level));
52+
}
53+
54+
pub(crate) struct FileLogCfg {
55+
56+
}
57+
58+
pub(crate) struct DbgLogCfg {
59+
2860
}
2961

30-
struct PirtLogger;
31-
struct EarlyLogger {
32-
level: Mutex<Level>
62+
pub(crate) struct LoggingCfg {
63+
level: Level
64+
}
65+
66+
#[derive(Default)]
67+
struct PirtLogger {
68+
cfg: RwLock<Option<LoggingCfg>>
3369
}
3470

3571
impl Log for PirtLogger {
3672
fn enabled(&self, metadata: &log::Metadata) -> bool {
37-
todo!()
73+
match self.cfg.read() {
74+
// logging no configured yet
75+
Ok(x) => x
76+
.as_ref()
77+
.map(|x| metadata.level() >= x.level)
78+
.unwrap_or(true),
79+
Err(_) => {
80+
log_dbg("logging lock is poisoned, please report this error.", Level::Error);
81+
true
82+
}
83+
}
3884
}
3985

4086
fn log(&self, record: &log::Record) {
@@ -44,26 +90,4 @@ impl Log for PirtLogger {
4490
fn flush(&self) {
4591
todo!()
4692
}
47-
}
48-
49-
impl EarlyLogger {
50-
pub fn new() -> Self {
51-
EarlyLogger { level: Mutex::new(Level::Trace) }
52-
}
53-
}
54-
55-
impl Log for EarlyLogger {
56-
fn flush(&self) { }
57-
fn enabled(&self, _: &log::Metadata) -> bool { true }
58-
fn log(&self, record: &log::Record) {
59-
let procname = match env::current_exe() {
60-
Ok(p) => p.file_name()
61-
.map(|f| f.to_string_lossy().into_owned())
62-
.unwrap_or("{no filename}".to_string()),
63-
Err(c) => format!("{{filename err {}}}", c)
64-
};
65-
66-
output_debug_string(
67-
format!("kptnhook<{}@{}> {}: {}", procname, process::id(), record.args(), record.level()));
68-
}
6993
}

‎pirt/src/procenv.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use std::env;
2+
use std::io::Result as IOResult;
3+
use std::process;
4+
5+
pub fn procname() -> IOResult<Option<String>> {
6+
Ok(env::current_exe()?.file_name().map(|f| f.to_string_lossy().into_owned()))
7+
}
8+
9+
pub fn pid() -> u32 { process::id() }

0 commit comments

Comments
(0)

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