Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: output save file msg/ output red error msg #1506

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/detections/message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate lazy_static;
use super::configs::EventKeyAliasConfig;
use super::utils::remove_sp_char;
use super::utils::{get_writable_color, remove_sp_char};
use crate::detections::configs::CURRENT_EXE_PATH;
use crate::detections::field_data_map::{convert_field_data, FieldDataMap, FieldDataMapKey};
use crate::detections::rule::AggResult;
Expand All @@ -25,7 +25,7 @@ use std::fs::{create_dir, File};
use std::io::{self, BufWriter, Write};
use std::path::Path;
use std::sync::Mutex;
use termcolor::{BufferWriter, ColorChoice};
use termcolor::{BufferWriter, Color, ColorChoice};

/*
* This struct express log record
Expand Down Expand Up @@ -404,7 +404,7 @@ pub fn get_event_time(event_record: &Value, json_input_flag: bool) -> Option<Dat

impl AlertMessage {
///対象のディレクトリが存在することを確認後、最初の定型文を追加して、ファイルのbufwriterを返す関数
pub fn create_error_log(quiet_errors_flag: bool) {
pub fn create_error_log(quiet_errors_flag: bool, no_color: bool) {
if quiet_errors_flag {
return;
}
Expand Down Expand Up @@ -433,8 +433,15 @@ impl AlertMessage {
error_logs.iter().for_each(|error_log| {
writeln!(error_log_writer, "{error_log}").ok();
});
println!("Errors were generated. Please check {file_path} for details.");
println!();
let msg = format!("Errors were generated. Please check {file_path} for details.");
write_color_buffer(
&BufferWriter::stdout(ColorChoice::Always),
get_writable_color(Some(Color::Rgb(255, 0, 0)), no_color),
msg.as_str(),
true,
)
.ok();
}

/// ERRORメッセージを表示する関数
Expand Down
34 changes: 26 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,31 @@ impl App {
} else {
HashSet::default()
};

let no_color = stored_static.common_options.no_color;
let output_saved_file =
|output_path: &Option<PathBuf>, message: &str, html_report_flag: &bool| {
if let Some(path) = output_path {
if let Ok(metadata) = fs::metadata(path) {
let output_saved_str = format!(
"{message}: {} ({})",
path.display(),
ByteSize::b(metadata.len()).to_string_as(false)
);
let output_saved_str = format!("{message}:");
write_color_buffer(
&BufferWriter::stdout(ColorChoice::Always),
get_writable_color(Some(Color::Rgb(0, 255, 0)), no_color),
&output_saved_str,
false,
)
.ok();
write_color_buffer(
&BufferWriter::stdout(ColorChoice::Always),
None,
&format!(
" {} ({})",
path.display(),
ByteSize::b(metadata.len()).to_string_as(false)
),
true,
)
.ok();
println!();
output_and_data_stack_for_html(
&output_saved_str,
"General Overview {#general_overview}",
Expand Down Expand Up @@ -364,7 +379,6 @@ impl App {
"Saved file",
&stored_static.html_report_flag,
);
println!();
if stored_static.html_report_flag {
let html_str = HTML_REPORTER.read().unwrap().to_owned().create_html();
htmlreport::create_html_file(
Expand Down Expand Up @@ -842,6 +856,7 @@ impl App {
.as_mut()
.unwrap()
.calculate_all_stocked_results();

write_color_buffer(
&BufferWriter::stdout(ColorChoice::Always),
get_writable_color(
Expand Down Expand Up @@ -924,7 +939,10 @@ impl App {

// Qオプションを付けた場合もしくはパースのエラーがない場合はerrorのstackが0となるのでエラーログファイル自体が生成されない。
if ERROR_LOG_STACK.lock().unwrap().len() > 0 {
AlertMessage::create_error_log(stored_static.quiet_errors_flag);
AlertMessage::create_error_log(
stored_static.quiet_errors_flag,
stored_static.common_options.no_color,
);
}
println!();
let _ = self.output_open_close_message("closing_messages.txt", stored_static);
Expand Down
Loading