1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
extern crate chrono; // time
extern crate colored; // color

use chrono::prelude::*;
use std::fs::File;
use std::path::Path;
use std::{env, fs};
#[path = "./terminal.rs"]
mod terminal;

pub struct IOLogger {
    pkg: String,
    name: String,
    file: String,
    terminal: terminal::TerminalLogger,
}

impl IOLogger {
    fn new(name: &str, file: &str, log_name: Option<&str>) -> IOLogger {
        let pkg = log_name.unwrap_or(env!("CARGO_PKG_NAME"));
        if !file_exists(file) {
            let _file = create_file(file);
        }
        return IOLogger {
            pkg: pkg.to_string(),
            name: name.to_string(),
            file: file.to_string(),
            terminal: terminal::get(name),
        };
    }

    pub fn get_name(&self) -> String {
        return format!("{}", self.name);
    }

    pub fn get_file(&self) -> String {
        return format!("{}", self.file);
    }

    pub fn append_log(&self, content: &str) {
        let mut contents: std::string::String = read_file(&self.file, "Something went wrong...");
        contents = contents + "\n" + content;
        write_file(&self.file, &contents, "Something went wrong...");
    }
    pub fn info(&self, cont: &str) {
        let formatted_str = format!(
            "{} [{}:{}] {}: {}",
            get_current_time(),
            self.pkg,
            self.name,
            "INFO",
            cont.to_string()
        );
        self.terminal.info(cont);
        self.append_log(&formatted_str);
    }
    pub fn error(&self, cont: &str) {
        let formatted_str = format!(
            "{} [{}:{}] {}: {}",
            get_current_time(),
            self.pkg,
            self.name,
            "ERROR",
            cont.to_string()
        );
        self.terminal.error(cont);
        self.append_log(&formatted_str);
    }
    pub fn fatal(&self, cont: &str) {
        let formatted_str = format!(
            "{} [{}:{}] {}: {}",
            get_current_time(),
            self.pkg,
            self.name,
            "FATAL",
            cont.to_string()
        );
        self.terminal.fatal(cont);
        self.append_log(&formatted_str);
    }
    pub fn critical(&self, cont: &str) {
        let formatted_str = format!(
            "{} [{}:{}] {}: {}",
            get_current_time(),
            self.pkg,
            self.name,
            "CRITICAL",
            cont.to_string()
        );
        self.terminal.critical(cont);
        self.append_log(&formatted_str);
    }
    pub fn debug(&self, cont: &str) {
        let formatted_str = format!(
            "{} [{}:{}] {}: {}",
            get_current_time(),
            self.pkg,
            self.name,
            "DEBUG",
            cont.to_string()
        );
        self.terminal.debug(cont);
        self.append_log(&formatted_str);
    }
    pub fn warn(&self, cont: &str) {
        let formatted_str = format!(
            "{} [{}:{}] {}: {}",
            get_current_time(),
            self.pkg,
            self.name,
            "WARN",
            cont.to_string()
        );
        self.terminal.warn(cont);
        self.append_log(&formatted_str);
    }
}

pub fn get(name: &str, file: &str) -> IOLogger {
    let io_logger = IOLogger::new(name, file, None);
    return io_logger;
}

pub fn get_current_time() -> String {
    let local_time = Local::now();
    return local_time.format("%Y-%m-%d %H:%M:%S").to_string();
}

pub fn read_file(path: &str, expect: &str) -> std::string::String {
    return fs::read_to_string(path).expect(expect);
}

pub fn write_file(path: &str, content: &str, expect: &str) -> bool {
    fs::write(path, content).expect(expect);
    return true;
}

pub fn file_exists(p: &str) -> bool {
    let path = Path::new(p);
    if path.exists() {
        return true;
    } else {
        return false;
    }
}

pub fn create_file(file: &str) -> std::io::Result<()> {
    File::create(file)?;
    Ok(())
}