1
+ use crate :: cli:: diag:: logger:: DualLogger ;
1
2
use crate :: command:: Command ;
2
- use crate :: error;
3
- use crate :: info;
4
3
use crate :: log:: MaybeFancy ;
5
4
use crate :: log:: Spinner ;
6
- use crate :: warning;
7
5
use anyhow:: Context ;
8
6
use camino:: Utf8Path ;
9
7
use camino:: Utf8PathBuf ;
@@ -39,44 +37,57 @@ impl Command for DiagCollectCommand {
39
37
}
40
38
41
39
async fn execute ( & self , _: TEdgeConfig ) -> Result < ( ) , MaybeFancy < anyhow:: Error > > {
42
- let plugins = self . read_diag_plugins ( ) . await ?;
40
+ file:: create_directory_with_defaults ( & self . diag_dir )
41
+ . await
42
+ . with_context ( || format ! ( "failed to create directory at {}" , self . diag_dir) ) ?;
43
+ let mut logger = DualLogger :: new ( self . diag_dir . join ( "summary.log" ) )
44
+ . context ( "Failed to initialize logging" ) ?;
45
+
46
+ let plugins = self . read_diag_plugins ( & mut logger) . await ?;
43
47
let plugin_count = plugins. len ( ) ;
44
48
if plugin_count == 0 {
45
- error ! ( "No diagnostic plugins were found" ) ;
49
+ logger . error ( "No diagnostic plugins were found" ) ;
46
50
std:: process:: exit ( 2 )
47
51
}
48
52
49
- file:: create_directory_with_defaults ( & self . diag_dir )
50
- . await
51
- . with_context ( || format ! ( "failed to create directory at {}" , self . diag_dir) ) ?;
52
-
53
53
let mut skipped_count = 0 ;
54
54
let mut error_count = 0 ;
55
55
56
56
for plugin in plugins {
57
57
let banner = format ! ( "Executing {plugin}" ) ;
58
- let spinner = Spinner :: start ( banner) ;
58
+ let spinner = Spinner :: start ( banner. clone ( ) ) ;
59
59
let res = self . execute_diag_plugin ( & plugin) . await ;
60
60
61
61
match spinner. finish ( res) {
62
- Ok ( exit_status) if exit_status. success ( ) => { }
63
- Ok ( exit_status) if exit_status. code ( ) == Some ( 2 ) => {
64
- skipped_count += 1 ;
65
- info ! ( "{plugin} is marked skipped" ) ;
66
- }
67
62
Ok ( exit_status) => {
68
- error_count += 1 ;
69
- error ! ( "{plugin} failed with exit status: {exit_status}" ) ;
63
+ logger. only_to_file ( & format ! ( "{banner}... ✓" ) ) ;
64
+
65
+ match exit_status. code ( ) {
66
+ Some ( 0 ) => { }
67
+ Some ( 2 ) => {
68
+ skipped_count += 1 ;
69
+ logger. info ( & format ! ( "{plugin} is marked skipped" ) ) ;
70
+ }
71
+ Some ( code) => {
72
+ error_count += 1 ;
73
+ logger. error ( & format ! ( "{plugin} failed with exit status: {code}" ) ) ;
74
+ }
75
+ None => {
76
+ error_count += 1 ;
77
+ logger. error ( & format ! ( "{plugin} terminated by signal" ) ) ;
78
+ }
79
+ }
70
80
}
71
81
Err ( err) => {
72
82
error_count += 1 ;
73
- error ! ( "{plugin} failed with error: {err}" ) ;
83
+ logger. only_to_file ( & format ! ( "{banner}... ✗" ) ) ;
84
+ logger. error ( & format ! ( "{plugin} failed with error: {err}" ) ) ;
74
85
}
75
86
}
76
87
}
77
88
78
89
let success_count = plugin_count - skipped_count - error_count;
79
- eprintln ! ( "\n Total {plugin_count} executed: {success_count} completed, {error_count} failed, {skipped_count} skipped" ) ;
90
+ logger . log ( & format ! ( "\n Total {plugin_count} executed: {success_count} completed, {error_count} failed, {skipped_count} skipped" ) ) ;
80
91
81
92
self . compress_into_a_tarball ( )
82
93
. with_context ( || "Failed to compress diagnostic information" ) ?;
@@ -96,15 +107,21 @@ impl Command for DiagCollectCommand {
96
107
}
97
108
98
109
impl DiagCollectCommand {
99
- async fn read_diag_plugins ( & self ) -> Result < BTreeSet < Utf8PathBuf > , anyhow:: Error > {
110
+ async fn read_diag_plugins (
111
+ & self ,
112
+ logger : & mut DualLogger ,
113
+ ) -> Result < BTreeSet < Utf8PathBuf > , anyhow:: Error > {
100
114
let mut plugins = BTreeSet :: new ( ) ;
101
115
for dir_path in & self . plugin_dir {
102
- match Self :: read_diag_plugins_from_dir ( dir_path. as_ref ( ) ) . await {
116
+ match self
117
+ . read_diag_plugins_from_dir ( logger, dir_path. as_ref ( ) )
118
+ . await
119
+ {
103
120
Ok ( plugin_files) => {
104
121
plugins. extend ( plugin_files) ;
105
122
}
106
123
Err ( err) => {
107
- warning ! ( "Failed to read plugins from {dir_path}: {err}" ) ;
124
+ logger . warning ( & format ! ( "Failed to read plugins from {dir_path}: {err}" ) ) ;
108
125
continue ;
109
126
}
110
127
}
@@ -113,6 +130,8 @@ impl DiagCollectCommand {
113
130
}
114
131
115
132
async fn read_diag_plugins_from_dir (
133
+ & self ,
134
+ logger : & mut DualLogger ,
116
135
dir_path : & Utf8Path ,
117
136
) -> Result < BTreeSet < Utf8PathBuf > , anyhow:: Error > {
118
137
let mut plugins = BTreeSet :: new ( ) ;
@@ -126,10 +145,10 @@ impl DiagCollectCommand {
126
145
plugins. insert ( path) ;
127
146
continue ;
128
147
} else {
129
- warning ! ( "Skipping non-executable file: {:?}" , entry. path( ) ) ;
148
+ logger . warning ( & format ! ( "Skipping non-executable file: {:?}" , entry. path( ) ) ) ;
130
149
}
131
150
} else {
132
- warning ! ( "Ignoring invalid path: {:?}" , entry. path( ) ) ;
151
+ logger . warning ( & format ! ( "Ignoring invalid path: {:?}" , entry. path( ) ) ) ;
133
152
}
134
153
}
135
154
Ok ( plugins)
@@ -180,6 +199,7 @@ impl DiagCollectCommand {
180
199
tar. append_dir_all ( & self . tarball_name , & self . diag_dir ) ?;
181
200
tar. finish ( ) ?;
182
201
202
+ // Cannot write this message to summary.log since the tarball has already been created
183
203
eprintln ! ( "Diagnostic information saved to {tarball_path}" ) ;
184
204
Ok ( tarball_path)
185
205
}
@@ -206,7 +226,8 @@ mod tests {
206
226
with_exec_permission ( command. first_plugin_dir ( ) . join ( "plugin_b" ) , "pwd" ) ;
207
227
with_exec_permission ( command. first_plugin_dir ( ) . join ( "plugin_c" ) , "pwd" ) ;
208
228
209
- let plugins = command. read_diag_plugins ( ) . await . unwrap ( ) ;
229
+ let mut logger = DualLogger :: new ( command. diag_dir . join ( "summary.log" ) ) . unwrap ( ) ;
230
+ let plugins = command. read_diag_plugins ( & mut logger) . await . unwrap ( ) ;
210
231
assert_eq ! ( plugins. len( ) , 3 ) ;
211
232
}
212
233
@@ -225,7 +246,8 @@ mod tests {
225
246
with_exec_permission ( third_plugin_dir. utf8_path ( ) . join ( "plugin_3a" ) , "pwd" ) ;
226
247
with_exec_permission ( third_plugin_dir. utf8_path ( ) . join ( "plugin_3b" ) , "pwd" ) ;
227
248
228
- let plugins = command. read_diag_plugins ( ) . await . unwrap ( ) ;
249
+ let mut logger = DualLogger :: new ( command. diag_dir . join ( "summary.log" ) ) . unwrap ( ) ;
250
+ let plugins = command. read_diag_plugins ( & mut logger) . await . unwrap ( ) ;
229
251
assert_eq ! ( plugins. len( ) , 6 ) ;
230
252
}
231
253
@@ -238,7 +260,8 @@ mod tests {
238
260
let mut command = DiagCollectCommand :: new ( & ttd) ;
239
261
command. add_plugin_dir ( & second_plugin_dir) ;
240
262
241
- let plugins = command. read_diag_plugins ( ) . await . unwrap ( ) ;
263
+ let mut logger = DualLogger :: new ( command. diag_dir . join ( "summary.log" ) ) . unwrap ( ) ;
264
+ let plugins = command. read_diag_plugins ( & mut logger) . await . unwrap ( ) ;
242
265
assert_eq ! ( plugins. len( ) , 0 ) ;
243
266
}
244
267
@@ -257,7 +280,8 @@ mod tests {
257
280
"pwd" ,
258
281
) ;
259
282
260
- let plugins = command. read_diag_plugins ( ) . await . unwrap ( ) ;
283
+ let mut logger = DualLogger :: new ( command. diag_dir . join ( "summary.log" ) ) . unwrap ( ) ;
284
+ let plugins = command. read_diag_plugins ( & mut logger) . await . unwrap ( ) ;
261
285
assert_eq ! ( plugins. len( ) , 0 ) ;
262
286
}
263
287
@@ -268,7 +292,8 @@ mod tests {
268
292
ttd. dir ( "plugins" ) . file ( "plugin_a.ignore" ) ;
269
293
ttd. dir ( "plugins" ) . file ( "plugin_b.ignore" ) ;
270
294
271
- let plugins = command. read_diag_plugins ( ) . await . unwrap ( ) ;
295
+ let mut logger = DualLogger :: new ( command. diag_dir . join ( "summary.log" ) ) . unwrap ( ) ;
296
+ let plugins = command. read_diag_plugins ( & mut logger) . await . unwrap ( ) ;
272
297
assert_eq ! ( plugins. len( ) , 0 ) ;
273
298
}
274
299
0 commit comments