Unverified Commit 37474f45 authored by Niklas Adolfsson's avatar Niklas Adolfsson Committed by GitHub
Browse files

switch to the tracing crate (#525)

parent af16b390
......@@ -10,7 +10,8 @@ publish = false
anyhow = "1"
env_logger = "0.9"
jsonrpsee = { path = "../jsonrpsee", features = ["full"] }
log = "0.4"
tracing = "0.1"
tracing-subscriber = "0.2"
tokio = { version = "1", features = ["full"] }
[[example]]
......
......@@ -34,7 +34,9 @@ use std::net::SocketAddr;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
// init tracing `FmtSubscriber`.
let subscriber = tracing_subscriber::FmtSubscriber::new();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
let (server_addr, _handle) = run_server().await?;
let url = format!("http://{}", server_addr);
......@@ -42,7 +44,7 @@ async fn main() -> anyhow::Result<()> {
let client = HttpClientBuilder::default().build(url)?;
let params = rpc_params!(1_u64, 2, 3);
let response: Result<String, _> = client.request("say_hello", params).await;
println!("r: {:?}", response);
tracing::info!("r: {:?}", response);
Ok(())
}
......
......@@ -72,7 +72,9 @@ impl RpcServer<ExampleHash, ExampleStorageKey> for RpcServerImpl {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
// init tracing `FmtSubscriber`.
let subscriber = tracing_subscriber::FmtSubscriber::builder().finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
let (server_addr, _handle) = run_server().await?;
let url = format!("ws://{}", server_addr);
......
......@@ -33,13 +33,16 @@ use std::net::SocketAddr;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
// init tracing `FmtSubscriber`.
let subscriber = tracing_subscriber::FmtSubscriber::builder().finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
let addr = run_server().await?;
let url = format!("ws://{}", addr);
let client = WsClientBuilder::default().build(&url).await?;
let response: String = client.request("say_hello", None).await?;
println!("r: {:?}", response);
tracing::info!("response: {:?}", response);
Ok(())
}
......
......@@ -34,7 +34,10 @@ use std::net::SocketAddr;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();
// init tracing `FmtSubscriber`.
let subscriber = tracing_subscriber::FmtSubscriber::builder().finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
let addr = run_server().await?;
let url = format!("ws://{}", addr);
......@@ -43,12 +46,12 @@ async fn main() -> anyhow::Result<()> {
// Subscription with a single parameter
let mut sub_params_one =
client.subscribe::<Option<char>>("sub_one_param", rpc_params![3], "unsub_one_param").await?;
println!("subscription with one param: {:?}", sub_params_one.next().await);
tracing::info!("subscription with one param: {:?}", sub_params_one.next().await);
// Subscription with multiple parameters
let mut sub_params_two =
client.subscribe::<String>("sub_params_two", rpc_params![2, 5], "unsub_params_two").await?;
println!("subscription with two params: {:?}", sub_params_two.next().await);
tracing::info!("subscription with two params: {:?}", sub_params_two.next().await);
Ok(())
}
......
......@@ -32,10 +32,14 @@ use jsonrpsee::{
};
use std::net::SocketAddr;
const NUM_SUBSCRIPTION_RESPONSES: usize = 5;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
const NUM_SUBSCRIPTION_RESPONSES: usize = 5;
env_logger::init();
// init tracing `FmtSubscriber`.
let subscriber = tracing_subscriber::FmtSubscriber::builder().finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
let addr = run_server().await?;
let url = format!("ws://{}", addr);
......@@ -46,7 +50,7 @@ async fn main() -> anyhow::Result<()> {
let mut i = 0;
while i <= NUM_SUBSCRIPTION_RESPONSES {
let r = subscribe_hello.next().await;
println!("received {:?}", r);
tracing::info!("received {:?}", r);
i += 1;
}
......
......@@ -15,7 +15,7 @@ hyper-rustls = "0.22"
hyper = { version = "0.14.10", features = ["client", "http1", "http2", "tcp"] }
jsonrpsee-types = { path = "../types", version = "0.4.1" }
jsonrpsee-utils = { path = "../utils", version = "0.4.1", features = ["http-helpers"] }
log = "0.4"
tracing = "0.1"
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1", features = ["time"] }
......
......@@ -39,7 +39,7 @@ impl HttpTransportClient {
}
async fn inner_send(&self, body: String) -> Result<hyper::Response<hyper::Body>, Error> {
log::debug!("send: {}", body);
tracing::debug!("send: {}", body);
if body.len() > self.max_request_body_size as usize {
return Err(Error::RequestTooLarge);
......
......@@ -17,7 +17,7 @@ jsonrpsee-types = { path = "../types", version = "0.4.1" }
jsonrpsee-utils = { path = "../utils", version = "0.4.1", features = ["server", "http-helpers"] }
globset = "0.4"
lazy_static = "1.4"
log = "0.4"
tracing = "0.1"
serde_json = "1"
socket2 = "0.4"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
......
......@@ -25,8 +25,8 @@
// DEALINGS IN THE SOFTWARE.
use globset::{GlobBuilder, GlobMatcher};
use log::warn;
use std::{fmt, hash};
use tracing::warn;
/// Pattern that can be matched to string.
pub(crate) trait Pattern {
......
......@@ -222,7 +222,7 @@ impl Server {
Err(GenericTransportError::TooLarge) => return Ok::<_, HyperError>(response::too_large()),
Err(GenericTransportError::Malformed) => return Ok::<_, HyperError>(response::malformed()),
Err(GenericTransportError::Inner(e)) => {
log::error!("Internal error reading request body: {}", e);
tracing::error!("Internal error reading request body: {}", e);
return Ok::<_, HyperError>(response::internal_error());
}
};
......@@ -281,7 +281,7 @@ impl Server {
} else {
collect_batch_response(rx).await
};
log::debug!("[service_fn] sending back: {:?}", &response[..cmp::min(response.len(), 1024)]);
tracing::debug!("[service_fn] sending back: {:?}", &response[..cmp::min(response.len(), 1024)]);
Ok::<_, HyperError>(response::ok_response(response))
}
}))
......
......@@ -17,7 +17,7 @@ proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0", default-features = false, features = ["extra-traits", "full", "visit", "parsing"] }
proc-macro-crate = "1"
log = "0.4"
tracing = "0.1"
[dev-dependencies]
jsonrpsee = { path = "../jsonrpsee", features = ["full"] }
......
......@@ -294,7 +294,7 @@ impl RpcDescription {
let #name: #ty = match seq.optional_next() {
Ok(v) => v,
Err(e) => {
log::error!(concat!("Error parsing optional \"", stringify!(#name), "\" as \"", stringify!(#ty), "\": {:?}"), e);
tracing::error!(concat!("Error parsing optional \"", stringify!(#name), "\" as \"", stringify!(#ty), "\": {:?}"), e);
return Err(e.into())
}
};
......@@ -304,7 +304,7 @@ impl RpcDescription {
let #name: #ty = match seq.next() {
Ok(v) => v,
Err(e) => {
log::error!(concat!("Error parsing \"", stringify!(#name), "\" as \"", stringify!(#ty), "\": {:?}"), e);
tracing::error!(concat!("Error parsing \"", stringify!(#name), "\" as \"", stringify!(#ty), "\": {:?}"), e);
return Err(e.into())
}
};
......
......@@ -12,7 +12,7 @@ anyhow = "1"
futures-channel = "0.3.14"
futures-util = "0.3.14"
hyper = { version = "0.14.10", features = ["full"] }
log = "0.4"
tracing = "0.1"
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = "1"
soketto = { version = "0.7", features = ["http"] }
......
......@@ -278,12 +278,12 @@ async fn connection_task(socket: tokio::net::TcpStream, mode: ServerMode, mut ex
match &mode {
ServerMode::Subscription { subscription_response, .. } => {
if let Err(e) = sender.send_text(&subscription_response).await {
log::warn!("send response to subscription: {:?}", e);
tracing::warn!("send response to subscription: {:?}", e);
}
},
ServerMode::Notification(n) => {
if let Err(e) = sender.send_text(&n).await {
log::warn!("send notification: {:?}", e);
tracing::warn!("send notification: {:?}", e);
}
},
_ => {}
......@@ -296,12 +296,12 @@ async fn connection_task(socket: tokio::net::TcpStream, mode: ServerMode, mut ex
match &mode {
ServerMode::Response(r) => {
if let Err(e) = sender.send_text(&r).await {
log::warn!("send response to request error: {:?}", e);
tracing::warn!("send response to request error: {:?}", e);
}
},
ServerMode::Subscription { subscription_id, .. } => {
if let Err(e) = sender.send_text(&subscription_id).await {
log::warn!("send subscription id error: {:?}", e);
tracing::warn!("send subscription id error: {:?}", e);
}
},
_ => {}
......@@ -340,7 +340,7 @@ async fn handler(
other_server: String,
) -> Result<hyper::Response<Body>, soketto::BoxedError> {
if is_upgrade_request(&req) {
log::debug!("{:?}", req);
tracing::debug!("{:?}", req);
match req.uri().path() {
"/myblock/two" => {
......
......@@ -13,4 +13,4 @@ futures = { version = "0.3.14", default-features = false, features = ["std"] }
jsonrpsee = { path = "../jsonrpsee", features = ["full"] }
tokio = { version = "1", features = ["full"] }
serde_json = "1"
log = "0.4"
tracing = "0.1"
......@@ -15,7 +15,7 @@ anyhow = "1"
beef = { version = "0.5.1", features = ["impl_serde"] }
futures-channel = { version = "0.3.14", features = ["sink"] }
futures-util = { version = "0.3.14", default-features = false, features = ["std", "sink", "channel"] }
log = { version = "0.4", default-features = false }
tracing = { version = "0.1", default-features = false }
serde = { version = "1", default-features = false, features = ["derive"] }
serde_json = { version = "1", default-features = false, features = ["alloc", "raw_value", "std"] }
thiserror = "1.0"
......
......@@ -158,18 +158,18 @@ impl<'a> ParamsSequence<'a> {
T: Deserialize<'a>,
{
let mut json = self.0;
log::trace!("[next_inner] Params JSON: {:?}", json);
tracing::trace!("[next_inner] Params JSON: {:?}", json);
match json.as_bytes().get(0)? {
b']' => {
self.0 = "";
log::trace!("[next_inner] Reached end of sequence.");
tracing::trace!("[next_inner] Reached end of sequence.");
return None;
}
b'[' | b',' => json = &json[1..],
_ => {
let errmsg = format!("Invalid params. Expected one of '[', ']' or ',' but found {:?}", json);
log::error!("[next_inner] {}", errmsg);
tracing::error!("[next_inner] {}", errmsg);
return Some(Err(CallError::InvalidParams(anyhow!(errmsg))));
}
}
......@@ -183,7 +183,7 @@ impl<'a> ParamsSequence<'a> {
Some(Ok(value))
}
Err(e) => {
log::error!(
tracing::error!(
"[next_inner] Deserialization to {:?} failed. Error: {:?}, input JSON: {:?}",
std::any::type_name::<T>(),
e,
......
......@@ -14,7 +14,7 @@ futures-channel = { version = "0.3.14", default-features = false, optional = tru
futures-util = { version = "0.3.14", default-features = false, optional = true }
hyper = { version = "0.14.10", default-features = false, features = ["stream"], optional = true }
jsonrpsee-types = { path = "../types", version = "0.4.1", optional = true }
log = { version = "0.4", optional = true }
tracing = { version = "0.1", optional = true }
rustc-hash = { version = "1", optional = true }
rand = { version = "0.8", optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
......@@ -33,7 +33,7 @@ server = [
"rustc-hash",
"serde",
"serde_json",
"log",
"tracing",
"parking_lot",
"rand",
"tokio",
......
......@@ -39,14 +39,14 @@ pub fn send_response(id: Id, tx: &MethodSink, result: impl Serialize) {
let json = match serde_json::to_string(&Response { jsonrpc: TwoPointZero, id: id.clone(), result }) {
Ok(json) => json,
Err(err) => {
log::error!("Error serializing response: {:?}", err);
tracing::error!("Error serializing response: {:?}", err);
return send_error(id, tx, ErrorCode::InternalError.into());
}
};
if let Err(err) = tx.unbounded_send(json) {
log::error!("Error sending response to the client: {:?}", err)
tracing::error!("Error sending response to the client: {:?}", err)
}
}
......@@ -55,14 +55,14 @@ pub fn send_error(id: Id, tx: &MethodSink, error: ErrorObject) {
let json = match serde_json::to_string(&RpcError { jsonrpc: TwoPointZero, error, id }) {
Ok(json) => json,
Err(err) => {
log::error!("Error serializing error message: {:?}", err);
tracing::error!("Error serializing error message: {:?}", err);
return;
}
};
if let Err(err) = tx.unbounded_send(json) {
log::error!("Could not send error response to the client: {:?}", err)
tracing::error!("Could not send error response to the client: {:?}", err)
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment