crashy 0.3.2

crash reporting with nicer stack traces and information about the current process, with optional Sentry integration, with optional Sentry integration, with optional Sentry integration, with optional Sentry integration
Documentation
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#![allow(dead_code)]

use std::ffi::CStr;
use std::sync::{Arc, Mutex};
/**
 * Compile hint:
 * remap paths in stack trace:
 * env RUSTFLAGS=--remap-path-prefix=`pwd`/= cargo test crashy::
 *
 * Logging, crash reporting, tracing, and metrics are important QA methods. This crate supports resiliant
 * operations, which are especially important for logging, tracing and crash reporting. This is done by
 * starting a seperate process that is doing the uploads of crash reports, tracing, and logging.
 *
 * Supported platforms: Unix (because pipe communication channel is needed). Graceful degradation
 * otherwise.
 *
 * Possible future extensions:
 * - sending logging to remote servers;
 * - integrate logging with tasks of executors (so only the logs of the current tasks are displayed);
 */
use std::{io::Write, path::PathBuf};
use std::fmt::{Write as WriteFmt, Display};

use rand::Rng;
use chrono::prelude::*;

use buildinfy::*;

pub use log;

mod termcolors;
use crate::termcolors::*;

#[cfg(feature = "trace")]
pub use tracing_opentelemetry::OpenTelemetrySpanExt;
#[cfg(feature = "trace")]
pub use opentelemetry::trace::TraceContextExt;

const LOGGER_DEFAULT_LATEST_CAPACITY: usize = 128;

#[derive(PartialEq, Eq, Copy, Clone)]
pub enum SendFormat {
    None,
    PlainText,
    JsonSentry,
}

pub struct Breadcrumb {

}

pub struct CrashOptions {
    pub format: SendFormat,
    pub sender: Arc<dyn Fn (SendFormat, Vec<u8>, usize)  + Sync + Send>,
    get_breadcrumbs: fn () -> Vec<Breadcrumb>,

    release: String,
    dist: String,
    environment: String,

    command: String,
    path: PathBuf,

    pub report_username: bool,

    pub level: log::LevelFilter,
}

#[cfg(feature = "sentry")]
mod sentry {
    pub use httpclienty::*;
}
#[cfg(feature = "sentry")]
use sentry::*;

