zng_wgt_inspector/
crash_handler.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#![cfg(all(
    feature = "crash_handler",
    not(any(target_arch = "wasm32", target_os = "android", target_os = "ios"))
))]

//! Debug crash handler.

use std::path::PathBuf;
use zng_app::crash_handler::*;
use zng_ext_config::CONFIG;
use zng_ext_l10n::l10n;
use zng_ext_window::{StartPosition, WindowRoot, WINDOWS};
use zng_wgt::prelude::*;
use zng_wgt::{align, corner_radius, enabled, margin};
use zng_wgt_ansi_text::AnsiText;
use zng_wgt_button::Button;
use zng_wgt_container::{padding, Container};
use zng_wgt_dialog::{FileDialogFilters, FileDialogResponse, DIALOG};
use zng_wgt_fill::background_color;
use zng_wgt_scroll::Scroll;
use zng_wgt_stack::Stack;
use zng_wgt_stack::StackDirection;
use zng_wgt_style::style_fn;
use zng_wgt_style::Style;
use zng_wgt_text::Text;
use zng_wgt_text_input::selectable::SelectableText;
use zng_wgt_toggle::{self as toggle, Toggle};
use zng_wgt_tooltip::{tooltip, Tip};
use zng_wgt_window::Window;
use zng_wgt_wrap::Wrap;

// l10n-## Debug Crash Handler

/// Debug dialog window.
///
/// Used by `zng::app::init_debug`.
pub fn debug_dialog(args: CrashArgs) -> WindowRoot {
    let error = args.latest();
    Window! {
        title = l10n!("crash-handler/window.title", "{$app} - App Crashed", app=zng_env::about().app.clone());
        start_position = StartPosition::CenterMonitor;
        color_scheme = ColorScheme::Dark;

        on_load = hn_once!(|_| {
            // force to foreground
            let _ = WINDOWS.focus(WINDOW.id());
        });
        on_close = hn_once!(args, |_| {
            args.exit(0);
        });

        padding = 5;
        child_top = header(error), 5;
        child = panels(error);
        child_bottom = commands(args), 5;
    }
}

fn header(error: &CrashError) -> impl UiNode {
    SelectableText! {
        txt = error.message();
        margin = 10;
    }
}

fn panels(error: &CrashError) -> impl UiNode {
    let mut options = vec![ErrorPanel::Summary];
    let mut active = ErrorPanel::Summary;

    if !error.stdout.is_empty() {
        options.push(ErrorPanel::StdoutPlain);
        active = ErrorPanel::StdoutPlain;
        if !error.is_stdout_plain() {
            options.push(ErrorPanel::Stdout);
            active = ErrorPanel::Stdout;
        }
    }

    if !error.stderr.is_empty() {
        options.push(ErrorPanel::StderrPlain);
        active = ErrorPanel::StderrPlain;
        if error.is_stderr_plain() {
            options.push(ErrorPanel::Stderr);
            active = ErrorPanel::Stderr;
        }
    }

    if error.has_panic() {
        options.push(ErrorPanel::Panic);
        active = ErrorPanel::Panic;
    }
    if error.has_panic_widget() {
        options.push(ErrorPanel::Widget);
    }
    if error.minidump.is_some() {
        options.push(ErrorPanel::Minidump);
        active = ErrorPanel::Minidump;
    }

    let active = var(active);

    Container! {
        child_top = Wrap! {
            toggle::selector = toggle::Selector::single(active.clone());
            children = options.iter().map(|p| Toggle! {
                child = Text!(p.title());
                value = *p;
            }).collect::<UiVec>();
            toggle::style_fn = Style! {
                padding = (2, 4);
                corner_radius = 2;
            };
            spacing = 5;
        }, 5;
        child = presenter(active, wgt_fn!(error, |p: ErrorPanel| p.panel(&error)));
    }
}

// l10n-## Panels

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ErrorPanel {
    Summary,
    Stdout,
    Stderr,
    StdoutPlain,
    StderrPlain,
    Panic,
    Widget,
    Minidump,
}
impl ErrorPanel {
    fn title(&self) -> Txt {
        match self {
            ErrorPanel::Summary => l10n!("crash-handler/summary.title", "Summary").get(),
            ErrorPanel::Stdout => l10n!("crash-handler/stdout.title", "Stdout").get(),
            ErrorPanel::Stderr => l10n!("crash-handler/stderr.title", "Stderr").get(),
            ErrorPanel::StdoutPlain => l10n!("crash-handler/stdout.title-plain", "Stdout (plain)").get(),
            ErrorPanel::StderrPlain => l10n!("crash-handler/stderr.title-plain", "Stderr (plain)").get(),
            ErrorPanel::Panic => l10n!("crash-handler/panic.title", "Panic").get(),
            ErrorPanel::Widget => l10n!("crash-handler/widget.title", "Widget").get(),
            ErrorPanel::Minidump => l10n!("crash-handler/minidump.title", "Minidump").get(),
        }
    }

