Skip to content

Commit

Permalink
Merge pull request #2086 from Ruadhri17/log-plugin-ref
Browse files Browse the repository at this point in the history
Create generic tedge log plugin
  • Loading branch information
didier-wenzek authored Aug 18, 2023
2 parents a67c8e5 + f5e2519 commit 05cec39
Show file tree
Hide file tree
Showing 21 changed files with 1,903 additions and 449 deletions.
62 changes: 59 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"plugins/c8y_remote_access_plugin",
"plugins/tedge_apt_plugin",
"plugins/tedge_dummy_plugin",
"plugins/tedge_log_plugin",
]
resolver = "2"

Expand Down
23 changes: 23 additions & 0 deletions crates/common/log_manager/Cargo.toml
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"] }
80 changes: 80 additions & 0 deletions crates/common/log_manager/src/config.rs
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.
7 changes: 7 additions & 0 deletions crates/common/log_manager/src/lib.rs
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::*;
Loading

1 comment on commit 05cec39

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass %
255 0 5 255 100

Please sign in to comment.