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