1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! Forward log messages to logging webxdc
use crate::chat::ChatId;
use crate::config::Config;
use crate::context::Context;
use crate::events::EventType;
use crate::message::{Message, MsgId, Viewtype};
use crate::param::Param;
use crate::tools::time;
use crate::webxdc::StatusUpdateItem;
use async_channel::{self as channel, Receiver, Sender};
use serde_json::json;
use std::path::PathBuf;
use tokio::task;

#[derive(Debug)]
pub(crate) struct DebugLogging {
    /// The message containing the logging xdc
    pub(crate) msg_id: MsgId,
    /// Handle to the background task responsible for sending
    pub(crate) loop_handle: task::JoinHandle<()>,
    /// Channel that log events should be sent to.
    /// A background loop will receive and handle them.
    pub(crate) sender: Sender<DebugEventLogData>,
}

impl DebugLogging {
    pub(crate) fn log_event(&self, event: EventType) {
        let event_data = DebugEventLogData {
            time: time(),
            msg_id: self.msg_id,
            event,
        };

        self.sender.try_send(event_data).ok();
    }
}

/// Store all information needed to log an event to a webxdc.
pub struct DebugEventLogData {
    pub time: i64,
    pub msg_id: MsgId,
    pub event: EventType,
}

/// Creates a loop which forwards all log messages send into the channel to the associated
/// logging xdc.
pub async fn debug_logging_loop(context: &Context, events: Receiver<DebugEventLogData>) {
    while let Ok(DebugEventLogData {
        time,
        msg_id,
        event,
    }) = events.recv().await
    {
        match context
            .write_status_update_inner(
                &msg_id,
                &StatusUpdateItem {
                    payload: json!({
                        "event": event,
                        "time": time,
                    }),
                    info: None,
                    summary: None,
                    document: None,
                    uid: None,
                },
                time,
            )
            .await
        {
            Err(err) => {
                eprintln!("Can't log event to webxdc status update: {err:#}");
            }
            Ok(serial) => {
                if let Some(serial) = serial {
                    if !matches!(event, EventType::WebxdcStatusUpdate { .. }) {
                        context.emit_event(EventType::WebxdcStatusUpdate {
                            msg_id,
                            status_update_serial: serial,
                        });
                    }
                } else {
                    // This should not happen as the update has no `uid`.
                    error!(context, "Debug logging update is not created.");
                };
            }
        }
    }
}

/// Set message as new logging webxdc if filename and chat_id fit
pub async fn maybe_set_logging_xdc(
    context: &Context,
    msg: &Message,
    chat_id: ChatId,
) -> anyhow::Result<()> {
    maybe_set_logging_xdc_inner(
        context,
        msg.get_viewtype(),
        chat_id,
        msg.param.get_path(Param::File, context).unwrap_or_default(),
        msg.get_id(),
    )
    .await?;

    Ok(())
}

/// Set message as new logging webxdc if filename and chat_id fit
pub async fn maybe_set_logging_xdc_inner(
    context: &Context,
    viewtype: Viewtype,
    chat_id: ChatId,
    file: Option<PathBuf>,
    msg_id: MsgId,
) -> anyhow::Result<()> {
    if viewtype == Viewtype::Webxdc {
        if let Some(file) = file {
            if let Some(file_name) = file.file_name().and_then(|name| name.to_str()) {
                if file_name.starts_with("debug_logging")
                    && file_name.ends_with(".xdc")
                    && chat_id.is_self_talk(context).await?
                {
                    set_debug_logging_xdc(context, Some(msg_id)).await?;
                }
            }
        }
    }
    Ok(())
}

/// Set the webxdc contained in the msg as the current logging xdc on the context and save it to db
/// If id is a `None` value, disable debug logging
pub(crate) async fn set_debug_logging_xdc(ctx: &Context, id: Option<MsgId>) -> anyhow::Result<()> {
    match id {
        Some(msg_id) => {
            ctx.sql
                .set_raw_config(
                    Config::DebugLogging.as_ref(),
                    Some(msg_id.to_string().as_ref()),
                )
                .await?;
            {
                let debug_logging = &mut *ctx.debug_logging.write().expect("RwLock is poisoned");
                match debug_logging {
                    // Switch logging xdc
                    Some(debug_logging) => debug_logging.msg_id = msg_id,
                    // Bootstrap background loop for message forwarding
                    None => {
                        let (sender, debug_logging_recv) = channel::bounded(1000);
                        let loop_handle = {
                            let ctx = ctx.clone();
                            task::spawn(async move {
                                debug_logging_loop(&ctx, debug_logging_recv).await
                            })
                        };
                        *debug_logging = Some(DebugLogging {
                            msg_id,
                            loop_handle,
                            sender,
                        });
                    }
                }
            }
            info!(ctx, "replacing logging webxdc");
        }
        // Delete current debug logging
        None => {
            ctx.sql
                .set_raw_config(Config::DebugLogging.as_ref(), None)
                .await?;
            *ctx.debug_logging.write().expect("RwLock is poisoned") = None;
            info!(ctx, "removing logging webxdc");
        }
    }
    Ok(())
}