From b1dbfa6110d2f7ad84851dd0c4faf29bca2ad784 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Sun, 23 Feb 2025 02:49:35 -0500 Subject: [PATCH] feat: add file logging via LOG_FILE environment variable --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ src/main.rs | 48 +++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5ac63c8..8d6493c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1180,6 +1180,8 @@ version = "0.2.0" dependencies = [ "anyhow", "azalea", + "bevy_app", + "bevy_log", "clap", "futures", "http-body-util", diff --git a/Cargo.toml b/Cargo.toml index 7af3538..55eb68d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,8 @@ strip = true [dependencies] anyhow = "1" azalea = { git = "https://github.com/azalea-rs/azalea.git" } +bevy_app = "0" +bevy_log = "0" clap = { version = "4", features = ["derive"] } futures = "0" http-body-util = "0" diff --git a/src/main.rs b/src/main.rs index 9eaf58e..5c008df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,13 +6,27 @@ mod events; mod http; mod lua; -use azalea::{brigadier::prelude::CommandDispatcher, prelude::*}; +use azalea::{ + DefaultBotPlugins, DefaultPlugins, brigadier::prelude::CommandDispatcher, prelude::*, +}; +use bevy_app::PluginGroup; +use bevy_log::{ + LogPlugin, + tracing_subscriber::{Layer, fmt::layer}, +}; use clap::Parser; use commands::{CommandSource, register}; use events::handle_event; use futures::lock::Mutex; use mlua::{Function, Lua}; -use std::{collections::HashMap, net::SocketAddr, path::PathBuf, sync::Arc}; +use std::{ + collections::HashMap, + env, + fs::{OpenOptions, read_to_string}, + net::SocketAddr, + path::PathBuf, + sync::Arc, +}; const DEFAULT_SCRIPT_PATH: &str = "errornowatcher.lua"; @@ -32,13 +46,15 @@ async fn main() -> anyhow::Result<()> { let script_path = args.script.unwrap_or(PathBuf::from(DEFAULT_SCRIPT_PATH)); let lua = Lua::new(); + let globals = lua.globals(); + globals.set("script_path", &*script_path)?; + lua::register_functions(&lua, &globals)?; + lua.load( - std::fs::read_to_string(&script_path) + read_to_string(script_path) .expect(&(DEFAULT_SCRIPT_PATH.to_owned() + " should be in current directory")), ) .exec()?; - - let globals = lua.globals(); let server = globals .get::("Server") .expect("Server should be in lua globals"); @@ -46,13 +62,27 @@ async fn main() -> anyhow::Result<()> { .get::("Username") .expect("Username should be in lua globals"); - globals.set("script_path", script_path)?; - lua::register_functions(&lua, &globals)?; - let mut commands = CommandDispatcher::new(); register(&mut commands); - let Err(error) = ClientBuilder::new() + let Err(error) = ClientBuilder::new_without_plugins() + .add_plugins(DefaultPlugins.set(LogPlugin { + custom_layer: |_| { + env::var("LOG_FILE").ok().map(|log_file| { + layer() + .with_writer( + OpenOptions::new() + .append(true) + .create(true) + .open(log_file) + .expect("should have been able to open log file"), + ) + .boxed() + }) + }, + ..Default::default() + })) + .add_plugins(DefaultBotPlugins) .set_handler(handle_event) .set_state(State { lua,