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
pub mod event; pub mod point; pub mod time; pub mod trace; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); #[cfg(test)] mod test { use std::fs::File; use std::io::{prelude::*, BufReader, LineWriter, Write}; use std::sync::Mutex; use serde_json::{from_str, json, to_string, Value}; use super::point::{Metadata, TracePoint}; use super::trace; use super::VERSION; struct WriteReporter<W: Write + Send + 'static> { reporter: Mutex<W>, } impl<W: Write + Send + 'static> WriteReporter<W> { fn new(writable: W) -> Box<WriteReporter<W>> { Box::new(WriteReporter { reporter: Mutex::new(writable), }) } } impl<W: Write + Send + 'static> trace::Trace for WriteReporter<W> { fn report(&self, point: TracePoint) { let mut file = self.reporter.lock().unwrap(); file.write_all(to_string(&point).unwrap().as_bytes()) .expect("write file failed"); file.write_all(b"\n").expect("write file failed"); } fn metadata(&self) -> Metadata { Metadata { address: "0x0000000000000000000000000000000000000000".to_string(), } } fn now(&self) -> u64 { 0 } } #[test] fn test_tracer() -> std::io::Result<()> { trace::set_boxed_tracer(WriteReporter::new(LineWriter::new( File::create("log.log")?, ))) .expect("init tracer failed"); trace::start_block(1); trace::start_round(0, 1); trace::custom( "broadcast_proposal".to_string(), Some(json!({ "hash": "0x00", "block_height": 1, "round_id": 0 })), ); trace::receive_proposal( "receive_propose".to_string(), 1, 0, "0x".to_string(), "".to_string(), None, ); trace::start_step("propose".to_string(), 0, 1); trace::error("check_failed".to_string(), None); let file = File::open("log.log")?; let reader = BufReader::new(file); let events = vec![ json!({ "timestamp": 0, "event_name": "start_block", "event_type": "keyframe", "tag": { "block_height": 1 }, "metadata": { "address": "0x0000000000000000000000000000000000000000", "v": VERSION } }), json!({ "timestamp": 0, "event_name": "start_round", "event_type": "keyframe", "tag": { "block_height": 1, "round_id": 0 }, "metadata": { "address": "0x0000000000000000000000000000000000000000", "v": VERSION } }), json!({ "timestamp": 0, "event_name": "broadcast_proposal", "event_type": "custom", "tag": { "block_height": 1, "hash": "0x00", "round_id": 0 }, "metadata": { "address": "0x0000000000000000000000000000000000000000", "v": VERSION } }), json!({ "timestamp": 0, "event_name": "receive_propose", "event_type": "propose", "tag": { "block_height": 1, "hash": "", "proposer": "0x", "round_id": 0 }, "metadata": { "address": "0x0000000000000000000000000000000000000000", "v": VERSION } }), json!({ "timestamp": 0, "event_name": "start_step", "event_type": "keyframe", "tag": { "block_height": 1, "round_id": 0, "step_name": "propose" }, "metadata": { "address": "0x0000000000000000000000000000000000000000", "v": VERSION } }), json!({ "timestamp": 0, "event_name": "check_failed", "event_type": "error", "tag": {}, "metadata": { "address": "0x0000000000000000000000000000000000000000", "v": VERSION } }), ]; for (_i, (line, json)) in reader.lines().zip(events.iter()).enumerate() { let e: Value = from_str(&line?)?; let a: Value = from_str(&to_string(json)?)?; assert_eq!(e, a); } Ok(()) } }