11use std:: fs:: OpenOptions ;
22use std:: io:: Write ;
3+ use std:: path:: Path ;
4+ use std:: path:: PathBuf ;
35
46const LOG_COMMAND_PREVIEW_LIMIT : usize = 200 ;
57pub const LOG_FILE_NAME : & str = "sandbox_commands.rust.log" ;
@@ -13,35 +15,43 @@ fn preview(command: &[String]) -> String {
1315 }
1416}
1517
16- fn append_line ( line : & str ) {
17- if let Ok ( mut f) = OpenOptions :: new ( )
18- . create ( true )
19- . append ( true )
20- . open ( LOG_FILE_NAME )
21- {
22- let _ = writeln ! ( f, "{}" , line) ;
18+ fn log_file_path ( base_dir : & Path ) -> Option < PathBuf > {
19+ if base_dir. is_dir ( ) {
20+ Some ( base_dir. join ( LOG_FILE_NAME ) )
21+ } else {
22+ None
23+ }
24+ }
25+
26+ fn append_line ( line : & str , base_dir : Option < & Path > ) {
27+ if let Some ( dir) = base_dir {
28+ if let Some ( path) = log_file_path ( dir) {
29+ if let Ok ( mut f) = OpenOptions :: new ( ) . create ( true ) . append ( true ) . open ( path) {
30+ let _ = writeln ! ( f, "{}" , line) ;
31+ }
32+ }
2333 }
2434}
2535
26- pub fn log_start ( command : & [ String ] ) {
36+ pub fn log_start ( command : & [ String ] , base_dir : Option < & Path > ) {
2737 let p = preview ( command) ;
28- append_line ( & format ! ( "START: {}" , p ) ) ;
38+ append_line ( & format ! ( "START: {p}" ) , base_dir ) ;
2939}
3040
31- pub fn log_success ( command : & [ String ] ) {
41+ pub fn log_success ( command : & [ String ] , base_dir : Option < & Path > ) {
3242 let p = preview ( command) ;
33- append_line ( & format ! ( "SUCCESS: {}" , p ) ) ;
43+ append_line ( & format ! ( "SUCCESS: {p}" ) , base_dir ) ;
3444}
3545
36- pub fn log_failure ( command : & [ String ] , detail : & str ) {
46+ pub fn log_failure ( command : & [ String ] , detail : & str , base_dir : Option < & Path > ) {
3747 let p = preview ( command) ;
38- append_line ( & format ! ( "FAILURE: {} ({})" , p , detail ) ) ;
48+ append_line ( & format ! ( "FAILURE: {p } ({detail })" ) , base_dir ) ;
3949}
4050
4151// Debug logging helper. Emits only when SBX_DEBUG=1 to avoid noisy logs.
42- pub fn debug_log ( msg : & str ) {
52+ pub fn debug_log ( msg : & str , base_dir : Option < & Path > ) {
4353 if std:: env:: var ( "SBX_DEBUG" ) . ok ( ) . as_deref ( ) == Some ( "1" ) {
44- append_line ( & format ! ( "DEBUG: {}" , msg ) ) ;
45- eprintln ! ( "{}" , msg ) ;
54+ append_line ( & format ! ( "DEBUG: {msg}" ) , base_dir ) ;
55+ eprintln ! ( "{msg}" ) ;
4656 }
4757}
0 commit comments