zenyx-engine/engine/src/main.rs

97 lines
2.6 KiB
Rust

use core::{panic::set_panic_hook, repl::setup, splash};
use std::{fs::OpenOptions, io::BufWriter};
use colored::Colorize;
use tokio::runtime;
use tracing::level_filters::LevelFilter;
#[allow(unused_imports)]
use tracing::{debug, error, info, warn};
use tracing_subscriber::{Registry, fmt, layer::SubscriberExt};
use winit::event_loop::EventLoop;
pub mod cli;
pub mod core;
pub mod error;
pub mod metadata;
fn init_logger() {
let stdout_layer = fmt::layer()
.with_level(true)
.compact()
.pretty()
.log_internal_errors(false)
.without_time()
.with_thread_names(true);
let file_layer = fmt::layer()
.with_level(true)
.compact()
.with_ansi(false)
.log_internal_errors(false)
.without_time()
.with_writer(|| {
let file = OpenOptions::new()
.write(true)
.append(true)
.open("zenyx.log")
.unwrap_or_else(|_| {
eprintln!("Couldn't open log file, creating a new one.");
OpenOptions::new()
.write(true)
.create(true)
.open("zenyx.log")
.expect("Failed to create log file")
});
BufWriter::new(file)
})
.with_thread_names(true);
let subscriber = Registry::default()
.with(LevelFilter::DEBUG)
.with(stdout_layer)
.with(file_layer);
tracing::subscriber::set_global_default(subscriber).expect("Failed to set global subscriber");
}
#[tokio::main]
async fn main() {
init_logger();
cli::parse();
let sysinfo = crate::metadata::SystemMetadata::current();
// set_panic_hook();
setup();
splash::print_splash();
if !cfg!(debug_assertions) {
info!("{}", "Debug mode disabled".bright_blue());
set_panic_hook();
} else {
println!("{}", sysinfo.verbose_summary());
}
info!("Type 'help' for a list of commands.");
let repl_thread = std::thread::spawn(|| {
let rt = match runtime::Builder::new_current_thread().enable_all().build() {
Ok(rt) => rt,
Err(e) => {
error!("A fatal error has occured: {e}");
std::process::exit(1)
}
};
rt.block_on(core::repl::input::handle_repl())
});
match EventLoop::new() {
Ok(event_loop) => {
core::render::init_renderer(event_loop);
}
Err(e) => {
error!("{e}")
}
};
if let Err(_) = repl_thread.join() {
error!("REPL thread panicked");
}
}