Skip to content

Commit 0cda080

Browse files
Refactor tracing usage. (#38)
1 parent 16dad4f commit 0cda080

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

lib/protoflow-blocks/src/blocks/io/decode_hex.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ use crate::{
44
prelude::{format, Bytes, Vec},
55
IoBlocks, StdioConfig, StdioError, StdioSystem, System,
66
};
7-
use protoflow_core::{Block, BlockError, BlockResult, BlockRuntime, InputPort, OutputPort};
7+
use protoflow_core::{error, Block, BlockError, BlockResult, BlockRuntime, InputPort, OutputPort};
88
use protoflow_derive::Block;
99
use simple_mermaid::mermaid;
10-
#[cfg(feature = "tracing")]
11-
use tracing;
1210

1311
/// A block that decodes a hexadecimal byte stream to byte.
1412
///
@@ -96,8 +94,7 @@ fn hex_value(byte: u8) -> Result<u8, BlockError> {
9694
b'A'..=b'F' => Ok(byte - b'A' + 10),
9795
_ => {
9896
let err = format!("Invalid hex character: '{}' (0x{:02X})", byte as char, byte);
99-
#[cfg(feature = "tracing")]
100-
tracing::error!(target: "DecodeHex:hex_value", err);
97+
error!(target: "DecodeHex:hex_value", err);
10198
Err(BlockError::Other(err))
10299
}
103100
}

lib/protoflow-blocks/src/blocks/sys/read_socket.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use crate::{
55
StdioConfig, StdioError, StdioSystem, System,
66
};
77
use protoflow_core::{
8-
Block, BlockError, BlockResult, BlockRuntime, OutputPort, Port, PortResult, SystemBuilding,
8+
error, info, Block, BlockError, BlockResult, BlockRuntime, OutputPort, Port, PortResult,
9+
SystemBuilding,
910
};
1011
use protoflow_derive::Block;
1112
use serde::{Deserialize, Serialize};
@@ -16,8 +17,6 @@ use std::{
1617
net::{TcpListener, TcpStream},
1718
sync::{Arc, Mutex, PoisonError},
1819
};
19-
#[cfg(feature = "tracing")]
20-
use tracing::{error, info};
2120

2221
/// A block that reads a proto object from a TCP port.
2322
///
@@ -101,7 +100,6 @@ impl Block for ReadSocket {
101100
fn prepare(&mut self, _runtime: &dyn BlockRuntime) -> BlockResult {
102101
let listener = TcpListener::bind(&self.config.connection)?;
103102
*self.listener.lock().map_err(lock_error)? = Some(listener);
104-
#[cfg(feature = "tracing")]
105103
info!("Server listening on {}", &self.config.connection);
106104
Ok(())
107105
}
@@ -116,26 +114,22 @@ impl Block for ReadSocket {
116114
.ok_or(BlockError::Other("Invalid TCP listener".into()))?;
117115

118116
let (stream, addr) = listener.accept().map_err(|e| {
119-
#[cfg(feature = "tracing")]
120117
error!("Failed to accept client connection: {}", e);
121118
BlockError::Other("Failed to accept client connection".into())
122119
})?;
123-
#[cfg(feature = "tracing")]
124120
info!("Accepted connection from {}", addr);
125121
*stream_guard = Some(stream);
126122
}
127123

128124
if let Some(stream) = stream_guard.as_mut() {
129125
handle_client::<_>(stream, self.config.buffer_size, |message| {
130-
#[cfg(feature = "tracing")]
131126
info!("Processing received message");
132127
if self.output.is_connected() {
133128
self.output.send(message)?;
134129
}
135130
Ok(())
136131
})
137132
.map_err(|e| {
138-
#[cfg(feature = "tracing")]
139133
error!("Error handling client: {}", e);
140134
BlockError::Other("Error handling client".into())
141135
})?;
@@ -163,17 +157,14 @@ where
163157
let bytes_read = stream.read(&mut buffer)?;
164158

165159
if bytes_read == 0 {
166-
#[cfg(feature = "tracing")]
167160
info!("Client disconnected");
168161
break;
169162
}
170163

171164
let message = Bytes::copy_from_slice(&buffer[..bytes_read]);
172-
#[cfg(feature = "tracing")]
173165
info!("Received message: {:?}", message);
174166

175167
if let Err(e) = process_fn(&message) {
176-
#[cfg(feature = "tracing")]
177168
error!("Failed to process message: {:?}", e);
178169
return Err(BlockError::Other("Failed to process message".into()));
179170
}
@@ -223,7 +214,6 @@ pub mod read_socket_tests {
223214
));
224215
s.connect(&read_socket.output, &std_out.input);
225216
}) {
226-
#[cfg(feature = "tracing")]
227217
error!("{}", e)
228218
}
229219
}

