1use chrono::Local;
2use std::fs::{self, OpenOptions};
3use std::io::Write;
4use std::path::PathBuf;
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 use colored::Colorize;
19 eprint!("{}", "[ERROR] ".red());
20 eprintln!($($arg)*)
21 }};
22}
23
24#[macro_export]
26macro_rules! usage {
27 ($($arg:tt)*) => {{
28 use colored::Colorize;
29 print!("{}", "💡 Usage: ".green());
30 println!($($arg)*)
31 }};
32}
33
34#[macro_export]
36macro_rules! debug_log {
37 ($config:expr, $($arg:tt)*) => {{
38 if $config.is_verbose() {
39 println!($($arg)*)
40 }
41 }};
42}
43
44#[allow(dead_code)]
46pub fn print_line() {
47 println!("- - - - - - - - - - - - - - - - - - - - - - -");
48}
49
50pub fn capitalize_first_letter(s: &str) -> String {
52 let mut chars = s.chars();
53 match chars.next() {
54 None => String::new(),
55 Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
56 }
57}
58
59pub fn write_error_log(context: &str, error: &str) {
62 let log_dir = dirs::home_dir()
63 .unwrap_or_else(|| PathBuf::from("."))
64 .join(".jdata")
65 .join("agent")
66 .join("logs");
67
68 if let Err(e) = fs::create_dir_all(&log_dir) {
70 eprintln!("无法创建日志目录: {}", e);
71 return;
72 }
73
74 let log_file = log_dir.join("error.log");
75
76 match OpenOptions::new().create(true).append(true).open(&log_file) {
78 Ok(mut file) => {
79 let timestamp = Local::now().format("%Y-%m-%d %H:%M:%S");
80 let log_entry = format!(
81 "\n========================================\n[{}] {}\n错误详情:\n{}\n",
82 timestamp, context, error
83 );
84 if let Err(e) = file.write_all(log_entry.as_bytes()) {
85 eprintln!("写入错误日志失败: {}", e);
86 }
87 }
88 Err(e) => {
89 eprintln!("无法打开错误日志文件: {}", e);
90 }
91 }
92}