use std::str::FromStr;
use async_trait::async_trait;
use bevy_brp_mcp_macros::ParamStruct;
use bevy_brp_mcp_macros::ResultStruct;
use schemars::JsonSchema;
use serde::Deserialize;
use serde::Serialize;
use super::tracing::TracingLevel;
use crate::error::Error;
use crate::error::Result;
use crate::tool::ToolFn;
#[derive(Clone, Deserialize, Serialize, JsonSchema, ParamStruct)]
pub struct SetTracingLevelParams {
pub level: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, ResultStruct)]
pub struct SetTracingLevelResult {
#[serde(rename = "tracing_level")]
#[to_metadata]
level: String,
#[serde(rename = "tracing_log_file")]
#[to_metadata]
log_file: String,
#[to_message(message_template = "Set tracing level to {tracing_level}")]
message_template: String,
}
pub struct SetTracingLevel;
#[async_trait]
impl ToolFn for SetTracingLevel {
type Output = SetTracingLevelResult;
type Params = SetTracingLevelParams;
async fn handle_impl(&self, params: SetTracingLevelParams) -> Result<SetTracingLevelResult> {
let tracing_level = match TracingLevel::from_str(¶ms.level) {
Ok(level) => level,
Err(e) => {
return Err(Error::invalid(
"tracing level",
format!(
"{}: {e}. Valid levels are: error, warn, info, debug, trace",
params.level
),
)
.into());
},
};
TracingLevel::set_tracing_level(tracing_level);
let log_path = TracingLevel::get_trace_log_path();
let log_file = log_path.to_string_lossy().to_string();
Ok(SetTracingLevelResult::new(
tracing_level.as_str().to_string(),
log_file,
))
}
}