radicle_ci_broker/
logger.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
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
//! A logger abstraction for the CI broker.

#[cfg(test)]
use std::sync::Once;
use std::{
    fmt,
    io::Write,
    path::Path,
    process::ExitStatus,
    sync::{Arc, Mutex},
    time::Duration,
};

use clap::ValueEnum;
use radicle::{git::raw::Oid, identity::RepoId, node::Event};
use slog::{debug, error, info, o, trace, warn, Drain};
use slog_scope::GlobalLoggerGuard;

use crate::{
    ci_event::CiEvent,
    ci_event_source::CiEventSource,
    config::Config,
    db::{QueueId, QueuedCiEvent},
    msg::Request,
    node_event_source::NodeEventSource,
    run::Run,
};

// We define our own type for log levels so that we can apply
// clap::ValueEnum on it.
#[derive(ValueEnum, Eq, PartialEq, Copy, Clone, Debug)]
pub enum LogLevel {
    Trace,
    Debug,
    Info,
    Warning,
    Error,
    Critical,
}

impl fmt::Display for LogLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "<{:?}>", self)
    }
}

impl From<LogLevel> for slog::Level {
    fn from(log_level: LogLevel) -> Self {
        match log_level {
            LogLevel::Trace => slog::Level::Trace,
            LogLevel::Debug => slog::Level::Debug,
            LogLevel::Info => slog::Level::Info,
            LogLevel::Warning => slog::Level::Warning,
            LogLevel::Error => slog::Level::Error,
            LogLevel::Critical => slog::Level::Critical,
        }
    }
}

// A custom type for log output. We need this to hide log messages
// from output in successful tests. Cargo will hide standard output
// and error by default, which is what we want, but it seems this only
// applies to output via the `print!` family of macros. If we open a
// new handle for stdout or stderr, output via that handle is not
// captured by Cargo.
//
// Instead, we have our own log writer type that writes to stderr in
// production mode, and uses `print!` in tests.
#[derive(Default)]
struct LogWriter {}

#[cfg(test)]
impl Write for LogWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let s = String::from_utf8_lossy(buf);
        print!("{s}");
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

#[cfg(not(test))]
impl Write for LogWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let mut stderr = std::io::stderr();
        stderr.write_all(buf)?;
        stderr.flush()?;
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

pub struct Logger {
    minimum_log_level: Arc<Mutex<slog::Level>>,

    // We need to keep this for as long as need to log. But we don't
    // refer to it directly.
    #[allow(dead_code)]
    guard: GlobalLoggerGuard,
}

impl Logger {
    pub fn set_minimum_level(&self, level: LogLevel) {
        *self
            .minimum_log_level
            .lock()
            .expect("set minimum log level") = level.into();
    }
}

pub fn open() -> Logger {
    let underlying_logger = slog_json::Json::new(LogWriter::default())
        .add_default_keys()
        .set_flush(true)
        .set_newlines(true)
        .build();

    // Set the default log level. Trace is good for tests.
    let level = Arc::new(Mutex::new(LogLevel::Trace.into()));

    let filter = LogLevelFilter::new(underlying_logger, level.clone());
    let log = slog::Logger::root(Mutex::new(filter).fuse(), o!());
    let guard = slog_scope::set_global_logger(log);

    Logger {
        guard,
        minimum_log_level: level,
    }
}

// Set up structured logging for tests.
//
// We have to keep the GlobalLoggerGuard we get when we initialize
// `slog-scope` alive as long as we may need to do any logging. We can
// only create that once per process. For tests, we do that here.
//
// We can't do the same thing for non-test processes, as that would
// interfere with use of `radicle-ci-broker` as a library. Libraries
// should not interfere with global state, unless they're specifically
// intended to do that.
//
// This is for tests only: otherwise the default global logger is
// used, and that's OK for a long-running process.

// We use this to make sure we initialize the logger only once.
#[cfg(test)]
static INIT: Once = Once::new();

// This is the actual logger for tests.
#[cfg(test)]
static mut LOGGER: Option<Logger> = None;

struct LogLevelFilter<D> {
    drain: D,
    minimum_log_level: Arc<Mutex<slog::Level>>,
}

impl<D> LogLevelFilter<D> {
    pub fn new(drain: D, minimum_log_level: Arc<Mutex<slog::Level>>) -> Self {
        Self {
            drain,
            minimum_log_level,
        }
    }
}