impl Default for CrashOptions {
    fn default() -> Self {
        let mut release : String = String::new();
        if let (Some(revision),Some(pipeline_id)) = (build_revision(), build_pipeline_id()) {
            release = format!("{}-{}", revision, pipeline_id);
        }
        #[allow(unused_mut)]
        let mut sender : Arc<dyn Fn (SendFormat, Vec<u8>, usize)  + Sync + Send> = Arc::new(|_format, data: Vec<u8>, header_len: usize| {
            std::io::stderr().write_all(&data[header_len..]).unwrap();
        });
        #[allow(unused_mut)]
        let mut format = SendFormat::PlainText;
        #[cfg(any(feature = "nacl", feature = "sentry"))]
        if let Some(dsn) = buildinfy::build_sentry_dsn() {
            #[cfg(feature = "sentry")]
            if let Some(current) = dsn.strip_prefix("https://") {
                if let Some((key, current)) = current.split_once('@') {
                    if let Some((host, project)) = current.split_once('/') {
                            format = SendFormat::JsonSentry;
                            sender = Arc::new(move |_format: SendFormat, body: Vec<u8>, _header_len: usize| {
                                let url = format!("https://{host}/api/{project}/store/");
                                let mut client_req = httpclienty::Request::builder()
                                    .method("POST")
                                    .uri(url);
                                let headers = client_req.headers_mut().unwrap();
                                headers.insert(httpclienty::header::HeaderName::from_lowercase(b"x-sentry-auth").unwrap(), httpclienty::header::HeaderValue::from_str(&format!("Sentry sentry_version=7, sentry_client=indigo, sentry_key={key}")).unwrap());
                                let client_req = client_req
                                    .body(Full::new(body.into()))
                                    .expect("request builder");

                                let f = async move {
                                    let state = get_http_client_state();
                                    let resp = perform_http_request(client_req, state, None).await;
                                    match resp {
                                        Ok(resp) => {
                                            log::error!("sent crashy error report to sentry: {} ({})", if resp.status() == 200 { "ok" } else { "failed" }, resp.status());
                                        },
                                        Err(err) => {
                                            log::error!("error sending crashy error report to sentry: {:?}", err);
                                        },
                                    }
                                };

                                // error to reuse the same runtime as the caller
                                // error to make new runtime in another one
                                // so reuse if exists, otherwise make
                                if let Ok(rt) = tokio::runtime::Handle::try_current() {
                                    rt.spawn(f);
                                } else {
                                    let rt = tokio::runtime::Builder::new_current_thread()
                                        .enable_all()
                                        .build()
                                        .expect("build runtime");
                                    rt.block_on(f);
                                }
                            });
                    }
                }
            }
            #[cfg(feature = "nacl")]
            if let Some(current) = dsn.strip_prefix("nacl://") {
                if let Some((key, current)) = current.split_once('@') {
                    if let Some((host, project)) = current.split_once('/') {
                            format = SendFormat::JsonSentry;
                            sender = Arc::new(move |_format: SendFormat, body: Vec<u8>, _header_len: usize| {
                                // let url = format!("https://{host}/api/{project}/store/");
                                // let mut client_req = httpclienty::Request::builder()
                                //     .method("POST")
                                //     .uri(url);
                                // let headers = client_req.headers_mut().unwrap();
                                // headers.insert(httpclienty::header::HeaderName::from_lowercase(b"x-sentry-auth").unwrap(), httpclienty::header::HeaderValue::from_str(&format!("Sentry sentry_version=7, sentry_client=indigo, sentry_key={key}")).unwrap());
                                // let client_req = client_req
                                //     .body(Full::new(body.into()))
                                //     .expect("request builder");

                                let host = host.to_string();
                                let port = 443;

                                let f = async move {
                                    if let Ok(Ok(stream)) = tokio::time::timeout(std::time::Duration::from_secs(2), tokio::net::TcpStream::connect((host.as_str(),port))).await {
                                        stream.set_nodelay(true).unwrap();
                                        use networky::*;
                                        pub const PUBLIC_KEY : &str = "554c3d0282c39f3a777248b0dacc0052a92615e77e30ac4b37862397f9e48e08";
                                        let data = from_hex_slice(PUBLIC_KEY.as_bytes())
                                            .map_err(|_| "public key was not hex").unwrap()
                                            .try_into()
                                            .map_err(|_| "public key was incorrect size").unwrap();
                                        let pk = vec![PublicSignKey{data}];
                                        match networky::progress(connection(stream, "crashy", 0, &host, &pk, body), std::time::Duration::from_secs(60)).await {
                                            Ok(_) => {},
                                            Err(_) => {},
                                        }
                                    }
                                };

                                // error to reuse the same runtime as the caller
                                // error to make new runtime in another one
                                // so reuse if exists, otherwise make
                                if let Ok(rt) = tokio::runtime::Handle::try_current() {
                                    rt.spawn(f);
                                } else {
                                    let rt = tokio::runtime::Builder::new_current_thread()
                                        .enable_all()
                                        .build()
                                        .expect("build runtime");
                                    rt.block_on(f);
                                }
                            });
                    }
                }
            }
        }
        Self {
            format,
            sender,
            get_breadcrumbs: || {
                Vec::new()
            },
            release,
            dist: build_pipeline_id_per_project().unwrap_or_default().to_string(),
            environment: build_reference().unwrap_or_default().to_string(),
            command: std::env::args().reduce(|x,y| format!("{} {}", x, y)).unwrap_or_default(),
            path: std::env::current_dir().unwrap_or_default(),
            report_username: false,
            level: log::LevelFilter::Info,
        }
    }
}
#[cfg(feature = "nacl")]
pub fn from_hex_slice(s: &[u8]) -> Result<Vec<u8>,networky::EncodingError> {
    use networky::*;
    (0..s.len())
        .step_by(2)
        .map(|i| -> Result<u8,EncodingError> {
            Ok((from_hex_u8(s[i])? << 4) | from_hex_u8(s[i+1])?)
        })
    .collect::<Result<Vec<u8>,EncodingError>>()
}
#[cfg(feature = "nacl")]
async fn connection(mut stream: tokio::net::TcpStream, description: &str, version: u32, host: &String, public_sign_key_of_server: &[networky::nacl::PublicSignKey], mut body: Vec<u8>) -> Result<bool,String> {
    use networky::nacl::*;
    use tokio::io::AsyncWriteExt as _;
    use tokio::io::AsyncReadExt as _;
    // generate new session keys for forward secrecy
    let (pk, sk) = crypto_box_keypair(&mut rand::rngs::OsRng);
    let mut output = RawString::with_capacity(4 + 1 + host.len() + pk.data.len());
    output.write_u64(version as u64, 4).unwrap_infallible(); // version
    output.write_var_bytes(host.as_bytes()).unwrap_infallible();
    output.write_var_bytes(description.as_bytes()).unwrap_infallible();
    output.write_bytes(&pk.data).unwrap_infallible();
    let output = output.build();
    stream.write_all(&output).await.map_err(|_| "WriteError".to_string())?;
    let mut signature = Signature { data: [0u8; networky::nacl::bindings::crypto_sign_ed25519_BYTES as usize] };
    stream.read_exact(&mut signature.data).await.map_err(|_| "ReadError".to_string())?;
    let mut public_key_of_session = PublicBoxKey{data: [0u8; 32]};
    stream.read_exact(&mut public_key_of_session.data).await.map_err(|_| "ReadError".to_string())?;

    if !public_sign_key_of_server.iter().any(|key| networky::nacl::crypto_sign_verify(&signature, &public_key_of_session.data, key).is_ok()) {
        return Err("CertificateError".to_string());
    }
    let mut send_nonce = nonce_for_client();
    let send_cache = crypto_box_prepare(&sk, &public_key_of_session);

    send(&mut body, &mut send_nonce, &send_cache, true, &mut stream).await.map_err(|_| "WriteError".to_string())?;
    let mut buffer = Vec::new();
    stream.read_to_end(&mut buffer).await.map_err(|_| "WriteError".to_string())?;
    Ok(true)
}