    fn panel(&self, error: &CrashError) -> BoxedUiNode {
        match self {
            ErrorPanel::Summary => summary_panel(error).boxed(),
            ErrorPanel::Stdout => std_panel(error.stdout.clone(), "stdout").boxed(),
            ErrorPanel::Stderr => std_panel(error.stderr.clone(), "stderr").boxed(),
            ErrorPanel::StdoutPlain => std_plain_panel(error.stdout_plain(), "stdout").boxed(),
            ErrorPanel::StderrPlain => std_plain_panel(error.stderr_plain(), "stderr").boxed(),
            ErrorPanel::Panic => panic_panel(error.find_panic().unwrap()).boxed(),
            ErrorPanel::Widget => widget_panel(error.find_panic().unwrap().widget_path).boxed(),
            ErrorPanel::Minidump => minidump_panel(error.minidump.clone().unwrap()).boxed(),
        }
    }
}

fn summary_panel(error: &CrashError) -> impl UiNode {
    let s = l10n!(
        "crash-handler/summary.text",
        "Timestamp: {$timestamp}
Exit Code: {$exit_code}
Signal: {$signal}
Stderr: {$stderr_len} bytes
Stdout: {$stdout_len} bytes
Panic: {$is_panic}
Minidump: {$minidump_path}

Args: {$args}
OS: {$os}
",
        timestamp = error.unix_time(),
        exit_code = match error.code {
            Some(c) => format!("{c:#x}"),
            None => String::new(),
        },
        signal = match error.signal {
            Some(c) => format!("{c}"),
            None => String::new(),
        },
        stderr_len = error.stderr.len(),
        stdout_len = error.stdout.len(),
        is_panic = error.find_panic().is_some(),
        minidump_path = match &error.minidump {
            Some(p) => {
                let path = p.display().to_string();
                let path = path.trim_start_matches(r"\\?\");
                path.to_owned()
            }
            None => "none".to_owned(),
        },
        args = format!("{:?}", error.args),
        os = error.os.clone(),
    );
    plain_panel(s.get(), "summary")
}

fn std_panel(std: Txt, config_key: &'static str) -> impl UiNode {
    Scroll! {
        child_align = Align::TOP_START;
        background_color = colors::BLACK;
        padding = 5;
        horizontal_offset = CONFIG.get(formatx!("{config_key}.scroll.h"), 0.fct());
        vertical_offset = CONFIG.get(formatx!("{config_key}.scroll.v"), 0.fct());
        child = AnsiText! {
            txt = std;
            font_size = 0.9.em();
        }
    }
}
fn std_plain_panel(std: Txt, config_key: &'static str) -> impl UiNode {
    plain_panel(std, config_key)
}
fn panic_panel(panic: CrashPanic) -> impl UiNode {
    plain_panel(panic.to_txt(), "panic")
}
fn widget_panel(widget_path: Txt) -> impl UiNode {
    plain_panel(widget_path, "widget")
}
fn minidump_panel(path: PathBuf) -> impl UiNode {
    let path_str = path.display().to_string();
    #[cfg(windows)]
    let path_str = path_str.trim_start_matches(r"\\?\").replace('/', "\\");
    let path_txt = path_str.to_txt();
    Scroll! {
        child_align = Align::TOP_START;
        background_color = colors::BLACK;
        padding = 5;
        horizontal_offset = CONFIG.get(formatx!("minidump.scroll.h"), 0.fct());
        vertical_offset = CONFIG.get(formatx!("minidump.scroll.v"), 0.fct());
        child = Stack! {
            direction = StackDirection::top_to_bottom();
            spacing = 5;
            children = ui_vec![
                SelectableText! {
                    txt = path_txt;
                    font_size = 0.9.em();
                    // same as AnsiText
                    font_family = ["JetBrains Mono", "Consolas", "monospace"];
                },
                Stack! {
                    direction = StackDirection::top_to_bottom();
                    zng_wgt_button::style_fn = style_fn!(|_| zng_wgt_button::LinkStyle!());
                    children = ui_vec![
                        {
                            let enabled = var(true);
                            Button! {
                                child = Text!("Open Minidump");
                                on_click = async_hn!(enabled, path, |_| {
                                    open_path(enabled, path).await;
                                });
                                enabled;
                            }
                        },
                        {
                            let enabled = var(true);
                            Button! {
                                child = Text!("Open Minidump Dir");
                                on_click = async_hn!(enabled, path, |_| {
                                    open_path(enabled, path.parent().unwrap().to_owned()).await;
                                });
                            }
                        },
                        {
                            let enabled = var(true);
                            Button! {
                                child = Text!("Save Minidump");
                                tooltip = Tip!(Text!("Save copy of the minidump"));
                                on_click = async_hn!(enabled, path, |_| {
                                    save_copy(enabled, path).await;
                                });
                            }
                        },
                        {
                            let enabled = var(true);
                            Button! {
                                child = Text!("Delete Minidump");
                                on_click = async_hn!(enabled, path, |_| {
                                    remove_path(enabled, path).await;
                                });
                            }
                        },
                    ]
                }
            ]
        }
    }
}
async fn open_path(enabled: ArcVar<bool>, path: PathBuf) {
    enabled.set(false);

    #[cfg(windows)]
    let path = path.display().to_string().replace('/', "\\");

    if let Err(e) = task::wait(move || open::that_detached(path)).await {
        DIALOG
            .error(
                "",
                l10n!(
                    "crash-handler/minidump.open-error",
                    "Failed to open minidump.\n{$error}",
                    error = e.to_string()
                ),
            )
            .wait_done()
            .await;
    }

    enabled.set(true);
}
async fn save_copy(enabled: ArcVar<bool>, path: PathBuf) {
    enabled.set(false);

    let mut filters = FileDialogFilters::new();
    if let Some(ext) = path.extension() {
        // l10n-# name for the minidump file type in the save file dialog
        filters.push_filter(
            l10n!("crash-handler/minidump.save-copy-filter-name", "Minidump").get().as_str(),
            &[ext.to_string_lossy()],
        );
    }

    let r = DIALOG
        .save_file(
            l10n!("crash-handler/minidump.save-copy-title", "Save Copy"),
            path.parent().unwrap().to_owned(),
            // l10n-# default file name
            l10n!("crash-handler/minidump.save-copy-starting-name", "minidump"),
            filters,
        )
        .wait_into_rsp()
        .await;

    match r {
        FileDialogResponse::Selected(mut paths) => {
            let destiny = paths.remove(0);
            if let Err(e) = task::wait(move || std::fs::copy(path, destiny)).await {
                DIALOG
                    .error(
                        "",
                        l10n!(
                            "crash-handler/minidump.save-error",
                            "Failed so save minidump copy.\n{$error}",
                            error = format!("[copy] {e}"),
                        ),
                    )
                    .wait_done()
                    .await;
            }
        }
        FileDialogResponse::Cancel => {}
        FileDialogResponse::Error(e) => {
            DIALOG
                .error(
                    "",
                    l10n!(
                        "crash-handler/minidump.save-error",
                        "Failed so save minidump copy.\n{$error}",
                        error = format!("[dialog] {e}"),
                    ),
                )
                .wait_done()
                .await
        }
    }

    enabled.set(true);
}
async fn remove_path(enabled: ArcVar<bool>, path: PathBuf) {
    enabled.set(false);

    if let Err(e) = task::wait(move || std::fs::remove_file(path)).await {
        if e.kind() != std::io::ErrorKind::NotFound {
            DIALOG
                .error(
                    "",
                    l10n!(
                        "crash-handler/minidump.remove-error",
                        "Failed to remove minidump.\n{$error}",
                        error = e.to_string()
                    ),
                )
                .wait_into_rsp()
                .await
        }
    }

    enabled.set(true);
}

fn plain_panel(txt: Txt, config_key: &'static str) -> impl UiNode {
    Scroll! {
        child_align = Align::TOP_START;
        background_color = colors::BLACK;
        padding = 5;
        horizontal_offset = CONFIG.get(formatx!("{config_key}.scroll.h"), 0.fct());
        vertical_offset = CONFIG.get(formatx!("{config_key}.scroll.v"), 0.fct());
        child = SelectableText! {
            txt;
            font_size = 0.9.em();
            // same as AnsiText
            font_family = ["JetBrains Mono", "Consolas", "monospace"];
        }
    }
}

fn commands(args: CrashArgs) -> impl UiNode {
    Stack! {
        spacing = 5;
        direction = StackDirection::start_to_end();
        align = Align::END;
        children = ui_vec![
            Button! {
                child = Text!("Restart App");
                on_click = hn_once!(args, |_| {
                    args.restart();
                });
            },
            Button! {
                child = Text!("Exit App");
                on_click = hn_once!(|_| {
                    args.exit(0);
                });
            }
        ];
    }
}