1
+ use crate :: procenv;
2
+
3
+ use std:: fmt:: Display ;
1
4
use std:: os:: windows:: ffi:: OsStrExt ;
2
5
use std:: ffi:: OsStr ;
3
- use std:: sync:: { Arc , Mutex } ;
6
+ use std:: sync:: RwLock ;
4
7
use std:: process;
5
8
use std:: env;
6
9
@@ -21,20 +24,63 @@ pub fn output_debug_string(msg: impl AsRef<OsStr>) {
21
24
unsafe { OutputDebugStringW ( msg_wide. as_ptr ( ) ) ; }
22
25
}
23
26
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
+
28
60
}
29
61
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 > >
33
69
}
34
70
35
71
impl Log for PirtLogger {
36
72
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
+ }
38
84
}
39
85
40
86
fn log ( & self , record : & log:: Record ) {
@@ -44,26 +90,4 @@ impl Log for PirtLogger {
44
90
fn flush ( & self ) {
45
91
todo ! ( )
46
92
}
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
- }
69
93
}
0 commit comments