@@ -18,7 +18,7 @@ limitations under the License.
1818// it will only log messages from the hyperlight-guest target. It will not log messages from other targets.
1919// this target is only used when handling an outb log request from the guest, so this logger will only capture those messages.
2020
21- use std:: sync:: Once ;
21+ use std:: sync:: { Mutex , Once } ;
2222use std:: thread:: current;
2323
2424use log:: { set_logger, set_max_level, Level , Log , Metadata , Record } ;
@@ -35,8 +35,8 @@ pub struct LogCall {
3535 pub module_path : Option < String > ,
3636}
3737
38- static mut LOGCALLS : Vec < LogCall > = Vec :: < LogCall > :: new ( ) ;
39- static mut NUMBER_OF_ENABLED_CALLS : usize = 0 ;
38+ static LOGCALLS : Mutex < Vec < LogCall > > = Mutex :: new ( Vec :: < LogCall > :: new ( ) ) ;
39+ static NUMBER_OF_ENABLED_CALLS : Mutex < usize > = Mutex :: new ( 0 ) ;
4040
4141pub struct SimpleLogger { }
4242
@@ -49,29 +49,23 @@ impl SimpleLogger {
4949 }
5050
5151 pub fn num_enabled_calls ( & self ) -> usize {
52- unsafe { NUMBER_OF_ENABLED_CALLS }
52+ * NUMBER_OF_ENABLED_CALLS . lock ( ) . unwrap ( )
5353 }
5454
5555 pub fn num_log_calls ( & self ) -> usize {
56- unsafe { LOGCALLS . len ( ) }
56+ LOGCALLS . lock ( ) . unwrap ( ) . len ( )
5757 }
5858 pub fn get_log_call ( & self , idx : usize ) -> Option < LogCall > {
59- unsafe { LOGCALLS . get ( idx) . cloned ( ) }
59+ LOGCALLS . lock ( ) . unwrap ( ) . get ( idx) . cloned ( )
6060 }
6161
6262 pub fn clear_log_calls ( & self ) {
63- unsafe {
64- LOGCALLS . clear ( ) ;
65- NUMBER_OF_ENABLED_CALLS = 0 ;
66- }
63+ LOGCALLS . lock ( ) . unwrap ( ) . clear ( ) ;
64+ * NUMBER_OF_ENABLED_CALLS . lock ( ) . unwrap ( ) = 0 ;
6765 }
6866
6967 pub fn test_log_records < F : Fn ( & Vec < LogCall > ) > ( & self , f : F ) {
70- unsafe {
71- // this logger is only used for testing so unsafe is fine here
72- #[ allow( static_mut_refs) ]
73- f ( & LOGCALLS ) ;
74- } ;
68+ f ( & LOGCALLS . lock ( ) . unwrap ( ) ) ;
7569 self . clear_log_calls ( ) ;
7670 }
7771}
@@ -82,36 +76,32 @@ impl Log for SimpleLogger {
8276 // because the guest derives its log level from the host log level then the number times that enabled is called for
8377 // the "hyperlight-guest" target will be the same as the number of messages logged by the guest.
8478 // In other words this function should always return true for the "hyperlight-guest" target.
85- unsafe {
86- if metadata. target ( ) == "hyperlight-guest" {
87- NUMBER_OF_ENABLED_CALLS += 1 ;
88- }
89- metadata. target ( ) == "hyperlight-guest" && metadata. level ( ) <= log:: max_level ( )
79+ if metadata. target ( ) == "hyperlight-guest" {
80+ * NUMBER_OF_ENABLED_CALLS . lock ( ) . unwrap ( ) += 1 ;
9081 }
82+ metadata. target ( ) == "hyperlight-guest" && metadata. level ( ) <= log:: max_level ( )
9183 }
9284 fn log ( & self , record : & Record ) {
9385 if !self . enabled ( record. metadata ( ) ) {
9486 return ;
9587 }
9688
97- unsafe {
98- LOGCALLS . push ( LogCall {
99- level : record. level ( ) ,
100- args : format ! ( "{}" , record. args( ) ) ,
101- target : record. target ( ) . to_string ( ) ,
102- line : record. line ( ) ,
103- file : match record. file ( ) {
104- None => record. file_static ( ) . map ( |file| file. to_string ( ) ) ,
105- Some ( file) => Some ( file. to_string ( ) ) ,
106- } ,
107- module_path : match record. module_path ( ) {
108- None => record
109- . module_path_static ( )
110- . map ( |module_path| module_path. to_string ( ) ) ,
111- Some ( module_path) => Some ( module_path. to_string ( ) ) ,
112- } ,
113- } ) ;
114- } ;
89+ LOGCALLS . lock ( ) . unwrap ( ) . push ( LogCall {
90+ level : record. level ( ) ,
91+ args : format ! ( "{}" , record. args( ) ) ,
92+ target : record. target ( ) . to_string ( ) ,
93+ line : record. line ( ) ,
94+ file : match record. file ( ) {
95+ None => record. file_static ( ) . map ( |file| file. to_string ( ) ) ,
96+ Some ( file) => Some ( file. to_string ( ) ) ,
97+ } ,
98+ module_path : match record. module_path ( ) {
99+ None => record
100+ . module_path_static ( )
101+ . map ( |module_path| module_path. to_string ( ) ) ,
102+ Some ( module_path) => Some ( module_path. to_string ( ) ) ,
103+ } ,
104+ } ) ;
115105
116106 println ! ( "Thread {:?} {:?}" , current( ) . id( ) , record) ;
117107 }
0 commit comments