feat: add file logging via LOG_FILE environment variable

This commit is contained in:
Ryan 2025-02-23 02:49:35 -05:00
parent 6e75c4e586
commit b1dbfa6110
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
3 changed files with 43 additions and 9 deletions

2
Cargo.lock generated
View File

@ -1180,6 +1180,8 @@ version = "0.2.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"azalea", "azalea",
"bevy_app",
"bevy_log",
"clap", "clap",
"futures", "futures",
"http-body-util", "http-body-util",

View File

@ -17,6 +17,8 @@ strip = true
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"
azalea = { git = "https://github.com/azalea-rs/azalea.git" } azalea = { git = "https://github.com/azalea-rs/azalea.git" }
bevy_app = "0"
bevy_log = "0"
clap = { version = "4", features = ["derive"] } clap = { version = "4", features = ["derive"] }
futures = "0" futures = "0"
http-body-util = "0" http-body-util = "0"

View File

@ -6,13 +6,27 @@ mod events;
mod http; mod http;
mod lua; 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 clap::Parser;
use commands::{CommandSource, register}; use commands::{CommandSource, register};
use events::handle_event; use events::handle_event;
use futures::lock::Mutex; use futures::lock::Mutex;
use mlua::{Function, Lua}; 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"; 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 script_path = args.script.unwrap_or(PathBuf::from(DEFAULT_SCRIPT_PATH));
let lua = Lua::new(); let lua = Lua::new();
let globals = lua.globals();
globals.set("script_path", &*script_path)?;
lua::register_functions(&lua, &globals)?;
lua.load( 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")), .expect(&(DEFAULT_SCRIPT_PATH.to_owned() + " should be in current directory")),
) )
.exec()?; .exec()?;
let globals = lua.globals();
let server = globals let server = globals
.get::<String>("Server") .get::<String>("Server")
.expect("Server should be in lua globals"); .expect("Server should be in lua globals");
@ -46,13 +62,27 @@ async fn main() -> anyhow::Result<()> {
.get::<String>("Username") .get::<String>("Username")
.expect("Username should be in lua globals"); .expect("Username should be in lua globals");
globals.set("script_path", script_path)?;
lua::register_functions(&lua, &globals)?;
let mut commands = CommandDispatcher::new(); let mut commands = CommandDispatcher::new();
register(&mut commands); 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_handler(handle_event)
.set_state(State { .set_state(State {
lua, lua,