#[cfg(feature = "nacl")]
pub async fn send<Out: tokio::io::AsyncWrite + std::marker::Unpin>(data: &mut [u8], send_nonce: &mut networky::Nonce, send_cache: &networky::CryptoBoxCache, server: bool, stream: &mut Out) -> Result<(),std::io::Error> {
    use networky::nacl::*;
    use tokio::io::AsyncWriteExt as _;
    let tag = crypto_box_in_place(data, send_nonce, send_cache);
    let mut header = [0u8; 9 + bindings::crypto_box_MACBYTES as usize];
    let mut header_writer = RawWriter::with(&mut header);
    header_writer.write_var_u64((tag.data.len() + data.len()) as u64).unwrap();
    header_writer.write_bytes(&tag.data).unwrap();
    let header = header_writer.build();
    stream.write_all(header).await?;
    stream.write_all(data).await?;
    if server {
        decrease_nonce(&mut send_nonce.data);
    } else {
        increase_nonce(&mut send_nonce.data);
    }
    Ok(())
}

#[derive(Default)]
struct LatestLogs {
    latest: Vec<(String,f64,log::Level)>,
}

struct Logger {
    latest: Arc<Mutex<LatestLogs>>,
}

impl log::Log for Logger {
    fn enabled(&self, metadata: &log::Metadata) -> bool {
        metadata.level() <= log::Level::Info
    }

    fn log(&self, record: &log::Record) {
        if !self.enabled(record.metadata()) {
            return;
        }
        let mut line = String::new();
        let time = std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH).unwrap().as_secs_f64();
        if let Some(file) = record.file() {
            if let Some(lineno) = record.line() {
                write!(&mut line, "{}: {} [{}:{}]", record.module_path().unwrap_or_else(|| record.target()), record.args(), file, lineno).unwrap();
            }
        }
        if line.is_empty() {
            write!(&mut line, "{}: {}", record.module_path().unwrap_or_else(|| record.target()), record.args()).unwrap();
        }
        if self.enabled(record.metadata()) {
            let mut output = String::new();
            print_log_to_console(&line, time, record.level(), &mut output);
            let _ = std::io::stderr().write(output.as_bytes());
        }
        let mut locker = self.latest.lock().unwrap();
        if locker.latest.len() == locker.latest.capacity() {
            locker.latest.remove(0);
        }
        locker.latest.push((line,time,record.level()));
    }

    fn flush(&self) {}
}

