@@ -42,19 +42,19 @@ impl FollowComponents {
42
42
pub struct StdioLoggingExecutorHooks {
43
43
follow_components : FollowComponents ,
44
44
log_dir : Option < PathBuf > ,
45
- refresh_log : bool ,
45
+ truncate_log : bool ,
46
46
}
47
47
48
48
impl StdioLoggingExecutorHooks {
49
49
pub fn new (
50
50
follow_components : FollowComponents ,
51
51
log_dir : Option < PathBuf > ,
52
- refresh_log : bool ,
52
+ truncate_log : bool ,
53
53
) -> Self {
54
54
Self {
55
55
follow_components,
56
56
log_dir,
57
- refresh_log ,
57
+ truncate_log ,
58
58
}
59
59
}
60
60
@@ -65,7 +65,7 @@ impl StdioLoggingExecutorHooks {
65
65
log_dir : Option < & Path > ,
66
66
) -> Result < ComponentStdioWriter > {
67
67
let sanitized_component_id = sanitize_filename:: sanitize ( component_id) ;
68
- let log_path = santized_log_path ( log_dir, & sanitized_component_id, log_suffix) ;
68
+ let log_path = sanitized_log_path ( log_dir, & sanitized_component_id, log_suffix) ;
69
69
let log_path = log_path. as_deref ( ) ;
70
70
71
71
let follow = self . follow_components . should_follow ( component_id) ;
@@ -94,6 +94,35 @@ impl StdioLoggingExecutorHooks {
94
94
_ => Ok ( ( ) ) ,
95
95
}
96
96
}
97
+
98
+ fn truncate_log_files < F : RuntimeFactors > (
99
+ & self ,
100
+ configured_app : & spin_factors:: ConfiguredApp < F > ,
101
+ ) {
102
+ let sanitized_component_ids: Vec < String > = configured_app
103
+ . app ( )
104
+ . components ( )
105
+ . map ( |c| sanitize_filename:: sanitize ( c. locked . id . clone ( ) ) )
106
+ . collect ( ) ;
107
+
108
+ for sanitized_component_id in sanitized_component_ids {
109
+ if let Some ( stdout_log_path) = sanitized_log_path (
110
+ self . log_dir . as_deref ( ) ,
111
+ & sanitized_component_id,
112
+ STDOUT_LOG_FILE_SUFFIX ,
113
+ ) {
114
+ let _ = std:: fs:: File :: create ( stdout_log_path) ;
115
+ }
116
+
117
+ if let Some ( stderr_log_path) = sanitized_log_path (
118
+ self . log_dir . as_deref ( ) ,
119
+ & sanitized_component_id,
120
+ STDERR_LOG_FILE_SUFFIX ,
121
+ ) {
122
+ let _ = std:: fs:: File :: create ( stderr_log_path) ;
123
+ }
124
+ }
125
+ }
97
126
}
98
127
99
128
#[ async_trait]
@@ -104,43 +133,15 @@ impl<F: RuntimeFactors, U> ExecutorHooks<F, U> for StdioLoggingExecutorHooks {
104
133
) -> anyhow:: Result < ( ) > {
105
134
self . validate_follows ( configured_app. app ( ) ) ?;
106
135
107
- if self . refresh_log {
108
- let sanitized_component_ids: Vec < String > = configured_app
109
- . app ( )
110
- . components ( )
111
- . map ( |c| sanitize_filename:: sanitize ( c. locked . id . clone ( ) ) )
112
- . collect ( ) ;
113
-
114
- for sanitized_component_id in sanitized_component_ids {
115
- if let Some ( stdout_log_path) = santized_log_path (
116
- self . log_dir . as_deref ( ) ,
117
- & sanitized_component_id,
118
- STDOUT_LOG_FILE_SUFFIX ,
119
- ) {
120
- if let Err ( err) = std:: fs:: File :: create ( stdout_log_path) {
121
- //TODO: figure out if we want to return error here
122
- tracing:: warn!( "Failed to refresh stdout log file: {}" , err) ;
123
- }
124
- }
125
-
126
- if let Some ( stderr_log_path) = santized_log_path (
127
- self . log_dir . as_deref ( ) ,
128
- & sanitized_component_id,
129
- STDERR_LOG_FILE_SUFFIX ,
130
- ) {
131
- if let Err ( err) = std:: fs:: File :: create ( stderr_log_path) {
132
- //TODO: figure out if we want to return error here
133
- tracing:: warn!( "Failed to refresh stderr log file: {}" , err) ;
134
- }
135
- }
136
- }
137
- }
138
-
139
136
if let Some ( dir) = & self . log_dir {
140
137
// Ensure log dir exists if set
141
138
std:: fs:: create_dir_all ( dir)
142
139
. with_context ( || format ! ( "Failed to create log dir {}" , quoted_path( dir) ) ) ?;
143
140
141
+ if self . truncate_log {
142
+ self . truncate_log_files ( configured_app) ;
143
+ }
144
+
144
145
println ! ( "Logging component stdio to {}" , quoted_path( dir. join( "" ) ) )
145
146
}
146
147
Ok ( ( ) )
@@ -370,7 +371,7 @@ fn bullet_list<S: std::fmt::Display>(items: impl IntoIterator<Item = S>) -> Stri
370
371
. join ( "\n " )
371
372
}
372
373
373
- fn santized_log_path (
374
+ fn sanitized_log_path (
374
375
log_dir : Option < & Path > ,
375
376
sanitized_component_id : & str ,
376
377
log_suffix : & str ,
0 commit comments