lib/protoflow-blocks/src/blocks/sys/write_socket.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use crate::{
55
prelude::{bytes::Bytes, vec, String},
66
StdioConfig, StdioError, StdioSystem, System,
77
};
8-
use protoflow_core::{Block, BlockError, BlockResult, BlockRuntime, InputPort, SystemBuilding};
8+
use protoflow_core::{
9+
error, Block, BlockError, BlockResult, BlockRuntime, InputPort, SystemBuilding,
10+
};
911
use protoflow_derive::Block;
1012
use serde::{Deserialize, Serialize};
1113
use simple_mermaid::mermaid;
@@ -14,8 +16,6 @@ use std::{
1416
net::TcpStream,
1517
sync::{Arc, Mutex, PoisonError},
1618
};
17-
#[cfg(feature = "tracing")]
18-
use tracing::error;
1919

2020
/// A block that writes a proto object to a TCP socket.
2121
///
@@ -99,7 +99,6 @@ impl Block for WriteSocket {
9999

100100
if stream_guard.is_none() {
101101
*stream_guard = Some(TcpStream::connect(&self.config.connection).map_err(|e| {
102-
#[cfg(feature = "tracing")]
103102
error!("Failed to connect to {}: {}", &self.config.connection, e);
104103
BlockError::Other(format!(
105104
"Failed to connect to {}: {}",
@@ -109,7 +108,6 @@ impl Block for WriteSocket {
109108
}
110109

111110
let stream = stream_guard.as_mut().ok_or_else(|| {
112-
#[cfg(feature = "tracing")]
113111
error!("Stream is not connected");
114112
BlockError::Other("Stream is not connected".into())
115113
})?;
@@ -158,7 +156,6 @@ pub mod write_socket_tests {
158156
});
159157
s.connect(&stdin.output, &write_socket.input);
160158
}) {
161-
#[cfg(feature = "tracing")]
162159
error!("{}", e)
163160
}
164161
}

lib/protoflow-core/src/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,23 @@ pub(crate) mod utils {
9595
pub use prost_types as types;
9696

9797
pub use prost::DecodeError;
98+
99+
#[cfg(feature = "tracing")]
100+
#[doc(hidden)]
101+
mod tracing {
102+
pub use tracing::{debug, error, info, trace, warn};
103+
}
104+
105+
#[cfg(not(feature = "tracing"))]
106+
#[doc(hidden)]
107+
#[rustfmt::skip]
108+
mod tracing {
109+
#[macro_export] macro_rules! debug { ($($arg:tt)+) => (); }
110+
#[macro_export] macro_rules! error { ($($arg:tt)+) => (); }
111+
#[macro_export] macro_rules! info { ($($arg:tt)+) => (); }
112+
#[macro_export] macro_rules! trace { ($($arg:tt)+) => (); }
113+
#[macro_export] macro_rules! warn { ($($arg:tt)+) => (); }
114+
}
115+
116+
#[allow(unused)]
117+
pub use tracing::*;

0 commit comments

Comments
 (0)