1use core::fmt::Debug;
2use std::{fs::{File, self}, io::Write};
3
4use enum_iterator::all;
5
6use crate::{config::Config, time::Time, head::{LineType, LogTag}};
7pub struct Log;
8
9impl Log {
10 pub fn create_log_dir() {
11 let path = "log";
12 match fs::remove_dir_all(path) {
13 _ => { fs::create_dir(path).unwrap(); }
14 }
15 File::create(Log::panic_file()).unwrap();
16 File::create(Log::error_file()).unwrap();
17 }
18
19 pub fn create_dir(kind:LineType) {
20 if Config::log_off() { return; }
21 let path = format!("log/{:?}",kind);
22 fs::create_dir(path).unwrap();
23 for x in all::<LogTag>() {
24 Log::new(kind, &x);
25 }
26 }
27
28 pub fn create_file(path:String) {
29 if Config::log_off() { return; }
30 File::create(path).unwrap();
31 }
32
33 pub fn new<T:Debug>(kind:LineType,name:&T) {
34 let path = Log::get_path(kind,name);
35 Log::create_file(path);
36 }
37
38 pub fn add<T:Debug>(str:String,kind:LineType,name:&T) {
39 if Config::log_off() { return; }
40 let path = Log::get_path(kind,name);
41 let s = format!("{}|{}\n",Time::now(),str);
42 let mut f = File::options().append(true).open(path).unwrap();
43 f.write(s.as_bytes()).unwrap();
44 }
45
46 pub fn panic_file() -> String {
47 String::from("log/panic.log")
48 }
49
50 pub fn error(str:String) {
51 let s = format!("{}|{}\n",Time::now(),str);
52 let mut f = File::options().append(true).open(Log::error_file()).unwrap();
53 f.write(s.as_bytes()).unwrap();
54 }
55
56 pub fn error_file() -> String {
57 String::from("log/error.log")
58 }
59
60}
61
62impl Log {
63 fn get_path<T: Debug>(kind:LineType,name:&T) -> String {
64 format!("log/{:?}/{:?}.log",kind,name)
65 }
66}