diff --git a/src-gui/src/models/cliModel.ts b/src-gui/src/models/cliModel.ts index 31423ca7..587c6197 100644 --- a/src-gui/src/models/cliModel.ts +++ b/src-gui/src/models/cliModel.ts @@ -18,3 +18,26 @@ export interface CliLog { [index: string]: unknown; }[]; } + +function isCliLog(log: unknown): log is CliLog { + return ( + typeof log === "object" && + log !== null && + "timestamp" in log && + "level" in log && + "fields" in log + ); +} + +export function parseCliLogString(log: string): CliLog | string { + try { + const parsed = JSON.parse(log); + if (isCliLog(parsed)) { + return parsed; + } else { + return log; + } + } catch (err) { + return log; + } +} diff --git a/src-gui/src/renderer/components/pages/history/table/SwapLogFileOpenButton.tsx b/src-gui/src/renderer/components/pages/history/table/SwapLogFileOpenButton.tsx index be69246d..b2e9b371 100644 --- a/src-gui/src/renderer/components/pages/history/table/SwapLogFileOpenButton.tsx +++ b/src-gui/src/renderer/components/pages/history/table/SwapLogFileOpenButton.tsx @@ -6,29 +6,38 @@ import { DialogTitle, } from "@material-ui/core"; import { ButtonProps } from "@material-ui/core/Button/Button"; -import { CliLog } from "models/cliModel"; +import { CliLog, parseCliLogString } from "models/cliModel"; +import { GetLogsResponse } from "models/tauriModel"; import { useState } from "react"; import PromiseInvokeButton from "renderer/components/PromiseInvokeButton"; +import { getLogsOfSwap } from "renderer/rpc"; import CliLogsBox from "../../../other/RenderedCliLog"; export default function SwapLogFileOpenButton({ swapId, ...props }: { swapId: string } & ButtonProps) { - const [logs, setLogs] = useState(null); + const [logs, setLogs] = useState<(CliLog | string)[] | null>(null); + + function onLogsReceived(response: GetLogsResponse) { + setLogs(response.logs.map(parseCliLogString)); + } return ( <> { - setLogs(data as CliLog[]); - }} - onInvoke={async () => { - throw new Error("Not implemented"); - }} + onSuccess={onLogsReceived} + onInvoke={() => getLogsOfSwap(swapId, false)} {...props} > - View log + View full logs + + getLogsOfSwap(swapId, true)} + {...props} + > + View redacted logs {logs && ( setLogs(null)} fullWidth maxWidth="lg"> @@ -37,7 +46,13 @@ export default function SwapLogFileOpenButton({ - + )} diff --git a/src-gui/src/renderer/rpc.ts b/src-gui/src/renderer/rpc.ts index ee878521..340eb516 100644 --- a/src-gui/src/renderer/rpc.ts +++ b/src-gui/src/renderer/rpc.ts @@ -5,6 +5,8 @@ import { BalanceResponse, BuyXmrArgs, BuyXmrResponse, + GetLogsArgs, + GetLogsResponse, GetSwapInfoResponse, MoneroRecoveryArgs, ResumeSwapArgs, @@ -132,3 +134,13 @@ export async function checkContextAvailability(): Promise { const available = await invokeNoArgs("is_context_available"); return available; } + +export async function getLogsOfSwap( + swapId: string, + redact: boolean, +): Promise { + return await invoke("get_logs", { + swap_id: swapId, + redact, + }); +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index a218905d..0d7fe2f4 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -3,8 +3,8 @@ use std::sync::Arc; use swap::cli::{ api::{ request::{ - BalanceArgs, BuyXmrArgs, GetHistoryArgs, GetSwapInfosAllArgs, MoneroRecoveryArgs, - ResumeSwapArgs, SuspendCurrentSwapArgs, WithdrawBtcArgs, + BalanceArgs, BuyXmrArgs, GetHistoryArgs, GetLogsArgs, GetSwapInfosAllArgs, + MoneroRecoveryArgs, ResumeSwapArgs, SuspendCurrentSwapArgs, WithdrawBtcArgs, }, tauri_bindings::{TauriContextStatusEvent, TauriEmitter, TauriHandle}, Context, ContextBuilder, @@ -168,6 +168,7 @@ pub fn run() { resume_swap, get_history, monero_recovery, + get_logs, suspend_current_swap, is_context_available, ]) @@ -206,6 +207,7 @@ tauri_command!(buy_xmr, BuyXmrArgs); tauri_command!(resume_swap, ResumeSwapArgs); tauri_command!(withdraw_btc, WithdrawBtcArgs); tauri_command!(monero_recovery, MoneroRecoveryArgs); +tauri_command!(get_logs, GetLogsArgs); // These commands require no arguments tauri_command!(suspend_current_swap, SuspendCurrentSwapArgs, no_args); diff --git a/swap/src/cli/api/request.rs b/swap/src/cli/api/request.rs index 85b8cf5c..26f9b178 100644 --- a/swap/src/cli/api/request.rs +++ b/swap/src/cli/api/request.rs @@ -359,9 +359,10 @@ impl Request for GetSwapInfosAllArgs { #[typeshare] #[derive(Serialize, Deserialize, Debug)] pub struct GetLogsArgs { + #[typeshare(serialized_as = "Option")] pub swap_id: Option, pub redact: bool, - #[typeshare(serialized_as = "string")] + #[typeshare(serialized_as = "Option")] pub logs_dir: Option, }