1*7b32f508SLoGin use clap::Parser;
2*7b32f508SLoGin use logmonitor::app::{App, AppResult};
3*7b32f508SLoGin use logmonitor::command::{self, CommandLineArgs};
4*7b32f508SLoGin use logmonitor::constant::CMD_ARGS;
5*7b32f508SLoGin use logmonitor::event::{Event, EventHandler};
6*7b32f508SLoGin use logmonitor::handler::{handle_backend_events, handle_key_events};
7*7b32f508SLoGin use logmonitor::logging::LoggingInitResult;
8*7b32f508SLoGin use logmonitor::tui::Tui;
9*7b32f508SLoGin use ratatui::backend::CrosstermBackend;
10*7b32f508SLoGin use ratatui::Terminal;
11*7b32f508SLoGin use std::io;
12*7b32f508SLoGin
13*7b32f508SLoGin extern crate log;
14*7b32f508SLoGin
main() -> AppResult<()>15*7b32f508SLoGin fn main() -> AppResult<()> {
16*7b32f508SLoGin let command_line_args = command::CommandLineArgs::parse();
17*7b32f508SLoGin *CMD_ARGS.write().unwrap() = Some(command_line_args.clone());
18*7b32f508SLoGin println!("{:?}", command_line_args);
19*7b32f508SLoGin prepare_env();
20*7b32f508SLoGin
21*7b32f508SLoGin let logging_init_result = logmonitor::logging::init(&command_line_args);
22*7b32f508SLoGin if !command_line_args.tui {
23*7b32f508SLoGin return start_headless_app(command_line_args, logging_init_result);
24*7b32f508SLoGin } else {
25*7b32f508SLoGin return start_tui_app(command_line_args, logging_init_result);
26*7b32f508SLoGin }
27*7b32f508SLoGin }
28*7b32f508SLoGin
prepare_env()29*7b32f508SLoGin fn prepare_env() {
30*7b32f508SLoGin // 创建日志文件夹
31*7b32f508SLoGin let p = CMD_ARGS.read().unwrap().clone();
32*7b32f508SLoGin let log_dir = p.unwrap().log_dir;
33*7b32f508SLoGin std::fs::create_dir_all(log_dir).expect("Failed to create log directory.");
34*7b32f508SLoGin }
35*7b32f508SLoGin
36*7b32f508SLoGin /// 启动无界面应用
start_headless_app( cmdargs: CommandLineArgs, _logging_init_result: LoggingInitResult, ) -> AppResult<()>37*7b32f508SLoGin fn start_headless_app(
38*7b32f508SLoGin cmdargs: CommandLineArgs,
39*7b32f508SLoGin _logging_init_result: LoggingInitResult,
40*7b32f508SLoGin ) -> AppResult<()> {
41*7b32f508SLoGin let mut app = App::new("DragonOS Log Monitor");
42*7b32f508SLoGin let events = EventHandler::new(250);
43*7b32f508SLoGin let _app_backend = logmonitor::backend::AppBackend::new(cmdargs.clone(), events.sender());
44*7b32f508SLoGin
45*7b32f508SLoGin while app.running {
46*7b32f508SLoGin match events.next()? {
47*7b32f508SLoGin Event::Tick => app.tick(),
48*7b32f508SLoGin Event::Key(key_event) => handle_key_events(key_event, &mut app)?,
49*7b32f508SLoGin Event::Mouse(_) => {}
50*7b32f508SLoGin Event::Resize(_, _) => {}
51*7b32f508SLoGin Event::Backend(e) => {
52*7b32f508SLoGin handle_backend_events(e, &mut app)?;
53*7b32f508SLoGin }
54*7b32f508SLoGin }
55*7b32f508SLoGin }
56*7b32f508SLoGin println!("Headless mode not implemented yet.");
57*7b32f508SLoGin Ok(())
58*7b32f508SLoGin }
59*7b32f508SLoGin
60*7b32f508SLoGin /// 启动TUI应用
start_tui_app( cmdargs: CommandLineArgs, logging_init_result: LoggingInitResult, ) -> AppResult<()>61*7b32f508SLoGin fn start_tui_app(
62*7b32f508SLoGin cmdargs: CommandLineArgs,
63*7b32f508SLoGin logging_init_result: LoggingInitResult,
64*7b32f508SLoGin ) -> AppResult<()> {
65*7b32f508SLoGin // Create an application.
66*7b32f508SLoGin let mut app = App::new("DragonOS Log Monitor");
67*7b32f508SLoGin if let Some(receiver) = logging_init_result.tui_receiver {
68*7b32f508SLoGin app.set_backend_log_receiver(receiver);
69*7b32f508SLoGin }
70*7b32f508SLoGin
71*7b32f508SLoGin // Initialize the terminal user interface.
72*7b32f508SLoGin let backend = CrosstermBackend::new(io::stderr());
73*7b32f508SLoGin let terminal = Terminal::new(backend)?;
74*7b32f508SLoGin let events = EventHandler::new(250);
75*7b32f508SLoGin let mut tui = Tui::new(terminal, events);
76*7b32f508SLoGin tui.init()?;
77*7b32f508SLoGin let _app_backend = logmonitor::backend::AppBackend::new(cmdargs.clone(), tui.events.sender());
78*7b32f508SLoGin
79*7b32f508SLoGin // Start the main loop.
80*7b32f508SLoGin while app.running {
81*7b32f508SLoGin // Render the user interface.
82*7b32f508SLoGin tui.draw(&mut app)?;
83*7b32f508SLoGin // Handle events.
84*7b32f508SLoGin match tui.events.next()? {
85*7b32f508SLoGin Event::Tick => app.tick(),
86*7b32f508SLoGin Event::Key(key_event) => handle_key_events(key_event, &mut app)?,
87*7b32f508SLoGin Event::Mouse(_) => {}
88*7b32f508SLoGin Event::Resize(_, _) => {}
89*7b32f508SLoGin Event::Backend(e) => {
90*7b32f508SLoGin handle_backend_events(e, &mut app)?;
91*7b32f508SLoGin }
92*7b32f508SLoGin }
93*7b32f508SLoGin }
94*7b32f508SLoGin
95*7b32f508SLoGin // Exit the user interface.
96*7b32f508SLoGin tui.exit()?;
97*7b32f508SLoGin Ok(())
98*7b32f508SLoGin }
99