Remove log and switch to tracing

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 3 years ago
parent 4a5d1e6122
commit 9d75afc8c1
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

553
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -16,13 +16,13 @@ rustls_backend = ["serenity/rustls_backend"]
native_tls_backend = ["serenity/native_tls_backend"]
[dependencies]
tokio = "1.4.0"
thiserror = "1.0.24"
log = "0.4.14"
futures = "0.3.14"
serde_json = "1.0.64"
tokio = "1.17.0"
thiserror = "1.0.30"
tracing= "0.1.33"
futures = "0.3.21"
serde_json = "1.0.79"
[dependencies.serenity]
version = "0.10.5"
version = "0.10.10"
default-features = false
features = ["builder", "http", "cache", "model", "client", "gateway"]
features = ["builder", "http", "cache", "model", "client", "gateway"]

@ -10,6 +10,7 @@ use std::time::Duration;
pub struct EphemeralMessage;
impl EphemeralMessage {
#[tracing::instrument(level = "debug", skip(http, message))]
/// Ensures that an already existing message is
/// deleted after a certain amount of time
pub async fn create_from_message(
@ -17,20 +18,20 @@ impl EphemeralMessage {
message: &Message,
timeout: Duration,
) -> Result<()> {
log::debug!("Creating ephemeral message from existing message");
tracing::debug!("Creating ephemeral message from existing message");
let handle = MessageHandle::new(message.channel_id, message.id);
let http = Arc::clone(&http);
log::debug!("Starting delete task");
tracing::debug!("Starting delete task");
tokio::spawn(async move {
log::debug!("Waiting for timeout to pass");
tracing::debug!("Waiting for timeout to pass");
tokio::time::sleep(timeout).await;
log::debug!("Deleting ephemeral message");
tracing::debug!("Deleting ephemeral message");
if let Err(e) = http
.delete_message(handle.channel_id, handle.message_id)
.await
{
log::error!("Failed to delete ephemeral message {:?}: {}", handle, e);
tracing::error!("Failed to delete ephemeral message {:?}: {}", handle, e);
}
});
@ -38,6 +39,7 @@ impl EphemeralMessage {
}
/// Creates a new message that is deleted after a certain amount of time
#[tracing::instrument(level = "debug", skip(http, f))]
pub async fn create<'a, F>(
http: &Arc<Http>,
channel_id: ChannelId,
@ -47,7 +49,7 @@ impl EphemeralMessage {
where
F: for<'b> FnOnce(&'b mut CreateMessage<'a>) -> &'b mut CreateMessage<'a>,
{
log::debug!("Creating new ephemeral message");
tracing::debug!("Creating new ephemeral message");
let msg = channel_id.send_message(http, f).await?;
Self::create_from_message(http, &msg, timeout).await?;

@ -10,6 +10,7 @@ use tokio::time::Duration;
static UPDATE_INTERVAL_SECS: u64 = 5;
/// Starts the loop to handle message updates
#[tracing::instrument(level = "debug", skip(ctx))]
pub async fn start_update_loop(ctx: &Context) -> Result<()> {
let event_messages = get_listeners_from_context(ctx)
.await
@ -19,7 +20,7 @@ pub async fn start_update_loop(ctx: &Context) -> Result<()> {
tokio::task::spawn(async move {
loop {
{
log::trace!("Locking listener from update loop.");
tracing::debug!("Updating messages...");
let messages = {
let msgs_lock = event_messages.lock().await;
@ -28,13 +29,12 @@ pub async fn start_update_loop(ctx: &Context) -> Result<()> {
.map(|(k, v)| (*k, v.clone()))
.collect::<Vec<(MessageHandle, MessageRef)>>()
};
log::trace!("Listener locked.");
let mut frozen_messages = Vec::new();
for (key, msg) in messages {
let mut msg = msg.lock().await;
if let Err(e) = msg.update(&http).await {
log::error!("Failed to update message: {:?}", e);
tracing::error!("Failed to update message: {:?}", e);
}
if msg.is_frozen() {
frozen_messages.push(key);
@ -46,8 +46,8 @@ pub async fn start_update_loop(ctx: &Context) -> Result<()> {
msgs_lock.remove(&key);
}
}
tracing::debug!("Messages updated");
}
log::trace!("Listener unlocked");
tokio::time::sleep(Duration::from_secs(UPDATE_INTERVAL_SECS)).await;
}
});
@ -56,6 +56,7 @@ pub async fn start_update_loop(ctx: &Context) -> Result<()> {
}
/// To be fired from the serenity handler when a message was deleted
#[tracing::instrument(level = "debug", skip(ctx))]
pub async fn handle_message_delete(
ctx: &Context,
channel_id: ChannelId,
@ -64,9 +65,7 @@ pub async fn handle_message_delete(
let mut affected_messages = Vec::new();
{
let listeners = get_listeners_from_context(ctx).await?;
log::trace!("Locking listener from handle_message_delete.");
let mut listeners_lock = listeners.lock().await;
log::trace!("Listener locked.");
let handle = MessageHandle::new(channel_id, message_id);
if let Some(msg) = listeners_lock.get(&handle) {
@ -74,7 +73,6 @@ pub async fn handle_message_delete(
listeners_lock.remove(&handle);
}
}
log::trace!("Listener unlocked");
for msg in affected_messages {
let mut msg = msg.lock().await;
msg.on_deleted(ctx).await?;
@ -84,6 +82,7 @@ pub async fn handle_message_delete(
}
/// To be fired from the serenity handler when multiple messages were deleted
#[tracing::instrument(level = "debug", skip(ctx))]
pub async fn handle_message_delete_bulk(
ctx: &Context,
channel_id: ChannelId,
@ -92,9 +91,7 @@ pub async fn handle_message_delete_bulk(
let mut affected_messages = Vec::new();
{
let listeners = get_listeners_from_context(ctx).await?;
log::trace!("Locking listener from handle_message_delete_bulk.");
let mut listeners_lock = listeners.lock().await;
log::trace!("Listener locked.");
for message_id in message_ids {
let handle = MessageHandle::new(channel_id, *message_id);
@ -104,7 +101,6 @@ pub async fn handle_message_delete_bulk(
}
}
}
log::trace!("Listener unlocked");
for msg in affected_messages {
let mut msg = msg.lock().await;
msg.on_deleted(ctx).await?;
@ -114,13 +110,12 @@ pub async fn handle_message_delete_bulk(
}
/// Fired when a reaction was added to a message
#[tracing::instrument(level = "debug", skip(ctx))]
pub async fn handle_reaction_add(ctx: &Context, reaction: &Reaction) -> Result<()> {
let mut affected_messages = Vec::new();
{
let listeners = get_listeners_from_context(ctx).await?;
log::trace!("Locking listener from handle_reaction_add.");
let mut listeners_lock = listeners.lock().await;
log::trace!("Listener locked.");
let handle = MessageHandle::new(reaction.channel_id, reaction.message_id);
@ -128,7 +123,6 @@ pub async fn handle_reaction_add(ctx: &Context, reaction: &Reaction) -> Result<(
affected_messages.push(Arc::clone(&msg));
}
}
log::trace!("Listener unlocked");
for msg in affected_messages {
let mut msg = msg.lock().await;
msg.on_reaction_add(ctx, reaction.clone()).await?;
@ -138,13 +132,12 @@ pub async fn handle_reaction_add(ctx: &Context, reaction: &Reaction) -> Result<(
}
/// Fired when a reaction was added to a message
#[tracing::instrument(level = "debug", skip(ctx))]
pub async fn handle_reaction_remove(ctx: &Context, reaction: &Reaction) -> Result<()> {
let mut affected_messages = Vec::new();
{
let listeners = get_listeners_from_context(ctx).await?;
log::trace!("Locking listener from handle_reaction_remove.");
let mut listeners_lock = listeners.lock().await;
log::trace!("Listener locked.");
let handle = MessageHandle::new(reaction.channel_id, reaction.message_id);
@ -152,7 +145,6 @@ pub async fn handle_reaction_remove(ctx: &Context, reaction: &Reaction) -> Resul
affected_messages.push(Arc::clone(&msg));
}
}
log::trace!("Listener unlocked");
for msg in affected_messages {
let mut msg = msg.lock().await;
msg.on_reaction_remove(ctx, reaction.clone()).await?;

@ -49,6 +49,7 @@ pub struct RichEventHandler {
impl RichEventHandler {
/// Handles a generic event
#[tracing::instrument(level = "debug", skip_all)]
async fn handle_event<T: 'static + Send + Sync>(&self, ctx: Context, value: T) {
let callbacks = self.callbacks.clone();
@ -58,7 +59,7 @@ impl RichEventHandler {
for callback in callbacks {
if let Some(cb) = callback.downcast_ref::<EventCallback<T>>() {
if let Err(e) = cb.run(&ctx, &value).await {
log::error!("Error in event callback: {:?}", e);
tracing::error!("Error in event callback: {:?}", e);
}
}
}

@ -16,11 +16,11 @@ impl TypeMapKey for EventDrivenMessageContainer {
type Value = EventDrivenMessagesRef;
}
#[tracing::instrument(level = "trace", skip(ctx))]
pub async fn get_listeners_from_context(ctx: &Context) -> Result<EventDrivenMessagesRef> {
let data = ctx.data.read().await;
let listeners = data
.get::<EventDrivenMessageContainer>()
.ok_or(Error::Uninitialized)?;
log::trace!("Returning listener");
Ok(listeners.clone())
}

@ -10,8 +10,8 @@ use serenity::model::channel::Reaction;
use std::sync::atomic::Ordering;
/// Shows the next page in the menu
#[tracing::instrument(level = "debug", skip_all)]
pub async fn next_page(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Result<()> {
log::debug!("Showing next page");
menu.current_page = (menu.current_page + 1) % menu.pages.len();
display_page(ctx, menu).await?;
@ -19,8 +19,8 @@ pub async fn next_page(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Resul
}
/// Shows the previous page in the menu
#[tracing::instrument(level = "debug", skip_all)]
pub async fn previous_page(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Result<()> {
log::debug!("Showing previous page");
if menu.current_page == 0 {
menu.current_page = menu.pages.len() - 1;
} else {
@ -32,8 +32,8 @@ pub async fn previous_page(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> R
}
/// Shows the previous page in the menu
#[tracing::instrument(level = "debug", skip_all)]
pub async fn close_menu(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Result<()> {
log::debug!("Closing menu");
menu.close(ctx.http()).await?;
let listeners = get_listeners_from_context(&ctx).await?;
let mut listeners_lock = listeners.lock().await;
@ -43,8 +43,9 @@ pub async fn close_menu(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Resu
Ok(())
}
/// Shows a help menu
#[tracing::instrument(level = "debug", skip_all)]
pub async fn toggle_help(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Result<()> {
log::debug!("Displaying help");
let show_help = menu
.data
.get::<HelpActiveContainer>()
@ -63,7 +64,7 @@ pub async fn toggle_help(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Res
.get()
.await?;
let mut message = menu.get_message(ctx.http()).await?;
log::debug!("Building help entries");
tracing::debug!("Building help entries");
let mut help_entries = menu
.help_entries
.iter()
@ -75,7 +76,7 @@ pub async fn toggle_help(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Res
.map(|(_, e, h)| format!(" - {} {}", e, h))
.collect::<Vec<String>>()
.join("\n");
log::trace!("Help message is {}", help_message);
tracing::trace!("Help message is {}", help_message);
message
.edit(ctx, |m| {
@ -104,15 +105,16 @@ pub async fn toggle_help(ctx: &Context, menu: &mut Menu<'_>, _: Reaction) -> Res
m
})
.await?;
log::debug!("Help message displayed");
tracing::debug!("Help message displayed");
show_help.store(true, Ordering::Relaxed);
Ok(())
}
/// Displays the menu page
#[tracing::instrument(level = "debug", skip_all)]
pub async fn display_page(ctx: &Context, menu: &mut Menu<'_>) -> Result<()> {
log::debug!("Displaying page {}", menu.current_page);
tracing::debug!("Displaying page {}", menu.current_page);
let page = menu
.pages
.get(menu.current_page)
@ -126,7 +128,7 @@ pub async fn display_page(ctx: &Context, menu: &mut Menu<'_>) -> Result<()> {
e
})
.await?;
log::debug!("Page displayed");
tracing::debug!("Page displayed");
Ok(())
}

@ -81,8 +81,8 @@ pub struct Menu<'a> {
impl Menu<'_> {
/// Removes all reactions from the menu
#[tracing::instrument(level = "debug", skip_all)]
pub(crate) async fn close(&mut self, http: &Http) -> Result<()> {
log::debug!("Closing menu...");
let handle = self.message.read().await;
http.delete_message_reactions(handle.channel_id, handle.message_id)
.await?;
@ -101,14 +101,12 @@ impl Menu<'_> {
}
/// Recreates the message completely
#[tracing::instrument(level = "debug", skip_all)]
pub async fn recreate(&self, http: &Http) -> Result<()> {
log::debug!("Recreating message");
let old_handle = {
let handle = self.message.read().await;
(*handle).clone()
};
log::debug!("Getting current page");
let current_page = self
.pages
.get(self.current_page)
@ -117,7 +115,6 @@ impl Menu<'_> {
.get()
.await?;
log::debug!("Creating new message");
let message = http
.send_message(
old_handle.channel_id,
@ -139,7 +136,6 @@ impl Menu<'_> {
)
.await?;
}
log::trace!("New message is {:?}", message);
let new_handle = {
let mut handle = self.message.write().await;
@ -147,15 +143,14 @@ impl Menu<'_> {
(*handle).clone()
};
{
log::debug!("Changing key of message");
tracing::debug!("Changing key of message");
let mut listeners_lock = self.listeners.lock().await;
let menu = listeners_lock.remove(&old_handle).unwrap();
listeners_lock.insert(new_handle, menu);
}
log::debug!("Deleting original message");
tracing::debug!("Deleting original message");
http.delete_message(old_handle.channel_id, old_handle.message_id)
.await?;
log::debug!("Message recreated");
Ok(())
}
@ -167,13 +162,14 @@ impl<'a> EventDrivenMessage for Menu<'a> {
self.closed
}
#[tracing::instrument(level = "debug", skip_all)]
async fn update(&mut self, http: &Http) -> Result<()> {
log::trace!("Checking for menu timeout");
tracing::trace!("Checking for menu timeout");
if Instant::now() >= self.timeout {
log::debug!("Menu timout reached. Closing menu.");
tracing::debug!("Menu timout reached. Closing menu.");
self.close(http).await?;
} else if self.sticky {
log::debug!("Message is sticky. Checking for new messages in channel...");
tracing::debug!("Message is sticky. Checking for new messages in channel...");
let handle = {
let handle = self.message.read().await;
(*handle).clone()
@ -183,9 +179,8 @@ impl<'a> EventDrivenMessage for Menu<'a> {
let messages = channel_id
.messages(http, |p| p.after(handle.message_id).limit(1))
.await?;
log::trace!("Messages are {:?}", messages);
if messages.len() > 0 {
log::debug!("New messages in channel. Recreating...");
tracing::debug!("New messages in channel. Recreating...");
self.recreate(http).await?;
}
}
@ -193,26 +188,28 @@ impl<'a> EventDrivenMessage for Menu<'a> {
Ok(())
}
#[tracing::instrument(level = "debug", skip_all)]
async fn on_reaction_add(&mut self, ctx: &Context, reaction: Reaction) -> Result<()> {
log::debug!("Reaction to menu added");
let current_user = ctx.http.get_current_user().await?;
if reaction.user_id.unwrap().0 == current_user.id.0 {
log::debug!("Reaction is from current user.");
tracing::debug!("Reaction is from current user.");
return Ok(());
}
let emoji_string = reaction.emoji.as_data();
log::debug!("Deleting user reaction.");
tracing::debug!("Deleting user reaction.");
reaction.delete(ctx).await?;
if let Some(owner) = self.owner {
if owner != reaction.user_id.unwrap() {
log::debug!("Menu has an owner and the reaction is not from the owner of the menu");
tracing::debug!(
"Menu has an owner and the reaction is not from the owner of the menu"
);
return Ok(());
}
}
if let Some(control) = self.controls.get(&emoji_string).cloned() {
log::debug!("Running control");
tracing::debug!("Running control");
control.run(ctx, self, reaction).await?;
}
@ -249,8 +246,8 @@ impl Default for MenuBuilder {
impl MenuBuilder {
/// Creates a new pagination menu
#[tracing::instrument(level = "debug", skip_all)]
pub fn new_paginator() -> Self {
log::debug!("Creating new paginator");
let mut controls = HashMap::new();
let mut help_entries = HashMap::new();
controls.insert(
@ -391,12 +388,12 @@ impl MenuBuilder {
}
/// builds the menu
#[tracing::instrument(level = "debug", skip_all)]
pub async fn build(
self,
ctx: &Context,
channel_id: ChannelId,
) -> Result<Arc<RwLock<MessageHandle>>> {
log::debug!("Building menu...");
let mut current_page = self
.pages
.get(self.current_page)
@ -406,9 +403,8 @@ impl MenuBuilder {
.await?;
let message = channel_id.send_message(ctx, |_| &mut current_page).await?;
log::trace!("Message is {:?}", message);
let listeners = get_listeners_from_context(ctx).await?;
log::debug!("Sorting controls...");
tracing::debug!("Sorting controls...");
let mut controls = self
.controls
.clone()
@ -416,7 +412,7 @@ impl MenuBuilder {
.collect::<Vec<(String, ActionContainer)>>();
controls.sort_by_key(|(_, a)| a.position);
log::debug!("Creating menu...");
tracing::debug!("Creating menu...");
let message_handle = MessageHandle::new(message.channel_id, message.id);
let handle_lock = Arc::new(RwLock::new(message_handle));
@ -434,20 +430,19 @@ impl MenuBuilder {
owner: self.owner,
};
log::debug!("Storing menu to listeners...");
tracing::debug!("Storing menu to listeners...");
{
let mut listeners_lock = listeners.lock().await;
log::trace!("Listeners locked.");
tracing::trace!("Listeners locked.");
listeners_lock.insert(message_handle, Arc::new(Mutex::new(Box::new(menu))));
}
log::debug!("Adding controls...");
tracing::debug!("Adding controls...");
for (emoji, _) in controls {
message
.react(ctx, ReactionType::Unicode(emoji.clone()))
.await?;
}
log::debug!("Menu successfully created.");
Ok(handle_lock)
}

Loading…
Cancel
Save