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

feat: add log filter #98

Merged
merged 5 commits into from
Aug 19, 2023
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ readme = "README.md"
license = "MIT"

[workspace.dependencies]
logid = { version = "0.9", features = ["diagnostics"] }
logid = { version = "0.12.1", features = ["diagnostics"] }
thiserror = "1.0"
once_cell = "1.13.0"
clap = { version = "4.2.7", features = ["derive", "cargo", "env"] }
Expand Down
6 changes: 3 additions & 3 deletions cli/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn compile(config: Config) -> Result<(), GeneralError> {
let source = fs::read_to_string(&config.input).map_err(|error| {
pipe!(
GeneralError::FileRead,
&format!("Could not read file: '{:?}'", &config.input),
format!("Could not read file: '{:?}'", &config.input),
add: AddonKind::Info(format!("Cause: {}", error))
)
})?;
Expand Down Expand Up @@ -66,13 +66,13 @@ fn write_file(

log!(
GeneralInfo::WritingToFile,
&format!("Writing to file: {:?}", full_out_path),
format!("Writing to file: {:?}", full_out_path),
);

std::fs::write(&full_out_path, content).map_err(|error| {
pipe!(
GeneralError::FileWrite,
&format!("Could not write to file: {:?}", full_out_path),
format!("Could not write to file: {:?}", full_out_path),
add: AddonKind::Info(format!("Cause: {}", error))
)
})
Expand Down
24 changes: 18 additions & 6 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use clap::Parser;
use logid::{event_handler::LogEventHandlerBuilder, log, logging::event_entry::AddonKind};
use logid::{
event_handler::builder::LogEventHandlerBuilder,
log,
log_id::LogLevel,
logging::{
event_entry::AddonKind,
filter::{AddonFilter, FilterConfigBuilder},
},
};
use unimarkup_commons::config::{Config, ConfigFns};

use crate::log_id::{GeneralError, GeneralInfo};
Expand All @@ -8,8 +16,14 @@ mod compiler;
mod log_id;

fn main() {
let log_handler = LogEventHandlerBuilder::new()
.write_to_console()
let _ = logid::logging::filter::set_filter(
FilterConfigBuilder::new(LogLevel::Info)
.allowed_addons(AddonFilter::Infos)
.build(),
);

let _handler = LogEventHandlerBuilder::new()
.to_stderr()
.all_log_events()
.build();

Expand All @@ -19,7 +33,7 @@ fn main() {
if let Err(err) = cfg.validate() {
logid::log!(
GeneralError::Compile,
&format!("Configuration is invalid: {err}")
format!("Configuration is invalid: {err}")
);
break 'outer;
}
Expand All @@ -46,6 +60,4 @@ fn main() {
}
}
}

log_handler.shutdown();
}
19 changes: 11 additions & 8 deletions cli/tests/logging/cli_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ fn test__main_log_trace__attributes_file() {
path.push(format!("tests/test_files/{}", TEST_FILE));

let cli_proc = Command::new("cargo")
.stdout(Stdio::piped())
.args(["run", "--", "--formats=html", &path.to_string_lossy()])
.stderr(Stdio::piped())
.args([
"run",
"--",
"--formats=html",
"--lang=en",
&path.to_string_lossy(),
])
.spawn()
.expect("Failed to spawn 'cargo run'");

let output = cli_proc
.wait_with_output()
.expect("Failed to execute 'cargo run'");
let logs = String::from_utf8_lossy(&output.stdout);
let logs = String::from_utf8_lossy(&output.stderr);

assert!(logs.contains("INFO: Writing to file: "));
assert!(logs.contains("Writing to file: "));
assert!(logs.contains(&TEST_FILE.replace(".um", ".html")));
// assert!(logs.contains("64(origin): file="));
assert!(logs.contains("INFO: Unimarkup finished compiling."));
// assert!(logs.contains(TEST_FILE));
// assert!(logs.contains("65(origin): file="));
assert!(logs.contains("Unimarkup finished compiling."));
}
4 changes: 2 additions & 2 deletions commons/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl ConfigFns for Config {
if !self.input.exists() {
return err!(
ConfigErr::InvalidFile,
&format!("Input file not found: {:?}", self.input)
format!("Input file not found: {:?}", self.input)
);
}

Expand All @@ -90,7 +90,7 @@ impl Config {
.map_err(|_| {
pipe!(
ConfigErr::InvalidFile,
&format!(
format!(
"Failed to read locales file: {:?}",
locales_file.as_ref().map(|p| p.to_string_lossy())
)
Expand Down
2 changes: 1 addition & 1 deletion commons/src/config/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl ConfigFns for Output {
if filepath.exists() {
return err!(
ConfigErr::InvalidConfig,
&format!(
format!(
"Output file '{:?}' already exists, but `overwrite` was not set.",
filepath
)
Expand Down
20 changes: 10 additions & 10 deletions commons/src/config/preamble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl ConfigFns for I18n {
{
return err!(
ConfigErr::BadLocaleUsed,
&format!(
format!(
"{} locale(s) not supported by default. Only the following locales are allowed: {}.",
locales
.iter()
Expand Down Expand Up @@ -125,7 +125,7 @@ impl ConfigFns for I18n {
.map_err(|_| {
pipe!(
ConfigErr::InvalidFile,
&format!(
format!(
"Failed to read locales file: {}",
self.locales_file.as_ref().unwrap().to_string_lossy()
)
Expand Down Expand Up @@ -159,7 +159,7 @@ impl ConfigFns for I18n {
provider.load_buffer(key, req).map_err(|err| {
pipe!(
ConfigErr::BadLocaleUsed,
&format!(
format!(
"Could not find locale '{}' in data file. Cause: {}",
locale, err
)
Expand Down Expand Up @@ -204,7 +204,7 @@ impl I18n {
Err(err) => {
logid::log!(
ConfigErr::InvalidFile,
&format!(
format!(
"Locales file not found: {}. Cause: {}. Using default locales file.",
file_path.to_string_lossy(),
err
Expand All @@ -229,7 +229,7 @@ impl I18n {
let f = File::create(&file_path).map_err(|_| {
pipe!(
ConfigErr::FileCreate,
&format!(
format!(
"Failed to create locales file: {}",
file_path.as_ref().to_string_lossy()
)
Expand All @@ -246,7 +246,7 @@ impl I18n {
.map_err(|err| {
pipe!(
ConfigErr::LocaleDownload,
&format!(
format!(
"Failed to download locales file: {}. Cause: {}",
file_path.as_ref().to_string_lossy(),
err,
Expand Down Expand Up @@ -302,7 +302,7 @@ impl ConfigFns for Citedata {
if !file.exists() {
return err!(
ConfigErr::InvalidFile,
&format!("Citation Style Language file not found: {:?}", file)
format!("Citation Style Language file not found: {:?}", file)
);
}
}
Expand All @@ -311,7 +311,7 @@ impl ConfigFns for Citedata {
if !reference.exists() {
return err!(
ConfigErr::InvalidFile,
&format!("Bibliography references file not found: {:?}", reference)
format!("Bibliography references file not found: {:?}", reference)
);
}
}
Expand Down Expand Up @@ -348,7 +348,7 @@ impl ConfigFns for Metadata {
if !font.exists() {
return err!(
ConfigErr::InvalidFile,
&format!("Font file not found: {:?}", font)
format!("Font file not found: {:?}", font)
);
}
}
Expand Down Expand Up @@ -384,7 +384,7 @@ impl ConfigFns for HtmlSpecificParameter {
if !fav.exists() {
return err!(
ConfigErr::InvalidFile,
&format!("Favicon file not found: {:?}", fav)
format!("Favicon file not found: {:?}", fav)
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion parser/src/security/hashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn get_filehash(file: &Path) -> Result<Vec<u8>, HashingError> {
let source = fs::read_to_string(file).map_err(|err| {
pipe!(
HashingError::FailedReadingFile,
&format!("Could not read file: '{:?}'", file),
format!("Could not read file: '{:?}'", file),
add: AddonKind::Info(format!("Cause: {}", err))
)
})?;
Expand Down
2 changes: 1 addition & 1 deletion render/src/log_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use logid::{evident::event::intermediary::FinalizedEvent, log_id::LogId, ErrLogId};
use logid::{evident::event::finalized::FinalizedEvent, log_id::LogId, ErrLogId};
use thiserror::Error;

#[derive(Debug, Clone, ErrLogId, Error)]
Expand Down
Loading