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
#![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:
 * - catch signals and generate C/C++ backtraces;
 * - 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::*;

#[cfg(feature = "sentry")]
use hyper_rustls::ConfigBuilderExt;

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>)  + Sync + Send>,
    get_breadcrumbs: fn () -> Vec<Breadcrumb>,

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

    command: String,
    path: PathBuf,

    report_username: bool,
}

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>)  + Sync + Send> = Arc::new(|_format, data: Vec<u8>| {
            std::io::stdout().write_all(&data).unwrap();
        });
        #[allow(unused_mut)]
        let mut format = SendFormat::PlainText;
        #[cfg(feature = "sentry")]
        if let Some(dsn) = buildinfy::build_sentry_dsn() {
            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 |_, body: Vec<u8>| {
                                let url = format!("https://{host}/api/{project}/store/");
                                let mut client_req = hyper::Request::builder()
                                    .method("POST")
                                    .uri(url);
                                let headers = client_req.headers_mut().unwrap();
                                headers.insert(hyper::header::HeaderName::from_lowercase(b"x-sentry-auth").unwrap(), hyper::header::HeaderValue::from_str(&format!("Sentry sentry_version=7, sentry_client=indigo, sentry_key={key}")).unwrap());
                                let client_req = client_req
                                    .body(hyper::Body::from(body.to_vec()))
                                    .expect("request builder");
                                let client = {
                                    let tls = tokio_rustls::rustls::ClientConfig::builder()
                                        .with_safe_defaults()
                                        .with_native_roots()
                                        .with_no_client_auth();
                                    let https = hyper_rustls::HttpsConnectorBuilder::new()
                                        .with_tls_config(tls)
                                        .https_or_http()
                                        .enable_http1()
                                        .build();
                                    hyper::Client::builder()
                                        .http1_preserve_header_case(true)
                                        .http1_title_case_headers(true)
                                        .build::<_, hyper::Body>(https)
                                };
                                let rt = tokio::runtime::Builder::new_multi_thread()
                                    .enable_all()
                                    .worker_threads(1)
                                    .build()
                                    .expect("build runtime");

                                rt.block_on(async move {
                                    let resp = client.request(client_req).await;
                                    eprintln!("sent crashy error report to sentry: {}", if resp.is_ok() { "ok" } else { "failed" });
                                    if resp.is_err() {
                                        eprintln!("{:?}", resp);
                                    }
                                });
                            });
                    }
                }
            }
        }
        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,
        }
    }
}

pub struct LatestLogs {
    pub 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()) {
            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.target(), record.args(), file, lineno).unwrap();
                }
            }
            if line.is_empty() {
                write!(&mut line, "{}: {}", record.target(), record.args()).unwrap();
            }
            eprintln!("{}", line);
            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 {
    counter: Arc<Mutex<u32>>,
}
impl Drop for CrashHandler {
    fn drop(&mut self) {
        while *self.counter.lock().unwrap() > 0 {
            std::thread::sleep(std::time::Duration::from_millis(100));
        }
    }
}

pub fn setup_crashy() -> CrashHandler {
    setup_crashy_with_options(Box::default())
}
pub fn setup_crashy_with_options(options: Box<CrashOptions>) -> CrashHandler {
    let (logger, latest) = Logger::new();
    // FIXME: set level
    log::set_boxed_logger(Box::new(logger))
        .map(|()| log::set_max_level(log::LevelFilter::Info)).unwrap();

    let counter = Arc::new(Mutex::new(0));

    let _retval = latest.clone();
    let counter_copy = counter.clone();
    std::panic::set_hook(Box::new(move |pi: &core::panic::PanicInfo| {
        //println!("backtrace: {:?}", Backtrace::new());
        let mut started = false;
        let mut frames = Vec::new();
        backtrace::trace(|frame| {
            let mut display = true;
            backtrace::resolve_frame(frame, |symbol| {
                let mut n = String::from("-");
                let mut f = String::from("");
                let mut l = 0;
                if let Some(name) = symbol.name() {
                    if name.as_bytes() == b"rust_begin_unwind" {
                        started = true;
                        display = false;
                    }
                    if name.as_bytes().starts_with(b"_ZN4core9panicking9panic_fmt17") {
                        display = false;
                    }
                    // if name.as_bytes().contains(b"__rust_begin_short_backtrace") {
                    //     started = false;
                    // }
                    n = format!("{}", name);
                }
                if let Some(filename) = symbol.filename() {
                    f = filename.display().to_string();
                }
                if let Some(line_no) = symbol.lineno() {
                    l = line_no;
                }
                if started && display {
                    frames.push((n, f, l));
                }
            });

            true // keep going to the next frame
        });
        let mut msg = String::new();
        if let Some(s) = pi.payload().downcast_ref::<&str>() {
            write!(&mut msg, "{}", s).unwrap();
        } else if let Some(s) = pi.payload().downcast_ref::<String>() {
            write!(&mut msg, "{}", s).unwrap();
        } else {
            write!(&mut msg, "{}", pi).unwrap();
        }
        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 let Some(s) = pi.payload().downcast_ref::<&str>() {
                write!(&mut out, "panic {s:?} occurred").unwrap();
            } else if let Some(s) = pi.payload().downcast_ref::<String>() {
                write!(&mut out, "panic {s:?} occurred").unwrap();
            } else {
                write!(&mut out, "panic occurred").unwrap();
            }
            if let Some(location) = pi.location() {
                writeln!(&mut out, " in file '{}' at line {}",
                    location.file(),
                    location.line(),
                ).unwrap();
            } else {
                writeln!(&mut out).unwrap();
            }
            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();
                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 = latest.lock().unwrap();
            for (message, timestamp, level) in &locker.latest {
                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 = NaiveDateTime::from_timestamp_opt(*timestamp as i64, ((*timestamp % 1.0) * 1_000_000_000.0) as u32);
                if let Some(t) = t {
                    let t = Utc.from_utc_datetime(&t);
                    let ms = ((*timestamp % 1.0) * 1_000.0) as u32;
                    writeln!(&mut 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!(&mut out, "{TERM_BRIGHT_BLUE}<!>{TERM_RESET} xx-xx-xx xx:xx:xx.xxx {}{} {}{TERM_RESET}", color, level, message).unwrap();
                }
            }
        } 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": "panic""#).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();
                        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 = latest.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 = options.format;
        let sender = options.sender.clone();
        *counter_copy.lock().unwrap() += 1;
        let counter_copy = counter_copy.clone();
        std::thread::spawn(move || {
            (sender)(format, out.into_bytes());
            *counter_copy.lock().unwrap() -= 1;
        });
    }));
    CrashHandler{counter}
}

#[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());
    }

    fn bar() {
        info!("bar()");
        error!("bar error");
        let _t = trace("bar");
        foo();
    }

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