1use crate::constants::{AGENT_DIR, AGENT_LOG_DIR, AGENT_LOG_ERROR, AGENT_LOG_INFO};
2use chrono::Local;
3use std::fs::{self, OpenOptions};
4use std::io::Write;
5
6#[macro_export]
8macro_rules! info {
9 ($($arg:tt)*) => {{
10 println!($($arg)*)
11 }};
12}
13
14#[macro_export]
16macro_rules! error {
17 ($($arg:tt)*) => {{
18 eprint!("[ERROR] ");
19 eprintln!($($arg)*)
20 }};
21}
22
23#[macro_export]
25macro_rules! usage {
26 ($($arg:tt)*) => {{
27 print!("Usage: ");
28 println!($($arg)*)
29 }};
30}
31
32#[macro_export]
34macro_rules! debug_log {
35 ($config:expr, $($arg:tt)*) => {{
36 if $config.is_verbose() {
37 println!($($arg)*)
38 }
39 }};
40}
41
42pub fn capitalize_first_letter(s: &str) -> String {
44 let mut chars = s.chars();
45 match chars.next() {
46 None => String::new(),
47 Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
48 }
49}
50
51pub fn write_info_log(context: &str, content: &str) {
54 let log_dir = crate::constants::data_root()
55 .join(AGENT_DIR)
56 .join(AGENT_LOG_DIR);
57
58 if let Err(e) = fs::create_dir_all(&log_dir) {
59 eprintln!("无法创建日志目录: {}", e);
60 return;
61 }
62
63 let log_file = log_dir.join(AGENT_LOG_INFO);
64
65 match OpenOptions::new().create(true).append(true).open(&log_file) {
66 Ok(mut file) => {
67 let timestamp = Local::now().format("%Y-%m-%d %H:%M:%S");
68 let log_entry = format!(
69 "\n========================================\n[{}] {}\n{}\n",
70 timestamp, context, content
71 );
72 if let Err(e) = file.write_all(log_entry.as_bytes()) {
73 eprintln!("写入信息日志失败: {}", e);
74 }
75 }
76 Err(e) => {
77 eprintln!("无法打开信息日志文件: {}", e);
78 }
79 }
80}
81
82pub fn write_error_log(context: &str, error: &str) {
85 let log_dir = crate::constants::data_root()
86 .join(AGENT_DIR)
87 .join(AGENT_LOG_DIR);
88
89 if let Err(e) = fs::create_dir_all(&log_dir) {
91 eprintln!("无法创建日志目录: {}", e);
92 return;
93 }
94
95 let log_file = log_dir.join(AGENT_LOG_ERROR);
96
97 match OpenOptions::new().create(true).append(true).open(&log_file) {
99 Ok(mut file) => {
100 let timestamp = Local::now().format("%Y-%m-%d %H:%M:%S");
101 let log_entry = format!(
102 "\n========================================\n[{}] {}\n错误详情:\n{}\n",
103 timestamp, context, error
104 );
105 if let Err(e) = file.write_all(log_entry.as_bytes()) {
106 eprintln!("写入错误日志失败: {}", e);
107 }
108 }
109 Err(e) => {
110 eprintln!("无法打开错误日志文件: {}", e);
111 }
112 }
113}