impl<D> Drain for LogLevelFilter<D>
where
    D: Drain,
{
    type Ok = Option<D::Ok>;
    type Err = Option<D::Err>;

    fn log(
        &self,
        record: &slog::Record,
        values: &slog::OwnedKVList,
    ) -> Result<Self::Ok, Self::Err> {
        #[allow(clippy::unwrap_used)] // We have no good way to report an error
        let min = *self
            .minimum_log_level
            .lock()
            .expect("lock log level filter minimum level");
        if record.level().is_at_least(min) {
            self.drain.log(record, values).map(Some).map_err(Some)
        } else {
            Ok(None)
        }
    }
}

#[cfg(test)]
#[ctor::ctor]
fn open_for_tests() {
    INIT.call_once(|| unsafe {
        LOGGER = Some(open());
    });
}

pub fn start_cib() {
    info!(slog_scope::logger(), "CI broker starts"; "version" => env!("GIT_HEAD"));
}

pub fn end_cib_successfully() {
    info!(slog_scope::logger(), "CI broker ends successfully");
}

pub fn end_cib_in_error() {
    error!(
        slog_scope::logger(),
        "CI broker ends in unrecoverable error"
    );
}

pub fn node_event_source_created(source: &NodeEventSource) {
    info!(
        slog_scope::logger(),
        "created node event source";
        "source" => format!("{source:#?}")
    );
}

pub fn node_event_source_got_event(event: &Event) {
    info!(
        slog_scope::logger(),
        "node event source received event";
        "node_event" => format!("{event:#?}")
    );
}

pub fn node_event_source_eof(source: &NodeEventSource) {
    info!(
        slog_scope::logger(),
        "node event source end of file on control socket";
        "node_event_source" => format!("{source:#?}")
    );
}

pub fn ci_event_source_created(source: &CiEventSource) {
    info!(
        slog_scope::logger(),
        "created CI event source";
        "source" => format!("{source:#?}")
    );
}

pub fn ci_event_source_got_events(events: &[CiEvent]) {
    info!(
        slog_scope::logger(),
        "CI event source received events";
        "ci_events" => format!("{events:#?}")
    );
}

pub fn ci_event_source_disconnected() {
    info!(
        slog_scope::logger(),
        "CI event source received disconnection"
    );
}

pub fn ci_event_source_end() {
    info!(
        slog_scope::logger(),
        "CI event source was notified end of events"
    );
}

pub fn ci_event_source_eof(source: &CiEventSource) {
    info!(
        slog_scope::logger(),
        "CI event source end of file";
        "ci_event_source" => format!("{source:#?}")
    );
}

pub fn loaded_config(config: &Config) {
    debug!(slog_scope::logger(), "loaded configuration {config:#?}");
}

pub fn adapter_config(config: &Config) {
    debug!(slog_scope::logger(), "adapter configuration {config:#?}");
}

pub fn queueproc_start() {
    info!(
        slog_scope::logger(),
        "start thread to process events until a shutdown event"
    );
}

pub fn queueproc_end() {
    info!(slog_scope::logger(), "thread to process events ends");
}

pub fn queueproc_channel_disconnect() {
    info!(
        slog_scope::logger(),
        "event notification channel disconnected"
    );
}

pub fn queueproc_queue_length(len: usize) {
    trace!(
        slog_scope::logger(),
        "event queue length"; "length" => len);
}

pub fn queueproc_picked_event(id: &QueueId, event: &QueuedCiEvent) {
    info!(
        slog_scope::logger(),
        "picked event from queue: {id}: {event:#?}"
    );
}

pub fn queueproc_remove_event(id: &QueueId) {
    info!(slog_scope::logger(), "remove event from queue: {id}");
}

pub fn queueproc_action_run(rid: &RepoId, oid: &Oid) {
    info!(slog_scope::logger(), "Action: run: {rid} {oid}");
}

pub fn queueproc_action_shutdown() {
    info!(slog_scope::logger(), "Action: shutdown");
}

pub fn queueadd_start() {
    info!(
        slog_scope::logger(),
        "start thread to add events from node to event queue"
    );
}

pub fn queueadd_control_socket_close() {
    info!(
        slog_scope::logger(),
        "no more events from node control socket"
    );
}

