Skip to content

Commit

Permalink
feat: change log file format
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi committed Dec 19, 2021
1 parent f095d1b commit bf5680d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ tauri-build = { version = "1.0.0-beta.4" }

[dependencies]
dirs = "4.0.0"
chrono = "0.4.19"
serde_json = "1.0"
serde_yaml = "0.8"
serde = { version = "1.0", features = ["derive"] }
Expand Down
32 changes: 16 additions & 16 deletions src-tauri/src/utils/init.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
extern crate serde_yaml;

use chrono::Local;
use log::LevelFilter;
use log4rs::append::console::ConsoleAppender;
use log4rs::append::file::FileAppender;
use log4rs::config::{Appender, Config, Root};
use log4rs::encode::pattern::PatternEncoder;
use std::fs;
use std::fs::{self, File};
use std::io::Write;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};
use tauri::PackageInfo;

use crate::utils::{app_home_dir, app_resources_dir};

/// initialize this instance's log file
fn init_log(log_dir: &PathBuf) {
let log_time = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let log_file = format!("log-{:?}", log_time);
let local_time = Local::now().format("%Y-%m-%d_%H:%M:%S").to_string();
let log_file = format!("{}.log", local_time);
let log_file = log_dir.join(log_file);

let stdout = ConsoleAppender::builder().build();
let time_format = "{d(%Y-%m-%d %H:%M:%S)} - {m}{n}";
let stdout = ConsoleAppender::builder()
.encoder(Box::new(PatternEncoder::new(time_format)))
.build();
let tofile = FileAppender::builder()
.encoder(Box::new(PatternEncoder::new(
"{d(%Y-%m-%d %H:%M:%S)} - {m}{n}",
)))
.encoder(Box::new(PatternEncoder::new(time_format)))
.build(log_file)
.unwrap();

Expand Down Expand Up @@ -62,11 +60,13 @@ fn init_config_file(app_dir: &PathBuf, res_dir: &PathBuf) {
fs::copy(clash_tmpl, clash_path).unwrap();
} else {
// make sure that the config.yaml not null
let content = "mixed-port: 7890\nallow-lan: false\n".as_bytes();
fs::File::create(clash_path)
.unwrap()
.write(content)
.unwrap();
let content = b"\
mixed-port: 7890\n\
log-level: info\n\
allow-lan: false\n\
external-controller: 127.0.0.1:9090\n\
secret: \"\"\n";
File::create(clash_path).unwrap().write(content).unwrap();
}
}

Expand Down

0 comments on commit bf5680d

Please sign in to comment.