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
pub mod messaging;
pub mod network_knowledge;
pub mod types;
#[macro_use]
extern crate tracing;
pub use network_knowledge::{elder_count, SectionAuthorityProvider};
const DEFAULT_DATA_COPY_COUNT: usize = 4;
const SN_DATA_COPY_COUNT: &str = "SN_DATA_COPY_COUNT";
pub fn max_num_faulty_elders() -> usize {
elder_count() / 3
}
pub fn max_num_faulty_elders_for_sap(sap: SectionAuthorityProvider) -> usize {
sap.elder_count() / 3
}
pub fn at_least_one_correct_elder() -> usize {
max_num_faulty_elders() + 1
}
pub fn data_copy_count() -> usize {
match std::env::var(SN_DATA_COPY_COUNT) {
Ok(count) => match count.parse() {
Ok(count) => {
warn!(
"data_copy_count countout set from env var SN_DATA_COPY_COUNT: {:?}",
SN_DATA_COPY_COUNT
);
count
}
Err(error) => {
warn!("There was an error parsing {:?} env var. DEFAULT_DATA_COPY_COUNT will be used: {:?}", SN_DATA_COPY_COUNT, error);
DEFAULT_DATA_COPY_COUNT
}
},
Err(_) => DEFAULT_DATA_COPY_COUNT,
}
}
use tracing_core::{Event, Subscriber};
use tracing_subscriber::{
fmt::{
format::Writer,
time::{FormatTime, SystemTime},
FmtContext, FormatEvent, FormatFields,
},
registry::LookupSpan,
};
#[derive(Default, Debug)]
pub struct LogFormatter;
impl<S, N> FormatEvent<S, N> for LogFormatter
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &FmtContext<'_, S, N>,
mut writer: Writer,
event: &Event<'_>,
) -> std::fmt::Result {
let level = *event.metadata().level();
let module = event.metadata().module_path().unwrap_or("<unknown module>");
let time = SystemTime::default();
write!(writer, "[")?;
time.format_time(&mut writer)?;
write!(writer, " {level} {module}")?;
ctx.visit_spans(|span| write!(writer, "/{}", span.name()))?;
write!(writer, "] ")?;
ctx.field_format().format_fields(writer.by_ref(), event)?;
writeln!(writer)
}
}
#[cfg(feature = "test-utils")]
use std::sync::Once;
#[cfg(feature = "test-utils")]
static INIT: Once = Once::new();
#[cfg(feature = "test-utils")]
pub fn init_logger() {
INIT.call_once(|| {
tracing_subscriber::fmt::fmt()
.with_thread_names(true)
.with_ansi(false)
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_target(false)
.event_format(LogFormatter::default())
.try_init()
.unwrap_or_else(|_| println!("Error initializing logger"));
});
}