pub fn queueadd_push_event(e: &CiEvent) {
    debug!(
        slog_scope::logger(),
        "insert broker event into queue: {e:?}"
    );
}

pub fn queueadd_end() {
    info!(slog_scope::logger(), "thread to process events ends");
}

pub fn pages_directory_unset() {
    warn!(
        slog_scope::logger(),
        "not writing HTML report pages as output directory has not been set"
    );
}

pub fn pages_interval(interval: Duration) {
    info!(
        slog_scope::logger(),
        "wait about {} seconds to update HTML report pages again",
        interval.as_secs()
    );
}

pub fn pages_disconnected() {
    info!(
        slog_scope::logger(),
        "page updater: run notification channel disconnected"
    );
}

pub fn pages_start() {
    info!(slog_scope::logger(), "start page updater thread");
}

pub fn pages_end() {
    info!(slog_scope::logger(), "end page updater thread");
}

pub fn event_disconnected() {
    info!(
        slog_scope::logger(),
        "connection to node control socket broke"
    );
}

pub fn event_end() {
    info!(
        slog_scope::logger(),
        "no more node events from control socket: iterator ended"
    );
}

pub fn broker_db(filename: &Path) {
    info!(
        slog_scope::logger(),
        "broker database: {}",
        filename.display()
    );
}

pub fn broker_start_run(trigger: &Request) {
    info!(slog_scope::logger(), "start CI run");
    debug!(slog_scope::logger(), "trigger event: {trigger:#?}");
}

pub fn broker_end_run(run: &Run) {
    info!(slog_scope::logger(), "Finish CI run");
    debug!(slog_scope::logger(), "finished CI run: {run:#?}");
}

pub fn adapter_no_first_response() {
    error!(slog_scope::logger(), "no first response message");
}

pub fn adapter_no_second_response() {
    error!(slog_scope::logger(), "no second response message");
}

pub fn adapter_too_many_responses() {
    error!(slog_scope::logger(), "too many response messages");
}

pub fn adapter_stderr_line(line: &str) {
    debug!(slog_scope::logger(), "adapter stderr"; "stderr" => line);
}

pub fn adapter_result(exit: i32) {
    debug!(slog_scope::logger(), "adapter exit code"; "exit_code" => exit);
}

pub fn adapter_did_not_exit_voluntarily() {
    warn!(
        slog_scope::logger(),
        "adapter did not exit voluntarily: terminated for taking too long"
    );
}

pub fn adapter_did_not_exit() {
    warn!(
        slog_scope::logger(),
        "adapter did not exit: probably killed by signal"
    );
}

pub fn timeoutcmd_request_termination(result: Result<(), std::sync::mpsc::SendError<()>>) {
    trace!(
        slog_scope::logger(),
        "request termination of child process: {result:?}"
    );
}

pub fn timeoutcmd_wait_word_from_nanny() {
    trace!(slog_scope::logger(), "wait: wait for word from nanny");
}

pub fn timeoutcmd_wait_got_word_from_nanny() {
    trace!(slog_scope::logger(), "got word from nanny");
}

pub fn timeoutcmd_wait_on_nanny_to_end() {
    trace!(slog_scope::logger(), "wait: wait on nanny thread to end");
}

pub fn timeoutcmd_wait_on_stdin_writer_to_end() {
    trace!(
        slog_scope::logger(),
        "wait: wait for stdin writer to terminate"
    );
}

pub fn timeoutcmd_wait_on_stdout_reader_to_end() {
    trace!(
        slog_scope::logger(),
        "wait: wait for stdout reader to terminate"
    );
}

pub fn timeoutcmd_wait_on_stderr_reader_to_end() {
    trace!(
        slog_scope::logger(),
        "wait: wait for stderr reader to terminate"
    );
}

pub fn timeoutcmd_wait_on_child_to_end() {
    trace!(slog_scope::logger(), "wait: wait for child to terminate");
}

pub fn timeoutcmd_wait_status(status: ExitStatus) {
    trace!(slog_scope::logger(), "wait: wait status: {status:?}");
}

pub fn timeoutcmd_ok() {
    trace!(slog_scope::logger(), "wait: return Ok result");
}

pub fn timeoutcmd_nanny_start() {
    trace!(slog_scope::logger(), "nanny: start monitoring child");
}

