Revert "Feature/output errorlog#301" (#314)
This commit is contained in:
@@ -68,7 +68,6 @@ fn build_app<'a>() -> ArgMatches<'a> {
|
||||
-t --thread-number=[NUMBER] 'Thread number (default: optimal number for performance)'
|
||||
-s --statistics 'Prints statistics of event IDs'
|
||||
-q --quiet 'Quiet mode. Do not display the launch banner'
|
||||
-Q --quiet-errors 'Quiet errors mode. Do not display errors or save error logs'
|
||||
--contributors 'Prints the list of contributors'";
|
||||
App::new(&program)
|
||||
.about("Hayabusa: Aiming to be the world's greatest Windows event log analysis tool!")
|
||||
@@ -141,7 +140,7 @@ impl TargetEventTime {
|
||||
Err(err) => {
|
||||
AlertMessage::alert(
|
||||
&mut std::io::stderr().lock(),
|
||||
format!("start-timeline field: {}", err),
|
||||
format!("starttimeline field: {}", err),
|
||||
)
|
||||
.ok();
|
||||
None
|
||||
@@ -158,7 +157,7 @@ impl TargetEventTime {
|
||||
Err(err) => {
|
||||
AlertMessage::alert(
|
||||
&mut std::io::stderr().lock(),
|
||||
format!("end-timeline field: {}", err),
|
||||
format!("endtimeline field: {}", err),
|
||||
)
|
||||
.ok();
|
||||
None
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
extern crate csv;
|
||||
|
||||
use crate::detections::print::AlertMessage;
|
||||
use crate::detections::print::ERROR_LOG_PATH;
|
||||
use crate::detections::print::MESSAGES;
|
||||
use crate::detections::rule;
|
||||
use crate::detections::rule::AggResult;
|
||||
@@ -12,8 +11,6 @@ use crate::yaml::ParseYaml;
|
||||
use hashbrown;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::BufWriter;
|
||||
use tokio::{runtime::Runtime, spawn, task::JoinHandle};
|
||||
|
||||
use std::sync::Arc;
|
||||
@@ -61,12 +58,7 @@ impl Detection {
|
||||
rulefile_loader.read_dir(rulespath.unwrap_or(DIRPATH_RULES), &level, exclude_ids);
|
||||
if result_readdir.is_err() {
|
||||
AlertMessage::alert(
|
||||
&mut BufWriter::new(
|
||||
OpenOptions::new()
|
||||
.append(true)
|
||||
.open(ERROR_LOG_PATH.to_string())
|
||||
.unwrap(),
|
||||
),
|
||||
&mut std::io::stderr().lock(),
|
||||
format!("{}", result_readdir.unwrap_err()),
|
||||
)
|
||||
.ok();
|
||||
|
||||
@@ -2,20 +2,13 @@ extern crate lazy_static;
|
||||
use crate::detections::configs;
|
||||
use crate::detections::utils;
|
||||
use crate::detections::utils::get_serde_number_to_string;
|
||||
use chrono::{DateTime, Local, TimeZone, Utc};
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use lazy_static::lazy_static;
|
||||
use linecount::count_lines;
|
||||
use regex::Regex;
|
||||
use serde_json::Value;
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::fs::create_dir;
|
||||
use std::fs::remove_file;
|
||||
use std::fs::File;
|
||||
use std::io::BufWriter;
|
||||
use std::io::{self, Write};
|
||||
use std::path::Path;
|
||||
use std::sync::Mutex;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -39,15 +32,6 @@ pub struct AlertMessage {}
|
||||
lazy_static! {
|
||||
pub static ref MESSAGES: Mutex<Message> = Mutex::new(Message::new());
|
||||
pub static ref ALIASREGEX: Regex = Regex::new(r"%[a-zA-Z0-9-_]+%").unwrap();
|
||||
pub static ref ERROR_LOG_PATH: String = format!(
|
||||
"./logs/errorlog-{}.log",
|
||||
Local::now().format("%Y%m%d_%H%M%S")
|
||||
);
|
||||
pub static ref QUIET_ERRORS_FLAG: bool = configs::CONFIG
|
||||
.read()
|
||||
.unwrap()
|
||||
.args
|
||||
.is_present("quiet-errors");
|
||||
}
|
||||
|
||||
impl Message {
|
||||
@@ -196,68 +180,11 @@ impl Message {
|
||||
}
|
||||
|
||||
impl AlertMessage {
|
||||
//対象のディレクトリが存在することを確認後、最初の定型文を追加して、ファイルのbufwriterを返す関数
|
||||
pub fn create_error_log(path_str: String) {
|
||||
let path = Path::new(&path_str);
|
||||
if !path.parent().unwrap().exists() {
|
||||
create_dir(path.parent().unwrap()).ok();
|
||||
}
|
||||
// 1行目は必ず実行したコマンド情報を入れておく。
|
||||
let mut ret = BufWriter::new(File::create(path).unwrap());
|
||||
|
||||
ret.write(
|
||||
format!(
|
||||
"user input: {:?}\n",
|
||||
format_args!(
|
||||
"{}",
|
||||
env::args()
|
||||
.map(|arg| arg)
|
||||
.collect::<Vec<String>>()
|
||||
.join(" ")
|
||||
)
|
||||
)
|
||||
.as_bytes(),
|
||||
)
|
||||
.unwrap();
|
||||
ret.flush().ok();
|
||||
}
|
||||
|
||||
/// ERRORメッセージを表示する関数。error_log_flagでfalseの場合は外部へのエラーログの書き込みは行わずに指定されたwを用いた出力のみ行う。trueの場合はwを用いた出力を行わずにエラーログへの出力を行う
|
||||
pub fn alert<W: Write>(w: &mut W, contents: String) -> io::Result<()> {
|
||||
if *QUIET_ERRORS_FLAG {
|
||||
writeln!(w, "[ERROR] {}", contents)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
writeln!(w, "[ERROR] {}", contents)
|
||||
}
|
||||
|
||||
// WARNメッセージを表示する関数
|
||||
pub fn warn<W: Write>(w: &mut W, contents: String) -> io::Result<()> {
|
||||
if *QUIET_ERRORS_FLAG {
|
||||
writeln!(w, "[WARN] {}", contents)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// エラーログへのERRORメッセージの出力数を確認して、0であったらファイルを削除する。1以上あればエラーを書き出した旨を標準出力に表示する
|
||||
pub fn output_error_log_exist() {
|
||||
let error_log_path_str = ERROR_LOG_PATH.to_string();
|
||||
// 1行しかなかった場合は最初に書いたコマンド情報のみと判断して削除する
|
||||
if count_lines(File::open(&error_log_path_str).unwrap()).unwrap() == 1 {
|
||||
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_str),
|
||||
)
|
||||
.ok();
|
||||
}
|
||||
return;
|
||||
}
|
||||
println!(
|
||||
"Generated error was output to {}. Please see the file for details.",
|
||||
&error_log_path_str
|
||||
);
|
||||
writeln!(w, "[WARN] {}", contents)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use crate::detections::print::AlertMessage;
|
||||
use crate::detections::print::ERROR_LOG_PATH;
|
||||
use crate::detections::rule::AggResult;
|
||||
use crate::detections::rule::AggregationParseInfo;
|
||||
use crate::detections::rule::Message;
|
||||
@@ -7,8 +6,6 @@ use crate::detections::rule::RuleNode;
|
||||
use chrono::{DateTime, TimeZone, Utc};
|
||||
use hashbrown::HashMap;
|
||||
use serde_json::Value;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::BufWriter;
|
||||
use std::num::ParseIntError;
|
||||
use std::path::Path;
|
||||
|
||||
@@ -186,12 +183,7 @@ impl TimeFrameInfo {
|
||||
tnum.retain(|c| c != 'd');
|
||||
} else {
|
||||
AlertMessage::alert(
|
||||
&mut BufWriter::new(
|
||||
OpenOptions::new()
|
||||
.append(true)
|
||||
.open(ERROR_LOG_PATH.to_string())
|
||||
.unwrap(),
|
||||
),
|
||||
&mut std::io::stderr().lock(),
|
||||
format!("Timeframe is invalid. Input value:{}", value),
|
||||
)
|
||||
.ok();
|
||||
@@ -223,13 +215,8 @@ pub fn get_sec_timeframe(timeframe: &Option<TimeFrameInfo>) -> Option<i64> {
|
||||
}
|
||||
Err(err) => {
|
||||
AlertMessage::alert(
|
||||
&mut BufWriter::new(
|
||||
OpenOptions::new()
|
||||
.append(true)
|
||||
.open(ERROR_LOG_PATH.to_string())
|
||||
.unwrap(),
|
||||
),
|
||||
format!("Timeframe number is invalid. timeframe: {}", err),
|
||||
&mut std::io::stderr().lock(),
|
||||
format!("Timeframe number is invalid. timeframe.{}", err),
|
||||
)
|
||||
.ok();
|
||||
return Option::None;
|
||||
|
||||
Reference in New Issue
Block a user