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
use crate::event::{EventType, Json, Keyframe, TraceEvent};
use crate::point::{Metadata, TracePoint};
use crate::time::now;

static mut TRACER: &'static dyn Trace = &PrintTrace;

pub trait Trace: Sync + Send {
    fn report(&self, event: TracePoint);
    fn metadata(&self) -> Metadata;
    fn now(&self) -> u64;
}

struct PrintTrace;

impl Trace for PrintTrace {
    fn report(&self, point: TracePoint) {
        println!("{:?}", point);
    }

    fn metadata(&self) -> Metadata {
        Metadata {
            address: "".to_string(),
        }
    }

    fn now(&self) -> u64 {
        now()
    }
}

fn tracer() -> &'static dyn Trace {
    unsafe { TRACER }
}

fn set_tracer<F>(make_tracer: F) -> Result<(), SetTraceError>
where
    F: FnOnce() -> &'static dyn Trace,
{
    unsafe {
        TRACER = make_tracer();
        Ok(())
    }
}

#[derive(Debug)]
pub struct SetTraceError;

pub fn set_boxed_tracer(tracer: Box<dyn Trace>) -> Result<(), SetTraceError> {
    set_tracer(|| unsafe { &*Box::into_raw(tracer) })
}

fn report(event: TraceEvent) {
    let t = tracer();
    let metadata = t.metadata();
    t.report(TracePoint {
        timestamp: t.now(),
        event,
        metadata,
    });
}

fn report_keyframe(event_name: String, keyframe: Keyframe) {
    report(TraceEvent {
        event_name,
        event_type: EventType::Keyframe {
            frame_info: keyframe,
        },
        tag: None,
    })
}

pub fn start_block(block_height: u64) {
    report_keyframe("start_block".to_string(), Keyframe::NewBlock { block_height });
}

pub fn start_round(round_id: u64, block_height: u64) {
    report_keyframe(
        "start_round".to_string(),
        Keyframe::NewRound { round_id, block_height },
    )
}

pub fn start_step(step_name: String, round_id: u64, block_height: u64) {
    report_keyframe(
        "start_step".to_string(),
        Keyframe::NewStep {
            step_name,
            round_id,
            block_height,
        },
    )
}

pub fn receive_proposal(
    event_name: String,
    block_height: u64,
    round_id: u64,
    proposer: String,
    hash: String,
    tag: Option<Json>,
) {
    report(TraceEvent {
        event_name,
        event_type: EventType::Propose {
            block_height,
            round_id,
            proposer,
            hash,
        },
        tag,
    });
}

pub fn receive_vote(
    event_name: String,
    block_height: u64,
    round_id: u64,
    voter: String,
    hash: String,
    tag: Option<Json>,
) {
    report(TraceEvent {
        event_name,
        event_type: EventType::Vote {
            block_height,
            round_id,
            voter,
            hash,
        },
        tag,
    })
}

/// report a custom event
pub fn custom(event_name: String, tag: Option<Json>) {
    report(TraceEvent {
        event_name,
        event_type: EventType::Custom,
        tag,
    });
}

/// report an error event
pub fn error(event_name: String, tag: Option<Json>) {
    report(TraceEvent {
        event_name,
        event_type: EventType::Error,
        tag,
    });
}