pub fn timeoutcmd_nanny_terminated_as_requested(result: Result<(), std::io::Error>) {
    trace!(
        slog_scope::logger(),
        "nanny: terminated child by request: {result:?}"
    );
}

pub fn timeoutcmd_nanny_too_long(
    id: u32,
    elapsed: Duration,
    max: Duration,
    result: Result<(), std::io::Error>,
) {
    trace!(
        slog_scope::logger(),
        "nanny: child {} has run for too long ({} ms > {} ms); terminated it: {:?}",
        id,
        elapsed.as_millis(),
        max.as_millis(),
        result,
    );
}

pub fn timeoutcmd_nanny_child_died() {
    trace!(slog_scope::logger(), "nanny: child has terminated");
}

pub fn timeoutcmd_nanny_time_to_end() {
    trace!(
        slog_scope::logger(),
        "nanny: tell other threads it's time to end"
    );
}

pub fn timeoutcmd_nanny_ends() {
    trace!(slog_scope::logger(), "nanny: ends");
}

pub fn timeoutcmd_line_reader_try_byte(name: &'static str) {
    trace!(
        slog_scope::logger(),
        "line receiver {}: try to receive next byte",
        name
    );
}

pub fn timeoutcmd_line_reader_tried_byte(
    name: &'static str,
    result: Result<u8, std::sync::mpsc::TryRecvError>,
) {
    trace!(
        slog_scope::logger(),
        "line receiver {}: tried to read line: {:?}",
        name,
        result
    );
}

pub fn timeoutcmd_line_reader_got_line(name: &'static str, line: &str) {
    trace!(
        slog_scope::logger(),
        "line-receiver {}: received line={line:?}",
        name,
    );
}

pub fn timeoutcmd_line_reader_got_disconnected(name: &'static str) {
    trace!(slog_scope::logger(), "line-receiver {}: disconnected", name);
}

pub fn timeoutcmd_line_reader_did_child_die(name: &'static str) {
    trace!(
        slog_scope::logger(),
        "line-receiver {}: has child terminated?",
        name
    );
}

pub fn timeoutcmd_line_reader_child_died(name: &'static str) {
    trace!(
        slog_scope::logger(),
        "line receiver {}: OK: child has terminated, not returning line",
        name
    );
}

pub fn timeoutcmd_line_reader_child_channel_disconnected(name: &'static str) {
    trace!(
        slog_scope::logger(),
        "line receiver {}: Disconnected: child has terminated, not returning line",
        name
    );
}

pub fn timeoutcmd_nonblocking_try_byte(name: &'static str, count: usize) {
    trace!(
        slog_scope::logger(),
        "read_to_end {}: try to receive next byte ({} so far)",
        name,
        count
    );
}

pub fn timeoutcmd_nonblocking_tried_byte(
    name: &'static str,
    result: &Result<usize, std::io::Error>,
    byte: &[u8],
) {
    trace!(
        slog_scope::logger(),
        "read_to_end {}: tried to receive byte: result={:?} byte={:?}",
        name,
        result,
        byte
    );
}

pub fn timeoutcmd_nonblocking_eof(name: &'static str) {
    trace!(
        slog_scope::logger(),
        "read_to_end {}: got end of file",
        name,
    );
}

pub fn timeoutcmd_nonblocking_got_too_much(
    name: &'static str,
    result: Result<usize, std::io::Error>,
    byte: &[u8],
) {
    trace!(
        slog_scope::logger(),
        "read_to_end {}: received too much: result={:?} byte={:?}",
        name,
        result,
        byte
    );
}

pub fn timeoutcmd_nonblocking_read_error(name: &'static str, err: &std::io::Error) {
    trace!(
        slog_scope::logger(),
        "read_to_end {}: read error: {}",
        name,
        err
    );
}

pub fn timeoutcmd_nonblocking_ends(name: &'static str) {
    trace!(slog_scope::logger(), "read_to_end {}: ends", name,);
}

pub fn debug(msg: &str) {
    debug!(slog_scope::logger(), "{msg}");
}

pub fn debug2(msg: String) {
    debug!(slog_scope::logger(), "{msg}");
}

pub fn error(msg: &str, e: &impl std::error::Error) {
    error!(slog_scope::logger(), "{msg}: {e}");
    let mut e = e.source();
    while let Some(source) = e {
        error!(slog_scope::logger(), "caused by: {}", source);
        e = source.source();
    }
}