Skip to content

Commit

Permalink
fixed error and added output #301
Browse files Browse the repository at this point in the history
  • Loading branch information
hitenkoku committed Dec 19, 2021
1 parent 55c05c6 commit 7f9f234
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions src/detections/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,26 @@ lazy_static! {
"./hayabusa-logs/errorlog-{}.log",
Local::now().format("%Y%m%d_%H%M%S")
);
pub static ref ERROR_LOG_WRITER: BufWriter<File> =
get_error_file_writer(ERROR_LOG_PATH.to_string());
pub static ref ERROR_LOG_WRITER: Mutex<BufWriter<File>> =
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());
}

//対象のディレクトリが存在することを確認後、最初の定型文を追加して、ファイルのbufwriterを返す関数
fn get_error_file_writer(path_str: String) -> BufWriter<File> {
let path = Path::new(&path_str);
if !path.parent().unwrap().exists() {
create_dir(path.parent().unwrap());
create_dir(path.parent().unwrap()).ok();
}
// 1行目は必ず実行したコマンド情報を入れておく。
let ret = BufWriter::new(File::create(path).unwrap());
ret.write(format!(
"user input: {:?}\n",
format_args!("{:?}", env::args())
))
.ok();
ret.flush();
let mut ret = BufWriter::new(File::create(path).unwrap());
ret.write(format!("user input: {:?}\n", format_args!("{:?}", env::args())).as_bytes())
.ok();
ret.flush().ok();
return ret;
}

#[derive(Copy, Clone)]
/// エラーログに出力したエラー回数を保持した構造体
pub struct Counter {
pub count: u128,
Expand All @@ -75,7 +73,7 @@ impl Counter {
Counter { count: 0 }
}
/// エラーログに出力したエラー回数を1増やす
pub fn countup(self) {
pub fn countup(mut self) {
self.count += 1;
}
}
Expand Down Expand Up @@ -230,9 +228,7 @@ impl AlertMessage {
pub fn alert<W: Write>(w: &mut W, contents: String, error_log_flag: bool) -> io::Result<()> {
if error_log_flag {
ALERT_COUNT_IN_ERROR_LOG.lock().unwrap().countup();
let result = writeln!(ERROR_LOG_WRITER, "[ERROR] {}", contents);
ERROR_LOG_WRITER.flush();
result
writeln!(ERROR_LOG_WRITER.lock().unwrap(), "[ERROR] {}", contents)
} else {
writeln!(w, "[ERROR] {}", contents)
}
Expand All @@ -245,20 +241,22 @@ impl AlertMessage {

/// エラーログへのERRORメッセージの出力数を確認して、0であったらファイルを削除する。1以上あればエラーを書き出した旨を標準出力に表示する
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 remove_file(ERROR_LOG_PATH.to_string()).is_err() {
if remove_file(error_log_path_str).is_err() {
AlertMessage::alert(
&mut std::io::stderr().lock(),
format!(
"failed to remove file. filepath:{}",
ERROR_LOG_PATH.to_string()
),
format!("failed to remove file. filepath:{}", error_log_path_str),
false,
);
)
.ok();
}
return;
}
println!("E")
println!(format!(
"Generated error was output to {}. Please see the file for details",
error_log_path_str
));
}
}

Expand Down

0 comments on commit 7f9f234

Please sign in to comment.