fixed error and added output #301

This commit is contained in:
DustInDark
2021-12-19 14:17:25 +09:00
parent 55c05c6d38
commit 7f9f2349f2

View File

@@ -43,8 +43,8 @@ lazy_static! {
"./hayabusa-logs/errorlog-{}.log", "./hayabusa-logs/errorlog-{}.log",
Local::now().format("%Y%m%d_%H%M%S") Local::now().format("%Y%m%d_%H%M%S")
); );
pub static ref ERROR_LOG_WRITER: BufWriter<File> = pub static ref ERROR_LOG_WRITER: Mutex<BufWriter<File>> =
get_error_file_writer(ERROR_LOG_PATH.to_string()); Mutex::new(get_error_file_writer(ERROR_LOG_PATH.to_string()));
pub static ref ALERT_COUNT_IN_ERROR_LOG: Mutex<Counter> = Mutex::new(Counter::new()); pub static ref ALERT_COUNT_IN_ERROR_LOG: Mutex<Counter> = Mutex::new(Counter::new());
} }
@@ -52,19 +52,17 @@ lazy_static! {
fn get_error_file_writer(path_str: String) -> BufWriter<File> { fn get_error_file_writer(path_str: String) -> BufWriter<File> {
let path = Path::new(&path_str); let path = Path::new(&path_str);
if !path.parent().unwrap().exists() { if !path.parent().unwrap().exists() {
create_dir(path.parent().unwrap()); create_dir(path.parent().unwrap()).ok();
} }
// 1行目は必ず実行したコマンド情報を入れておく。 // 1行目は必ず実行したコマンド情報を入れておく。
let ret = BufWriter::new(File::create(path).unwrap()); let mut ret = BufWriter::new(File::create(path).unwrap());
ret.write(format!( ret.write(format!("user input: {:?}\n", format_args!("{:?}", env::args())).as_bytes())
"user input: {:?}\n", .ok();
format_args!("{:?}", env::args()) ret.flush().ok();
))
.ok();
ret.flush();
return ret; return ret;
} }
#[derive(Copy, Clone)]
/// エラーログに出力したエラー回数を保持した構造体 /// エラーログに出力したエラー回数を保持した構造体
pub struct Counter { pub struct Counter {
pub count: u128, pub count: u128,
@@ -75,7 +73,7 @@ impl Counter {
Counter { count: 0 } Counter { count: 0 }
} }
/// エラーログに出力したエラー回数を1増やす /// エラーログに出力したエラー回数を1増やす
pub fn countup(self) { pub fn countup(mut self) {
self.count += 1; self.count += 1;
} }
} }
@@ -230,9 +228,7 @@ impl AlertMessage {
pub fn alert<W: Write>(w: &mut W, contents: String, error_log_flag: bool) -> io::Result<()> { pub fn alert<W: Write>(w: &mut W, contents: String, error_log_flag: bool) -> io::Result<()> {
if error_log_flag { if error_log_flag {
ALERT_COUNT_IN_ERROR_LOG.lock().unwrap().countup(); ALERT_COUNT_IN_ERROR_LOG.lock().unwrap().countup();
let result = writeln!(ERROR_LOG_WRITER, "[ERROR] {}", contents); writeln!(ERROR_LOG_WRITER.lock().unwrap(), "[ERROR] {}", contents)
ERROR_LOG_WRITER.flush();
result
} else { } else {
writeln!(w, "[ERROR] {}", contents) writeln!(w, "[ERROR] {}", contents)
} }
@@ -245,20 +241,22 @@ impl AlertMessage {
/// エラーログへのERRORメッセージの出力数を確認して、0であったらファイルを削除する。1以上あればエラーを書き出した旨を標準出力に表示する /// エラーログへのERRORメッセージの出力数を確認して、0であったらファイルを削除する。1以上あればエラーを書き出した旨を標準出力に表示する
pub fn output_error_log_exist() { pub fn output_error_log_exist() {
let error_log_path_str = ERROR_LOG_PATH.to_string();
if ALERT_COUNT_IN_ERROR_LOG.lock().unwrap().count == 0 { if ALERT_COUNT_IN_ERROR_LOG.lock().unwrap().count == 0 {
if remove_file(ERROR_LOG_PATH.to_string()).is_err() { if remove_file(error_log_path_str).is_err() {
AlertMessage::alert( AlertMessage::alert(
&mut std::io::stderr().lock(), &mut std::io::stderr().lock(),
format!( format!("failed to remove file. filepath:{}", error_log_path_str),
"failed to remove file. filepath:{}",
ERROR_LOG_PATH.to_string()
),
false, false,
); )
.ok();
} }
return; return;
} }
println!("E") println!(format!(
"Generated error was output to {}. Please see the file for details",
error_log_path_str
));
} }
} }