jsonrpsee_core/
tracing.rs1use serde::Serialize;
2use tracing::Level;
3
4const CLIENT: &str = "jsonrpsee-client";
5const SERVER: &str = "jsonrpsee-server";
6
7pub mod client {
9 use super::*;
10
11 pub fn tx_log_from_str(s: impl AsRef<str>, max: u32) {
13 if tracing::enabled!(Level::TRACE) {
14 let msg = truncate_at_char_boundary(s.as_ref(), max as usize);
15 tracing::trace!(target: CLIENT, send = msg);
16 }
17 }
18
19 pub fn tx_log_from_json(s: &impl Serialize, max: u32) {
21 if tracing::enabled!(Level::TRACE) {
22 let json = serde_json::to_string(s).unwrap_or_default();
23 let msg = truncate_at_char_boundary(&json, max as usize);
24 tracing::trace!(target: CLIENT, send = msg);
25 }
26 }
27
28 pub fn rx_log_from_str(s: impl AsRef<str>, max: u32) {
30 if tracing::enabled!(Level::TRACE) {
31 let msg = truncate_at_char_boundary(s.as_ref(), max as usize);
32 tracing::trace!(target: CLIENT, recv = msg);
33 }
34 }
35
36 pub fn rx_log_from_json(s: &impl Serialize, max: u32) {
38 if tracing::enabled!(Level::TRACE) {
39 let res = serde_json::to_string(s).unwrap_or_default();
40 let msg = truncate_at_char_boundary(res.as_str(), max as usize);
41 tracing::trace!(target: CLIENT, recv = msg);
42 }
43 }
44
45 pub fn rx_log_from_bytes(bytes: &[u8], max: u32) {
47 if tracing::enabled!(Level::TRACE) {
48 let res = serde_json::from_slice::<serde_json::Value>(bytes).unwrap_or_default();
49 rx_log_from_json(&res, max);
50 }
51 }
52}
53
54pub mod server {
56 use super::*;
57
58 pub fn tx_log_from_str(s: impl AsRef<str>, max: u32) {
60 if tracing::enabled!(Level::TRACE) {
61 let msg = truncate_at_char_boundary(s.as_ref(), max as usize);
62 tracing::trace!(target: SERVER, send = msg);
63 }
64 }
65
66 pub fn tx_log_from_json(s: &impl Serialize, max: u32) {
68 if tracing::enabled!(Level::TRACE) {
69 let json = serde_json::to_string(s).unwrap_or_default();
70 let msg = truncate_at_char_boundary(&json, max as usize);
71 tracing::trace!(target: SERVER, send = msg);
72 }
73 }
74
75 pub fn rx_log_from_str(s: impl AsRef<str>, max: u32) {
77 if tracing::enabled!(Level::TRACE) {
78 let msg = truncate_at_char_boundary(s.as_ref(), max as usize);
79 tracing::trace!(target: SERVER, recv = msg);
80 }
81 }
82
83 pub fn rx_log_from_json(s: &impl Serialize, max: u32) {
85 if tracing::enabled!(Level::TRACE) {
86 let res = serde_json::to_string(s).unwrap_or_default();
87 let msg = truncate_at_char_boundary(res.as_str(), max as usize);
88 tracing::trace!(target: SERVER, recv = msg);
89 }
90 }
91
92 pub fn rx_log_from_bytes(bytes: &[u8], max: u32) {
94 if tracing::enabled!(Level::TRACE) {
95 let res = serde_json::from_slice::<serde_json::Value>(bytes).unwrap_or_default();
96 rx_log_from_json(&res, max);
97 }
98 }
99}
100
101fn truncate_at_char_boundary(s: &str, max: usize) -> &str {
103 if s.len() < max {
104 return s;
105 }
106
107 match s.char_indices().nth(max) {
108 None => s,
109 Some((idx, _)) => &s[..idx],
110 }
111}
112
113#[cfg(test)]
114mod tests {
115 use super::truncate_at_char_boundary;
116
117 #[test]
118 fn truncate_at_char_boundary_works() {
119 assert_eq!(truncate_at_char_boundary("ボルテックス", 0), "");
120 assert_eq!(truncate_at_char_boundary("ボルテックス", 4), "ボルテッ");
121 assert_eq!(truncate_at_char_boundary("ボルテックス", 100), "ボルテックス");
122 assert_eq!(truncate_at_char_boundary("hola-hola", 4), "hola");
123 }
124}