From bb5453ee6ec4c83f71238f45818c75415f49c86a Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Thu, 4 Apr 2024 06:40:08 -0400 Subject: Update to new log syntax --- examples/log_events.rs | 4 +-- src/entities/itemsiter.rs | 8 +++--- src/event_stream.rs | 6 ++--- src/helpers/log.rs | 20 --------------- src/helpers/read_response.rs | 14 ++++++----- src/macros.rs | 58 ++++++++++++++++++++++---------------------- src/mastodon.rs | 17 ++++++------- src/page.rs | 26 ++++++++++---------- src/registration.rs | 21 ++++++++-------- tests/importing.rs | 1 - 10 files changed, 77 insertions(+), 98 deletions(-) diff --git a/examples/log_events.rs b/examples/log_events.rs index cfac8d2..e9854c3 100644 --- a/examples/log_events.rs +++ b/examples/log_events.rs @@ -3,7 +3,7 @@ mod register; use futures_util::TryStreamExt; -use log::{as_serde, info}; +use log::info; use mastodon_async::Result; #[cfg(feature = "toml")] @@ -16,7 +16,7 @@ async fn run() -> Result<()> { info!("watching mastodon for events. This will run forever, press Ctrl+C to kill the program."); stream .try_for_each(|(event, _client)| async move { - warn!(event = as_serde!(event); "unrecognized event received"); + warn!(event:serde = event; "unrecognized event received"); Ok(()) }) .await?; diff --git a/src/entities/itemsiter.rs b/src/entities/itemsiter.rs index 6f29391..f951e63 100644 --- a/src/entities/itemsiter.rs +++ b/src/entities/itemsiter.rs @@ -1,5 +1,5 @@ use futures::{stream::unfold, Stream}; -use log::{as_debug, as_serde, debug, info, warn}; +use log::{debug, info, warn}; use crate::page::Page; use serde::{Deserialize, Serialize}; @@ -61,7 +61,7 @@ impl<'a, T: Clone + for<'de> Deserialize<'de> + Serialize> ItemsIter { Some(()) } Err(err) => { - warn!(err = as_debug!(err); "error encountered filling next page"); + warn!(err:? = err; "error encountered filling next page"); None } _ => None, @@ -84,7 +84,7 @@ impl<'a, T: Clone + for<'de> Deserialize<'de> + Serialize> ItemsIter { this.cur_idx += 1; } let item = this.page.initial_items[idx].clone(); - debug!(item = as_serde!(item), index = idx; "yielding item from initial items"); + debug!(item:serde = item, index = idx; "yielding item from initial items"); // let item = Box::pin(item); // pin_mut!(item); Some((item, this)) @@ -95,7 +95,7 @@ impl<'a, T: Clone + for<'de> Deserialize<'de> + Serialize> ItemsIter { let idx = this.cur_idx; this.cur_idx += 1; let item = this.buffer[idx].clone(); - debug!(item = as_serde!(item), index = idx; "yielding item from initial stream"); + debug!(item:serde = item, index = idx; "yielding item from initial stream"); Some((item, this)) } }) diff --git a/src/event_stream.rs b/src/event_stream.rs index 42d762f..429090a 100644 --- a/src/event_stream.rs +++ b/src/event_stream.rs @@ -2,7 +2,7 @@ use std::io; use crate::{errors::Result, prelude::*, Error}; use futures::{stream::try_unfold, TryStream, TryStreamExt}; -use log::{as_debug, as_serde, debug, error, info, trace}; +use log::{debug, error, info, trace}; use reqwest::Response; use tokio::io::AsyncBufReadExt; use tokio_util::io::StreamReader; @@ -17,7 +17,7 @@ pub fn event_stream( client: &Mastodon, ) -> impl TryStream + '_ { let stream = StreamReader::new(response.bytes_stream().map_err(|err| { - error!(err = as_debug!(err); "error reading stream"); + error!(err:? = err; "error reading stream"); io::Error::new(io::ErrorKind::BrokenPipe, format!("{err:?}")) })); let lines_iter = stream.lines(); @@ -32,7 +32,7 @@ pub fn event_stream( } lines.push(line); if let Ok(event) = make_event(&lines) { - info!(event = as_serde!(event), location = location; "received event"); + info!(event:serde = event, location = location; "received event"); lines.clear(); return Ok(Some(((event, client.clone()), this))); } else { diff --git a/src/helpers/log.rs b/src/helpers/log.rs index 75dcb23..2b6783a 100644 --- a/src/helpers/log.rs +++ b/src/helpers/log.rs @@ -1,25 +1,5 @@ use serde::{Deserialize, Serialize}; -/// Log metadata about this request based on the type given: -/// -/// ```no_run -/// use mastodon_async::log_serde; -/// tokio_test::block_on(async { -/// let request = reqwest::get("https://example.org/").await.unwrap(); -/// log::warn!( -/// status = log_serde!(request Status), -/// headers = log_serde!(request Headers); -/// "test" -/// ); -/// }) -/// ``` -#[macro_export] -macro_rules! log_serde { - ($response:ident $type_name:tt) => { - log::as_serde!($crate::helpers::log::$type_name::from(&$response)) - }; -} - /// Serializable form of reqwest's Status type. #[derive(Debug, Copy, Clone, Serialize, Deserialize)] pub struct Status { diff --git a/src/helpers/read_response.rs b/src/helpers/read_response.rs index 66ce2b0..663bb3c 100644 --- a/src/helpers/read_response.rs +++ b/src/helpers/read_response.rs @@ -1,9 +1,9 @@ use std::time::Duration; -use crate::{errors::Result, log_serde, Error}; +use crate::{errors::Result, Error}; use futures::pin_mut; use futures_util::StreamExt; -use log::{as_debug, as_serde, debug, trace, warn}; +use log::{debug, trace, warn}; use reqwest::Response; use serde::{Deserialize, Serialize}; use tokio::time::timeout; @@ -21,13 +21,15 @@ where let mut bytes = vec![]; let url = response.url().clone(); let status = response.status(); - trace!(status = log_serde!(response Status), headers = log_serde!(response Headers); "attempting to stream response"); + trace!(status:serde = crate::helpers::log::Status::from(&response), headers:serde = crate::helpers::log::Headers::from(&response); "attempting to stream response"); let stream = response.bytes_stream(); pin_mut!(stream); loop { if let Ok(data) = timeout(Duration::from_secs(10), stream.next()).await { // as of here, we did not time out - let Some(data) = data else { break; }; + let Some(data) = data else { + break; + }; // as of here, we have not hit the end of the stream yet let data = data?; // as of here, we did not hit an error while reading the body @@ -58,14 +60,14 @@ where let result = serde_json::from_slice(bytes)?; debug!( url = url.as_str(), - result = as_serde!(result); + result:serde = result; "result parsed successfully" ); Ok(result) } else { // we've received an error message, let's deserialize that instead. let response = serde_json::from_slice(bytes)?; - debug!(status = as_debug!(status), response = as_serde!(response); "error received from API"); + debug!(status:? = status, response:serde = response; "error received from API"); Err(Error::Api { status, response }) } } diff --git a/src/macros.rs b/src/macros.rs index f4eb274..1bbb7bd 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -18,10 +18,10 @@ macro_rules! methods { async fn $method_with_call_id serde::Deserialize<'de> + serde::Serialize>(&self, url: impl AsRef, call_id: Uuid) -> Result { - use log::{debug, as_debug}; + use log::debug; let url = url.as_ref(); - debug!(url = url, method = stringify!($method), call_id = as_debug!(call_id); "making API request"); + debug!(url = url, method = stringify!($method), call_id:? = call_id; "making API request"); let response = self.authenticated(self.client.$method(url)).header("Accept", "application/json").send().await?; read_response(response).await } @@ -47,10 +47,10 @@ macro_rules! paged_routes { "```" ), pub async fn $name(&self) -> Result> { - use log::{debug, as_debug}; + use log::debug; let url = self.route(concat!("/api/v1/", $url)); let call_id = uuid::Uuid::new_v4(); - debug!(url = url, method = stringify!($method), call_id = as_debug!(call_id); "making API request"); + debug!(url = url, method = stringify!($method), call_id:? = call_id; "making API request"); let response = self.authenticated(self.client.$method(&url)).header("Accept", "application/json").send().await?; Page::new(self.clone(), response, call_id).await @@ -70,7 +70,7 @@ macro_rules! paged_routes { ), pub async fn $name<'a>(&self, $($param: $typ,)*) -> Result> { use serde_urlencoded; - use log::{debug, as_debug}; + use log::debug; let call_id = uuid::Uuid::new_v4(); @@ -99,7 +99,7 @@ macro_rules! paged_routes { let url = format!(concat!("/api/v1/", $url, "?{}"), &qs); let url = self.route(url); - debug!(url = url, method = "get", call_id = as_debug!(call_id); "making API request"); + debug!(url = url, method = "get", call_id:? = call_id; "making API request"); let response = self.authenticated(self.client.get(&url)).header("Accept", "application/json").send().await?; @@ -123,7 +123,7 @@ macro_rules! route_v2 { ), pub async fn $name<'a>(&self, $($param: $typ,)*) -> Result<$ret> { use serde_urlencoded; - use log::{debug, as_serde}; + use log::debug; use uuid::Uuid; let call_id = Uuid::new_v4(); @@ -147,7 +147,7 @@ macro_rules! route_v2 { let qs = serde_urlencoded::to_string(&qs_data)?; - debug!(query_string_data = as_serde!(qs_data); "URL-encoded data to be sent in API request"); + debug!(query_string_data:serde = qs_data; "URL-encoded data to be sent in API request"); let url = format!(concat!("/api/v2/", $url, "?{}"), &qs); @@ -167,7 +167,7 @@ macro_rules! route_v2 { "\n# Errors\nIf `access_token` is not set."), pub async fn $name(&self $(, $param: $typ)*, description: Option) -> Result<$ret> { use reqwest::multipart::Form; - use log::{debug, as_debug}; + use log::debug; use uuid::Uuid; let call_id = Uuid::new_v4(); @@ -185,7 +185,7 @@ macro_rules! route_v2 { debug!( url = url, method = stringify!($method), - multipart_form_data = as_debug!(form_data), call_id = as_debug!(call_id); + multipart_form_data:? = form_data, call_id:? = call_id; "making API request" ); @@ -210,7 +210,7 @@ macro_rules! route_v2 { "`\n# Errors\nIf `access_token` is not set."), pub async fn $name(&self, $($param: $typ,)*) -> Result<$ret> { use reqwest::multipart::Form; - use log::{debug, as_debug}; + use log::debug; use uuid::Uuid; @@ -225,7 +225,7 @@ macro_rules! route_v2 { debug!( url = url, method = stringify!($method), - multipart_form_data = as_debug!(form_data), call_id = as_debug!(call_id); + multipart_form_data:? = form_data, call_id:? = call_id; "making API request" ); @@ -254,7 +254,7 @@ macro_rules! route { "`\n# Errors\nIf `access_token` is not set."), pub async fn $name(&self, $($param: $typ,)*) -> Result<$ret> { use reqwest::multipart::Form; - use log::{debug, as_debug}; + use log::debug; use uuid::Uuid; @@ -269,7 +269,7 @@ macro_rules! route { debug!( url = url, method = stringify!($method), - multipart_form_data = as_debug!(form_data), call_id = as_debug!(call_id); + multipart_form_data:? = form_data, call_id:? = call_id; "making API request" ); @@ -295,7 +295,7 @@ macro_rules! route { "\n# Errors\nIf `access_token` is not set."), pub async fn $name(&self $(, $param: $typ)*, description: Option) -> Result<$ret> { use reqwest::multipart::Form; - use log::{debug, as_debug}; + use log::debug; use uuid::Uuid; @@ -314,7 +314,7 @@ macro_rules! route { debug!( url = url, method = stringify!($method), - multipart_form_data = as_debug!(form_data), call_id = as_debug!(call_id); + multipart_form_data:? = form_data, call_id:? = call_id; "making API request" ); @@ -338,7 +338,7 @@ macro_rules! route { ), pub async fn $name<'a>(&self, $($param: $typ,)*) -> Result<$ret> { use serde_urlencoded; - use log::{debug, as_serde}; + use log::debug; use uuid::Uuid; let call_id = Uuid::new_v4(); @@ -363,7 +363,7 @@ macro_rules! route { let qs = serde_urlencoded::to_string(&qs_data)?; - debug!(query_string_data = as_serde!(qs_data); "URL-encoded data to be sent in API request"); + debug!(query_string_data:serde = qs_data; "URL-encoded data to be sent in API request"); let url = format!(concat!("/api/v1/", $url, "?{}"), &qs); @@ -382,7 +382,7 @@ macro_rules! route { "`\n# Errors\nIf `access_token` is not set.", ), pub async fn $name(&self, $($param: $typ,)*) -> Result<$ret> { - use log::{debug, as_debug, as_serde}; + use log::debug; use uuid::Uuid; let call_id = Uuid::new_v4(); @@ -395,8 +395,8 @@ macro_rules! route { let url = &self.route(concat!("/api/v1/", $url)); debug!( url = url.as_str(), method = stringify!($method), - call_id = as_debug!(call_id), - form_data = as_serde!(&form_data); + call_id:? = call_id, + form_data:serde = &form_data; "making API request" ); @@ -482,13 +482,13 @@ macro_rules! paged_routes_with_id { "```" ), pub async fn $name(&self, id: impl AsRef) -> Result> { - use log::{debug, as_debug}; + use log::debug; use uuid::Uuid; let call_id = Uuid::new_v4(); let url = self.route(&format!(concat!("/api/v1/", $url), id.as_ref())); - debug!(url = url, method = stringify!($method), call_id = as_debug!(call_id); "making API request"); + debug!(url = url, method = stringify!($method), call_id:? = call_id; "making API request"); let response = self.authenticated(self.client.$method(&url)).header("Accept", "application/json").send().await?; Page::new(self.clone(), response, call_id).await } @@ -533,8 +533,8 @@ tokio_test::block_on(async { let url = self.route(&format!("/api/v1/streaming/{}", $stream)); let response = self.authenticated(self.client.get(&url)).header("Accept", "application/json").send().await?; debug!( - status = log_serde!(response Status), url = &url, - headers = log_serde!(response Headers); + status:serde = crate::helpers::log::Status::from(&response), url = &url, + headers:serde = crate::helpers::log::Headers::from(&response); "received API response" ); let status = response.status(); @@ -584,8 +584,8 @@ tokio_test::block_on(async { let url = url.to_string(); let response = self.authenticated(self.client.get(url.as_str())).header("Accept", "application/json").send().await?; debug!( - status = log_serde!(response Status), url = as_debug!(url), - headers = log_serde!(response Headers); + status:serde = crate::helpers::log::Status::from(&response), url:? = url, + headers:serde = crate::helpers::log::Headers::from(&response); "received API response" ); let status = response.status(); @@ -635,8 +635,8 @@ tokio_test::block_on(async { let url = url.to_string(); let response = self.authenticated(self.client.get(url.as_str())).header("Accept", "application/json").send().await?; debug!( - status = log_serde!(response Status), url = as_debug!(url), - headers = log_serde!(response Headers); + status:serde = crate::helpers::log::Status::from(&response), url:? = url, + headers:serde = crate::helpers::log::Headers::from(&response); "received API response" ); let status = response.status(); diff --git a/src/mastodon.rs b/src/mastodon.rs index ee9e60d..bb5a599 100644 --- a/src/mastodon.rs +++ b/src/mastodon.rs @@ -10,13 +10,12 @@ use crate::{ }, errors::{Error, Result}, helpers::read_response::read_response, - log_serde, polling_time::PollingTime, AddFilterRequest, AddPushRequest, Data, NewStatus, Page, StatusesRequest, UpdateCredsRequest, UpdatePushRequest, }; use futures::TryStream; -use log::{as_debug, as_serde, debug, error, trace}; +use log::{debug, error, trace}; use mastodon_async_entities::attachment::ProcessedAttachment; use reqwest::{multipart::Part, Client, RequestBuilder}; use url::Url; @@ -195,8 +194,8 @@ impl Mastodon { .send() .await?; debug!( - status = log_serde!(response Status), url = url, - headers = log_serde!(response Headers); + status:serde = crate::helpers::log::Status::from(&response), url = url, + headers:serde = crate::helpers::log::Headers::from(&response); "received API response" ); read_response(response).await @@ -249,7 +248,7 @@ impl Mastodon { url += request.to_query_string()?.as_str(); - debug!(url = url, method = stringify!($method), call_id = as_debug!(call_id); "making API request"); + debug!(url = url, method = stringify!($method), call_id:? = call_id; "making API request"); let response = self.client.get(&url).send().await?; Page::new(self.clone(), response, call_id).await @@ -275,7 +274,7 @@ impl Mastodon { debug!( url = url, method = stringify!($method), - call_id = as_debug!(call_id), account_ids = as_serde!(ids); + call_id:? = call_id, account_ids:serde = ids; "making API request" ); let response = self.client.get(&url).send().await?; @@ -290,7 +289,7 @@ impl Mastodon { let url = &self.route("/api/v1/push/subscription"); debug!( url = url, method = stringify!($method), - call_id = as_debug!(call_id), post_body = as_serde!(request); + call_id:? = call_id, post_body:serde = request; "making API request" ); let response = self.client.post(url).json(&request).send().await?; @@ -306,7 +305,7 @@ impl Mastodon { let url = &self.route("/api/v1/push/subscription"); debug!( url = url, method = stringify!($method), - call_id = as_debug!(call_id), post_body = as_serde!(request); + call_id:? = call_id, post_body:serde = request; "making API request" ); let response = self.client.post(url).json(&request).send().await?; @@ -405,7 +404,7 @@ impl Mastodon { Ok(Part::bytes(data).file_name(Cow::Owned(path.to_string_lossy().to_string()))) } Err(err) => { - error!(path = as_debug!(path), error = as_debug!(err); "error reading file contents for multipart form"); + error!(path:? = path, error:? = err; "error reading file contents for multipart form"); Err(err.into()) } } diff --git a/src/page.rs b/src/page.rs index 23e8640..feb8f0b 100644 --- a/src/page.rs +++ b/src/page.rs @@ -1,7 +1,7 @@ use super::{Mastodon, Result}; use crate::{entities::itemsiter::ItemsIter, helpers::read_response::read_response, Error}; use futures::Stream; -use log::{as_debug, as_serde, debug, error, trace}; +use log::{debug, error, trace}; use reqwest::{header::LINK, Response, Url}; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -27,7 +27,7 @@ macro_rules! pages { debug!( url = url.as_str(), method = "get", - call_id = as_debug!(self.call_id), + call_id:? = self.call_id, direction = stringify!($direction); "making API request" ); @@ -39,18 +39,18 @@ macro_rules! pages { let response: Vec = read_response(response).await?; if response.is_empty() && prev.is_none() && next.is_none() { debug!( - url = url, method = "get", call_id = as_debug!(self.call_id), + url = url, method = "get", call_id:? = self.call_id, direction = stringify!($direction); "received an empty page with no links" ); return Ok(None); } debug!( - url = url, method = "get",call_id = as_debug!(self.call_id), + url = url, method = "get", call_id:? = self.call_id, direction = stringify!($direction), - prev = as_debug!(prev), - next = as_debug!(next), - response = as_serde!(response); + prev:? = prev, + next:? = next, + response:serde = response; "received next pages from API" ); self.next = next; @@ -59,9 +59,9 @@ macro_rules! pages { } Err(err) => { error!( - err = as_debug!(err), url = url, + err:? = err, url = url, method = stringify!($method), - call_id = as_debug!(self.call_id); + call_id:? = self.call_id; "error making API request" ); Err(err.into()) @@ -127,8 +127,8 @@ impl<'a, T: for<'de> Deserialize<'de> + Serialize> Page { let (prev, next) = get_links(&response, call_id)?; let initial_items = read_response(response).await?; debug!( - initial_items = as_serde!(initial_items), prev = as_debug!(prev), - next = as_debug!(next), call_id = as_debug!(call_id); + initial_items:serde = initial_items, prev:? = prev, + next:? = next, call_id:? = call_id; "received first page from API call" ); Ok(Page { @@ -185,13 +185,13 @@ fn get_links(response: &Response, call_id: Uuid) -> Result<(Option, Option< if let Some(link_header) = response.headers().get(LINK) { let link_header = link_header.to_str()?; let raw_link_header = link_header.to_string(); - trace!(link_header = link_header, call_id = as_debug!(call_id); "parsing link header"); + trace!(link_header = link_header, call_id:? = call_id; "parsing link header"); let link_header = parse_link_header::parse(link_header)?; for (rel, link) in link_header.iter() { match rel.as_ref().map(|it| it.as_str()) { Some("next") => next = Some(link.uri.clone()), Some("prev") => prev = Some(link.uri.clone()), - None => debug!(link = as_debug!(link); "link header with no rel specified"), + None => debug!(link:? = link; "link header with no rel specified"), Some(other) => { return Err(Error::UnrecognizedRel { rel: other.to_string(), diff --git a/src/registration.rs b/src/registration.rs index cb869a4..d87ae63 100644 --- a/src/registration.rs +++ b/src/registration.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use log::{as_debug, as_serde, debug, error, trace}; +use log::{debug, error, trace}; use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; use reqwest::Client; use uuid::Uuid; @@ -8,7 +8,6 @@ use uuid::Uuid; use crate::{ apps::{App, AppBuilder}, helpers::read_response::read_response, - log_serde, scopes::Scopes, Data, Error, Mastodon, Result, }; @@ -196,24 +195,24 @@ impl<'a> Registration<'a> { async fn send_app(&self, app: &App) -> Result { let url = format!("{}/api/v1/apps", self.base); let call_id = Uuid::new_v4(); - debug!(url = url, app = as_serde!(app), call_id = as_debug!(call_id); "registering app"); + debug!(url = url, app:serde = app, call_id:? = call_id; "registering app"); let response = self.client.post(&url).json(&app).send().await?; match response.error_for_status() { Ok(response) => { let response = read_response(response).await?; debug!( - response = as_serde!(response), app = as_serde!(app), + response:serde = response, app:serde = app, url = url, method = stringify!($method), - call_id = as_debug!(call_id); + call_id:? = call_id; "received API response" ); Ok(response) } Err(err) => { error!( - err = as_debug!(err), url = url, method = stringify!($method), - call_id = as_debug!(call_id); + err:? = err, url = url, method = stringify!($method), + call_id:? = call_id; "error making API request" ); Err(err.into()) @@ -361,14 +360,14 @@ impl Registered { debug!(url = url; "completing registration"); let response = self.client.post(&url).send().await?; debug!( - status = log_serde!(response Status), url = url, - headers = log_serde!(response Headers); + status:serde = crate::helpers::log::Status::from(&response), url = url, + headers:serde = crate::helpers::log::Headers::from(&response); "received API response" ); let token: AccessToken = read_response(response).await?; - debug!(url = url, body = as_serde!(token); "parsed response body"); + debug!(url = url, body:serde = token; "parsed response body"); let data = self.registered(token.access_token); - trace!(auth_data = as_serde!(data); "registered"); + trace!(auth_data:serde = data; "registered"); Ok(Mastodon::new(self.client.clone(), data)) } diff --git a/tests/importing.rs b/tests/importing.rs index 8fb7f92..9743898 100644 --- a/tests/importing.rs +++ b/tests/importing.rs @@ -50,7 +50,6 @@ fn test_compile_imports() { use mastodon_async::helpers::log::Headers as _; use mastodon_async::helpers::log::Status as _; use mastodon_async::helpers::read_response::read_response as _; - use mastodon_async::log_serde as _; use mastodon_async::mastodon::Mastodon as _; use mastodon_async::mastodon::MastodonClient as _; use mastodon_async::mastodon::MastodonUnauthenticated as _; -- cgit v1.2.3