impl Logger {
    fn new() -> (Self, Arc<Mutex<LatestLogs>>) {
        let latest = Arc::new(Mutex::new(LatestLogs{
            latest: Vec::with_capacity(LOGGER_DEFAULT_LATEST_CAPACITY),
        }));
        (Self {
            latest: latest.clone(),
        }, latest)
    }
}

fn json_escaped_write(f: &mut core::fmt::Formatter<'_>, src: &str) -> Result<(),core::fmt::Error> {
    let mut utf16_buf = [0u16; 2];
    for c in src.chars() {
        match c {
            '\x08' => write!(f, "\\b")?,
            '\x0c' => write!(f, "\\f")?,
            '\n' => write!(f, "\\n")?,
            '\r' => write!(f, "\\r")?,
            '\t' => write!(f, "\\t")?,
            '"' => write!(f, "\\\"")?,
            '\\' => write!(f, "\\\\")?,
            ' ' => write!(f, " ")?,
            c if (c as u32) < 0x20 => {
                let encoded = c.encode_utf16(&mut utf16_buf);
                for utf16 in encoded {
                    write!(f, "\\u{:04X}", utf16)?;
                }
            },
            c => write!(f, "{}", c)?,
        }
    }
    Ok(())
}

struct Quoted<'a>(&'a str);

impl<'a> Display for Quoted<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, r#"""#)?;
        json_escaped_write(f, self.0)?;
        write!(f, r#"""#)?;
        Ok(())
    }
}

pub struct CrashHandler {
}

impl Drop for CrashHandler {
    fn drop(&mut self) {
        CRASH_STATE.get_or_init(Mutex::default).lock().unwrap().waiter = false;
        while CRASH_STATE.get_or_init(Mutex::default).lock().unwrap().counter > 0 {
            std::thread::sleep(std::time::Duration::from_millis(100));
        }
    }
}

#[must_use = "store result in a variable to wait for all crash reports to be send"]
pub fn setup_crashy() -> CrashHandler {
    setup_crashy_with_options(CrashOptions::default())
}

#[derive(Default)]
struct CrashState {
    options: CrashOptions,
    counter: usize,
    logger: Arc<Mutex<LatestLogs>>,
    waiter: bool,
}

static CRASH_STATE : std::sync::OnceLock<Mutex<CrashState>> = std::sync::OnceLock::new();

#[must_use = "store result in a variable to wait for all crash reports to be send"]
pub fn setup_crashy_with_options(options: CrashOptions) -> CrashHandler {
    let (logger, latest) = Logger::new();

    log::set_boxed_logger(Box::new(logger)).expect("crashy: could not set logger");
    log::set_max_level(options.level);

    {
        let mut crash_state = CRASH_STATE.get_or_init(Mutex::default).lock().unwrap();
        crash_state.options = options;
        crash_state.logger = latest;
    }

    unsafe {
        // install signal handlers
        // overview for windows: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/signal?view=msvc-170
        libc::signal(libc::SIGABRT, signal_handler as libc::sighandler_t);
        libc::signal(libc::SIGSEGV, signal_handler as libc::sighandler_t);
        #[cfg(target_family = "unix")]
        libc::signal(libc::SIGBUS, signal_handler as libc::sighandler_t);
        libc::signal(libc::SIGILL, signal_handler as libc::sighandler_t);
        libc::signal(libc::SIGFPE, signal_handler as libc::sighandler_t);

    }
    std::panic::set_hook(Box::new(|pi| {
        let mut msg = String::new();
        write!(&mut msg, "panic occured: {}", pi).unwrap();
        let mut type_of_error = "".to_string();
        if let Some(location) = pi.location() {
            write!(&mut msg, " in file '{}' at line {}",
                   location.file(),
                   location.line(),
                   ).unwrap();
            type_of_error = format!("panic-{}-{}-{}", location.file(), location.line(), location.column());
        }
        let crash_waiter_already_dropped = CRASH_STATE.get_or_init(Mutex::default).lock().unwrap().waiter;
        // crash waiter can already be dropped, e.g. if there is a panic in the main method. If so,
        // don't spawn a different thread.
        handle_crash(&msg, crash_waiter_already_dropped, &type_of_error, "panic", crash_waiter_already_dropped);
    }));
    CrashHandler{}
}

#[cfg(windows)]
fn strsignal(signal: std::ffi::c_int) -> &'static CStr {
    let name = match signal {
        libc::SIGABRT => "SIGABRT",
        libc::SIGSEGV => "SIGSEGV",
        libc::SIGILL => "SIGILL",
        libc::SIGFPE => "SIGFPE",
        _ => "unknown",
    };
    unsafe { CStr::from_bytes_with_nul_unchecked(name.as_bytes()) }
}

#[cfg(not(windows))]
fn strsignal(signal: std::ffi::c_int) -> &'static CStr {
    unsafe { CStr::from_ptr(libc::strsignal(signal)) }
}

/// # Safety
///
/// Following C-spec.
pub unsafe extern "C" fn signal_handler(signal: std::ffi::c_int) {
    let name = strsignal(signal);
    let name = name.to_str().unwrap_or("unknown");
    let msg = format!("signal {}: {}", signal, name);
    handle_crash(&msg, false, name, "signal", false);
}

// type of error is e.g. 'signal', error is e.g. 'Aborted'
pub fn handle_crash(msg: &str, handled: bool, error: &str, type_of_error: &str, send_in_new_thread: bool) {
    // Always print some error, in case this handler panics.
    eprintln!("{TERM_BRIGHT_RED}=========={TERM_RESET} CRASH {TERM_BRIGHT_RED}=========={TERM_RESET} [{}]", Utc::now());
    if !msg.is_empty() {
        eprintln!("{}", msg);
    }
    eprintln!("{TERM_BRIGHT_CYAN}{TERM_RESET}{}: {}", type_of_error, error);
    let mut frames = Vec::new();
    let mut display = false;
    let full = std::env::var("RUST_BACKTRACE").as_ref().map(String::as_str) == Ok("full");
    backtrace::trace(|frame| {
        backtrace::resolve_frame(frame, |symbol| {
            let mut n = String::from("-");
            let mut f = String::from("");
            let mut l = 0;
            let mut skip = false;
            if let Some(name) = symbol.name() {
                n = format!("{}", name);
                if n == "rust_begin_unwind" || n.starts_with("crashy::signal_handler") || n.starts_with("core::panicking::") || n.starts_with("std::panicking::") || n.starts_with("std::sys_common::backtrace::__rust_end_short_backtrace") {
                    skip = true;
                    display = true;
                }
                if n.starts_with("std::rt::") || n.starts_with("tokio::") || n.starts_with("<tokio::") || n.starts_with("std::thread::local::") || n.starts_with("<core::panic::unwind_safe::") || n.starts_with("<core::pin::Pin<P> as core::future::") || n.starts_with("std::panic::") || n == "__rust_try" || n.starts_with("core::ops::function::FnOnce") {
                    skip = true;
                }
                if n.starts_with("std::sys_common::backtrace::__rust_begin_short_backtrace::") {
                    skip = true;
                    display = false;
                }
            }
            if let Some(filename) = symbol.filename() {
                f = filename.display().to_string();
            }
            if let Some(line_no) = symbol.lineno() {
                l = line_no;
            }
            if (!skip || full) && display {
                frames.push((n, f, l));
            }
        });

        // keep going to the next frame, except if the current function is 'main'
        !matches!(frames.last().map(|(s,_,_)| s.as_str()), Some("main"))
    });
    let mut header_len = 0;
    let (format, sender, out) = {
        let mut crash_state = CRASH_STATE.get_or_init(Mutex::default).lock().unwrap();
        let options = &crash_state.options;
        let mut out = String::new();
        if options.format == SendFormat::PlainText {
            writeln!(&mut out, "{TERM_BRIGHT_RED}=========={TERM_RESET} CRASH {TERM_BRIGHT_RED}=========={TERM_RESET} [{}]", Utc::now()).unwrap();
            if !msg.is_empty() {
                writeln!(&mut out, "{}", msg).unwrap();
            }
            writeln!(&mut out, "{TERM_BRIGHT_CYAN}{TERM_RESET}{}: {}", type_of_error, error).unwrap();
            header_len = out.len();
            for (name, filename, line_no) in frames {
                writeln!(&mut out, "{TERM_BRIGHT_YELLOW}~~> {TERM_BOLD}{TERM_BRIGHT_WHITE}{}{TERM_RESET}", name).unwrap();
                writeln!(&mut out, "    {TERM_BRIGHT_BLACK}[{}:{}]{TERM_RESET}", filename, line_no).unwrap();
            }
            // FIXME: only display font awesome if some environmnet variable is set
            #[cfg(feature = "trace")]
            {
                let span = tracing::span::Span::current();
                let span = span.context();
                let span = span.span();
                let span = span.span_context();
                let trace_id = span.trace_id();
                let span_id = span.span_id();
                if trace_id != opentelemetry::trace::TraceId::INVALID {
                    writeln!(&mut out, "{TERM_BRIGHT_RED}{TERM_BOLD}{TERM_BRIGHT_WHITE}{:x} / {:x}{TERM_RESET}", trace_id, span_id).unwrap();
                }
            }
            writeln!(&mut out, "{TERM_BRIGHT_RED}{TERM_BOLD}{TERM_BRIGHT_WHITE}{}{TERM_RESET}", options.command).unwrap();
            #[cfg(unix)]
            // contexts
            unsafe {
                let mut version = std::mem::zeroed::<libc::utsname>();
                libc::uname(&mut version);
                // FIXME: add "model" (by using GetMachineModel() from the C++ version)
                writeln!(&mut out, "{TERM_BRIGHT_RED}{TERM_BOLD}{TERM_BRIGHT_WHITE}{} / {} / {} / {}{TERM_RESET}", CStr::from_ptr(&version.sysname as *const std::os::raw::c_char).to_string_lossy(), CStr::from_ptr(&version.release as *const std::os::raw::c_char).to_string_lossy(), CStr::from_ptr(&version.nodename as *const std::os::raw::c_char).to_string_lossy(), CStr::from_ptr(&version.machine as *const std::os::raw::c_char).to_string_lossy()).unwrap();
            }
            let locker = crash_state.logger.lock().unwrap();
            for (message, timestamp, level) in &locker.latest {
                print_log_to_console(message, *timestamp, *level, &mut out);
            }
        } else if options.format == SendFormat::JsonSentry {
            // see https://develop.sentry.dev/sdk/event-payloads/
            let id : u128 = rand::rngs::OsRng.gen::<u128>();
            let time = std::time::SystemTime::now().duration_since(std::time::SystemTime::UNIX_EPOCH).unwrap().as_secs();
            write!(&mut out, r"{{").unwrap();
            write!(&mut out, r#""event_id": "{:032x}""#, id).unwrap();
            write!(&mut out, r#","timestamp": {}"#, time).unwrap();
            write!(&mut out, r#","platform": "rust""#).unwrap();
            write!(&mut out, r#","logger": "indigo_crashy""#).unwrap();
            if !options.release.is_empty() {
                write!(&mut out, r#","release": {}"#, Quoted(&options.release)).unwrap();
            }
            if !options.dist.is_empty() {
                write!(&mut out, r#","dist": {}"#, Quoted(&options.dist)).unwrap();
            }
            if !options.environment.is_empty() {
                write!(&mut out, r#","environment": {}"#, Quoted(&options.environment)).unwrap();
            }
            write!(&mut out, r#","level": "fatal""#).unwrap();

            // FIXME: add 'tags' (with path and commandline)
            // FIXME: add 'thread_id' (with task name)
            // FIXME: add 'breadcrumbs' (with latest logs)
            
            // 'exception' (with actual problem)
            write!(&mut out, r#","exception": {{"values":[{{"#).unwrap();
            write!(&mut out, r#""type": {}"#, Quoted(error)).unwrap();
            write!(&mut out, r#","mechanism": {{ "type": {}, "handled": {} }}"#, Quoted(type_of_error), handled).unwrap();
            write!(&mut out, r#","value": {}"#, Quoted(msg)).unwrap();
            write!(&mut out, r#","stacktrace": {{"frames":["#).unwrap();
            let mut sep = "";
            for (name, filename, line_no) in frames.iter().rev() {
                write!(&mut out, r#"{}{{"function": {}, "filename": {}, "lineno": {}}}"#, sep, Quoted(name), Quoted(filename), line_no).unwrap();
                sep = ",";
            }
            write!(&mut out, r#"]}}"#).unwrap(); // end stacktrace

            write!(&mut out, r#"}}]}}"#).unwrap(); // end exception

            #[cfg(unix)]
            {
                // contexts
                let mut version;
                unsafe {
                    version = std::mem::zeroed::<libc::utsname>();
                    libc::uname(&mut version);
                }
                write!(&mut out, r#","contexts": {{"#).unwrap();
                {
                    write!(&mut out, r#""os": {{"#).unwrap();
                    unsafe {
                        write!(&mut out, r#""name": {}"#, Quoted(&CStr::from_ptr(&version.sysname as *const std::os::raw::c_char).to_string_lossy())).unwrap();
                        write!(&mut out, r#","version": {}"#, Quoted(&CStr::from_ptr(&version.release as *const std::os::raw::c_char).to_string_lossy())).unwrap();
                    }
                    write!(&mut out, r#"}}"#).unwrap();
                    write!(&mut out, r#","device": {{"#).unwrap();
                    unsafe {
                        write!(&mut out, r#""name": {}"#, Quoted(&CStr::from_ptr(&version.nodename as *const std::os::raw::c_char).to_string_lossy())).unwrap();
                        // FIXME: add "model" (by using GetMachineModel() from the C++ version)
                        write!(&mut out, r#","arch": {}"#, Quoted(&CStr::from_ptr(&version.machine as *const std::os::raw::c_char).to_string_lossy())).unwrap();
                    }
                    write!(&mut out, r#"}}"#).unwrap();
                    #[cfg(feature = "trace")]
                    {
                        let span = tracing::span::Span::current();
                        let span = span.context();
                        let span = span.span();
                        let span = span.span_context();
                        let trace_id = span.trace_id();
                        let span_id = span.span_id();
                        if trace_id != opentelemetry::trace::TraceId::INVALID {
                            write!(&mut out, r#","trace": {{"trace_id":"{}","span_id":"{}"}}"#, trace_id, span_id).unwrap();
                        }
                    }
                }
                write!(&mut out, r#"}}"#).unwrap();
                unsafe {
                    write!(&mut out, r#","server_name": {}"#, Quoted(&CStr::from_ptr(&version.nodename as *const std::os::raw::c_char).to_string_lossy())).unwrap();
                }
            }

            write!(&mut out, r#","breadcrumbs": {{"values":["#).unwrap();
            let mut sep = "";
            let locker = crash_state.logger.lock().unwrap();
            for (message, timestamp, level) in &locker.latest {
                let level = match level {
                    log::Level::Error => "error",
                    log::Level::Warn => "warning",
                    log::Level::Info => "info",
                    log::Level::Debug => "debug",
                    log::Level::Trace => "verbose",
                };
                write!(&mut out, r#"{}{{"message": {}, "timestamp": {}, "level": {}}}"#, sep, Quoted(message), timestamp, Quoted(level)).unwrap();
                sep = ",";
            }
            write!(&mut out, r#"]}}"#).unwrap();


            write!(&mut out, r#"}}"#).unwrap();
        }
        let format = crash_state.options.format;
        let sender = crash_state.options.sender.clone();
        crash_state.counter += 1;
        (format, sender, out)
    };
    if send_in_new_thread {
        std::thread::spawn(move || {
            (sender)(format, out.into_bytes(), header_len);
            let mut crash_state = CRASH_STATE.get_or_init(Mutex::default).lock().unwrap();
            crash_state.counter -= 1;
        });
    } else {
        // After signal handler exits, the program will stop. Therefore do send action inline. Same
        // for main panic handler.
        (sender)(format, out.into_bytes(), header_len);
    }
}

fn print_log_to_console(message: &str, timestamp: f64, level: log::Level, out: &mut String) {
    let color = match level {
        log::Level::Error => TERM_DIM_RED,
        log::Level::Warn  => TERM_DIM_YELLOW,
        log::Level::Info  => TERM_DIM_GREEN,
        log::Level::Debug => TERM_DIM_CYAN,
        log::Level::Trace => TERM_DIM_MAGENTA,
    };
    let level = match level {
        log::Level::Error => "[ERROR]",
        log::Level::Warn  => " [WARN]",
        log::Level::Info  => " [INFO]",
        log::Level::Debug => "[DEBUG]",
        log::Level::Trace => " [VERB]",
    };
    let t = DateTime::from_timestamp(timestamp as i64, ((timestamp % 1.0) * 1_000_000_000.0) as u32);
    if let Some(t) = t {
        let ms = ((timestamp % 1.0) * 1_000.0) as u32;
        writeln!(out, "{TERM_BRIGHT_BLUE}<!>{TERM_RESET} {:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:03} {}{} {}{TERM_RESET}", t.year(), t.month(), t.day(), t.hour(), t.minute(), t.second(), ms, color, level, message).unwrap();
    } else {
        writeln!(out, "{TERM_BRIGHT_BLUE}<!>{TERM_RESET} xx-xx-xx xx:xx:xx.xxx {}{} {}{TERM_RESET}", color, level, message).unwrap();
    }
}

#[cfg(test)]
mod crashy {
    use rand::{rngs::OsRng, RngCore};

    use crate::*;
    use log::*;

    use std::panic::Location;

    static mut SPAN_ID : Option<u64> = None;
    static mut TRACE_ID : u64 = 0;

    #[derive(Debug)]
    struct Span {
        trace_id: u64,
        span_id: u64,
        parent_id: Option<u64>,
        description: &'static str,
        start: std::time::Instant,
    }
    impl Drop for Span {
        fn drop(&mut self) {
            let now = std::time::Instant::now();
            eprintln!("trace: {:?} -> {:?}", self, now - self.start);
            // FIXME: queue binary representation to some global queue that is written once per
            // eventloop iteration to some buffer. Possible the external crash reporter?
            unsafe {
                if let Some(current_span_id) = SPAN_ID {
                    if current_span_id == self.span_id {
                        SPAN_ID = self.parent_id;
                    }
                }
            }
        }
    }

    fn trace(description: &'static str) -> Span {
        let trace_id;
        let span_id;
        let mut parent_id = None;
        unsafe {
            if SPAN_ID.is_some() {
                parent_id = SPAN_ID;
            } else {
                TRACE_ID = OsRng.next_u64();
            }
            trace_id = TRACE_ID;
            // FIXME: probably a very expensive random, so change in real implementation
            span_id = OsRng.next_u64();
            SPAN_ID = Some(span_id);
        }
        Span {
            trace_id,
            span_id,
            parent_id,
            description,
            start: std::time::Instant::now(),
        }
    }

    #[track_caller]
    fn get_caller_location() -> &'static Location<'static> {
        Location::caller()
    }

    fn foo() {
        warn!("foo()");
        let _t = trace("foo");
        println!("{:?}", get_caller_location());
        panic!("foo panic");
    }

    fn bar() {
        info!("bar()");
        error!("bar error");
        let _t = trace("bar");
        foo();
    }
    #[test] #[ignore]
    fn unix_signal() {
        let _ = setup_crashy();
        info!("just before abort");
        unsafe { libc::abort(); }
    }

    #[test] #[ignore]
    fn it_works() {
        let crash_options = CrashOptions{ format: SendFormat::PlainText, ..CrashOptions::default() };
        let _ = setup_crashy_with_options(crash_options);
        bar();
        bar();
        let result = 2 + 2;
        panic!("oh oh {}", result);
    }
}