-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2086 from Ruadhri17/log-plugin-ref
Create generic tedge log plugin
- Loading branch information
Showing
21 changed files
with
1,903 additions
and
449 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "log_manager" | ||
version = { workspace = true } | ||
authors = { workspace = true } | ||
edition = { workspace = true } | ||
rust-version = { workspace = true } | ||
license = { workspace = true } | ||
homepage = { workspace = true } | ||
repository = { workspace = true } | ||
|
||
[dependencies] | ||
easy_reader = "0.5" | ||
glob = "0.3" | ||
log = "0.4" | ||
serde = { version = "1.0", features = ["derive"] } | ||
thiserror = "1.0" | ||
time = { version = "0.3", features = ["formatting"] } | ||
toml = "0.5" | ||
|
||
[dev-dependencies] | ||
filetime = "0.2" | ||
tedge_test_utils = { path = "../../tests/tedge_test_utils" } | ||
time = { version = "0.3", features = ["macros"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use log::info; | ||
use log::warn; | ||
use serde::Deserialize; | ||
use std::collections::HashSet; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Default)] | ||
pub struct LogPluginConfig { | ||
pub files: Vec<FileEntry>, | ||
} | ||
|
||
#[derive(Deserialize, Debug, Eq, Default, Clone)] | ||
pub struct FileEntry { | ||
pub(crate) path: String, | ||
#[serde(rename = "type")] | ||
pub config_type: String, | ||
} | ||
|
||
impl PartialEq for FileEntry { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.config_type == other.config_type | ||
} | ||
} | ||
|
||
impl LogPluginConfig { | ||
pub fn new(config_file_path: &Path) -> Self { | ||
Self::read_config(config_file_path) | ||
} | ||
|
||
pub fn read_config(path: &Path) -> Self { | ||
let path_str = path.display().to_string(); | ||
info!("Using the configuration from {}", path_str); | ||
match fs::read_to_string(path) { | ||
Ok(contents) => match toml::from_str(contents.as_str()) { | ||
Ok(config) => config, | ||
_ => { | ||
warn!("The config file {} is malformed.", path_str); | ||
Self::default() | ||
} | ||
}, | ||
Err(_) => { | ||
warn!( | ||
"The config file {} does not exist or is not readable.", | ||
path_str | ||
); | ||
Self::default() | ||
} | ||
} | ||
} | ||
|
||
pub fn get_all_file_types(&self) -> Vec<String> { | ||
self.files | ||
.iter() | ||
.map(|x| x.config_type.to_string()) | ||
.collect::<HashSet<_>>() | ||
.iter() | ||
.map(|x| x.to_string()) | ||
.collect::<Vec<_>>() | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_no_duplicated_file_types() { | ||
let files = vec![ | ||
FileEntry { | ||
path: "a/path".to_string(), | ||
config_type: "type_one".to_string(), | ||
}, | ||
FileEntry { | ||
path: "some/path".to_string(), | ||
config_type: "type_one".to_string(), | ||
}, | ||
]; | ||
let logs_config = LogPluginConfig { files }; | ||
assert_eq!( | ||
logs_config.get_all_file_types(), | ||
vec!["type_one".to_string()] | ||
); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
mod config; | ||
mod error; | ||
mod log_utils; | ||
|
||
pub use config::*; | ||
pub use error::*; | ||
pub use log_utils::*; |
Oops, something went wrong.
05cec39
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Robot Results