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

Implemented Proper log handling #86

Merged
merged 44 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
ca2c1bc
refactor: separate cli function from main
SkuldNorniern Feb 5, 2024
647e34c
build: move to `log` from `env_logger`
SkuldNorniern Feb 5, 2024
e3f2d8f
feat: implement simple logger
SkuldNorniern Feb 5, 2024
899ce01
style: `rustfmt`
SkuldNorniern Feb 13, 2024
e112f25
build: bump `pcap-rs`
SkuldNorniern Mar 11, 2024
e997986
refactor: separate cli as a file
SkuldNorniern Mar 11, 2024
88fdcd4
feat: implement logger to `main.rs`
SkuldNorniern Mar 11, 2024
355993f
feat: overhaul interface, error
SkuldNorniern Mar 11, 2024
3d82693
refator: refactor cli to match the log level of the log crate
SkuldNorniern Mar 11, 2024
2e91ca4
feat: set time format for logger
SkuldNorniern Mar 11, 2024
cdd806e
fix: remove `String`, introduce `Cow<str>`
SkuldNorniern Mar 11, 2024
ee592cb
feat: introduce logger usage to `live` collect
SkuldNorniern Mar 11, 2024
3fb5b47
feat: introduce logger to `offline` conversion
SkuldNorniern Mar 11, 2024
fe44fb3
feat: introduce logger to `online` collect
SkuldNorniern Mar 11, 2024
8d51cfc
feat: introduce logger to `packet` collect
SkuldNorniern Mar 11, 2024
88d0139
refactor: remove `verbose` field from `Argument`
SkuldNorniern Mar 11, 2024
f758f3c
feat: introduce logger to csv exporter
SkuldNorniern Mar 11, 2024
4555aec
feat: implement log filter level conversion
SkuldNorniern Mar 11, 2024
45323a9
style: `rustfmt`
SkuldNorniern Mar 11, 2024
334a4f7
feat(plugin): implement `log` as a feature
SkuldNorniern Mar 11, 2024
a864cf5
style(plugin): `rustfmt`
SkuldNorniern Mar 11, 2024
32d1571
build(plugin): bump fluere-plugin version
SkuldNorniern Mar 11, 2024
315f4b6
feat(config): implement `log` as feature
SkuldNorniern Mar 11, 2024
f92cbb6
build(config): bump version of fluere-config
SkuldNorniern Mar 11, 2024
1807564
style: `rustfmt`
SkuldNorniern Mar 11, 2024
88d509f
refactor: commented unused `packet_count`
SkuldNorniern Mar 12, 2024
b0d7f18
style: `rustfmt`
SkuldNorniern Mar 12, 2024
7f6815d
refactor: change log level of few case
SkuldNorniern Mar 12, 2024
7dbbd21
fix: remove deprecated function
SkuldNorniern Mar 12, 2024
f4b6e00
refactor(plugin): use drop on task await
SkuldNorniern Mar 12, 2024
16da5db
refactor: remove unused struct
SkuldNorniern Mar 12, 2024
545a357
refactor: re-arrange some codes
SkuldNorniern Mar 12, 2024
3a37d75
fix: fix while to loop with match cause of `pcap-rs`'s recommendation
SkuldNorniern Mar 12, 2024
19a5709
feat: add some more debug logs
SkuldNorniern Mar 12, 2024
4af371b
style: `rustfmt`
SkuldNorniern Mar 12, 2024
096ec80
feat(plugin): remove use of `String` and use `Cow<str>`
SkuldNorniern Mar 12, 2024
79f809c
feat: add todo for removing panic!
SkuldNorniern Mar 13, 2024
345ed80
feat: apply code review's feedback
SkuldNorniern Mar 13, 2024
69efc2f
refactor: re-batch imports
SkuldNorniern Mar 13, 2024
75f69a8
fix: remove un-needed `unwrap`
SkuldNorniern Mar 13, 2024
914d7b3
style: `rustfmt`
SkuldNorniern Mar 13, 2024
4be4977
feat: change `net` functions to use `net/mod.rs`'s `NetError`
SkuldNorniern Mar 13, 2024
5d982dc
feat: log internal parsing error for later feature search
SkuldNorniern Mar 13, 2024
443fd89
style: `rustfmt`
SkuldNorniern Mar 13, 2024
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
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ pnet_macros_support = "0.34.0"
pnet_macros = "0.34.0"

# using custom forked version of pcap-rs for fixing audits
pcap = { version = "1.1.0", git = "https://github.com/SkuldNorniern/pcap", rev = "40f1163" }
# pcap = { version = "1.1.0", git = "https://github.com/SkuldNorniern/pcap", rev = "40f1163" }

pcap = "1.2.0"

chrono = { version = "0.4.29", default-features = false, features = ["clock"] }
libc = "0.2"
Expand All @@ -28,15 +30,14 @@ snafu = "0.8.0"
serde = { version = "1.0.193", features = ["derive"] }
toml = "0.8.8"

fluere_plugin = { version = "0.1.1", path = "./fluere-plugin" }
fluere-config = { version = "0.1.3", path = "./fluere-config" }
fluere_plugin = { version = "0.2.0", path = "./fluere-plugin", features = ["log"] }
fluere-config = { version = "0.2.0", path = "./fluere-config", features = ["log"] }
fluereflow = { version = "0.3.2", path = "./fluereflow" }

ratatui = { version = "0.25.0", features = ["all-widgets"] }
crossterm = "0.27"
dirs = "5.0.1"
logs = "0.7.1"
env_logger = "0.10.0"
log = { version = "0.4.21", features = ["std"]}

[workspace]
members = [
Expand Down
6 changes: 5 additions & 1 deletion fluere-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fluere-config"
version = "0.1.3"
version = "0.2.0"
edition = "2021"
readme = "README.md"
description = "Configuration manager for Fluere."
Expand All @@ -13,7 +13,11 @@ repository = "https://github.com/SkuldNorniern/fluere"
dirs = "5.0.1"
serde = { version = "1.0.193", features = ["derive"]}
toml = "0.8.8"
log = { version = "0.4.21", features = ["std"], optional = true }

[lib]
name = "fluere_config"


[features]
log = ["dep:log"]
33 changes: 28 additions & 5 deletions fluere-config/src/init.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
use dirs::config_dir;
use std::{default::Default, env, fs, path::Path, path::PathBuf};

use crate::Config;

use std::{default::Default, env, fs, path::Path, path::PathBuf};
use dirs::config_dir;

#[cfg(feature = "log")]
use log::{debug, error, warn};

impl Config {
pub fn new() -> Self {
let path_base = home_config_path();

let path_file = path_base.join(Path::new("fluere.toml"));
println!("path_file: {:?}", path_file);

#[cfg(feature = "log")]
debug!("Using config file from: {:?}", path_file);
#[cfg(not(feature = "log"))]
println!("Using config file from: {:?}", path_file);
if !path_base.exists() {
match fs::create_dir_all(&path_base) {
Ok(_) => (),
Ok(_) => {
#[cfg(feature = "log")]
debug!("Created directory at {:?}", path_base);
()

Check warning on line 25 in fluere-config/src/init.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded unit expression

warning: unneeded unit expression --> fluere-config/src/init.rs:25:21 | 25 | () | ^^ help: remove the final `()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_unit = note: `#[warn(clippy::unused_unit)]` on by default
}
Err(e) => {
#[cfg(feature = "log")]
error!("Failed to create directory at {:?}: {}", path_base, e);
#[cfg(not(feature = "log"))]
eprintln!("Failed to create directory at {:?}: {}", path_base, e);

return Config::default();
}
}
Expand All @@ -25,8 +40,16 @@
}

match Self::load(path_file.to_str().unwrap().to_string()) {
Ok(config) => config,
Ok(config) => {
#[cfg(feature = "log")]
debug!("Loaded configuration from: {:?}", path_file);

config
}
Err(_) => {
#[cfg(feature = "log")]
warn!("failed to load configuration, using default config");
#[cfg(not(feature = "log"))]
println!("failed to load configuration, using default config");
Config::default()
}
Expand Down Expand Up @@ -72,5 +95,5 @@
}
};
let path_config = path_base.join("fluere");
path_config

Check warning on line 98 in fluere-config/src/init.rs

View workflow job for this annotation

GitHub Actions / clippy

returning the result of a `let` binding from a block

warning: returning the result of a `let` binding from a block --> fluere-config/src/init.rs:98:5 | 97 | let path_config = path_base.join("fluere"); | ------------------------------------------- unnecessary `let` binding 98 | path_config | ^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return = note: `#[warn(clippy::let_and_return)]` on by default help: return the expression directly | 97 ~ 98 ~ path_base.join("fluere") |
}
10 changes: 8 additions & 2 deletions fluere-plugin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fluere_plugin"
version = "0.1.1"
version = "0.2.0"
authors = ["Skuld Norniern <skuldnorniern@gmail.com>"]
edition = "2021"
description = "Plugin API for Fluere."
Expand All @@ -16,12 +16,18 @@ repository = "https://github.com/SkuldNorniern/fluere"
[dependencies]
git2 = "0.18.1"
tokio = { version = "1.32", features = ["full","macros", "rt-multi-thread"] }
fluere-config = { version = "0.1.2", path = "../fluere-config" }
fluere-config = { version = "0.2.0", path = "../fluere-config" }
#fluere-plugin-trait = { path = "../fluere-plugin-trait" }
fluereflow = { version = "0.3.2", path = "../fluereflow" }
dirs = "5.0.1"
mlua = { version = "0.9.2", features = ["lua54", "vendored","async","send"] }
inksac = "0.4.0"

log = { version = "0.4.21", features = ["std"], optional = true }

[lib]
name = "fluere_plugin"


[features]
log = ["dep:log"]
23 changes: 22 additions & 1 deletion fluere-plugin/src/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use std::time::Duration;

use crate::util::home_cache_path;

#[cfg(feature = "log")]
use log::{debug, info};

#[derive(Debug)]
pub enum DownloadError {
Io(std::io::Error),
Expand Down Expand Up @@ -81,7 +84,11 @@ pub fn download_plugin_from_github(repo_name: &str) -> Result<(), DownloadError>
if has_local_changes(&repo)? {
println!("{}: You have uncommitted changes. Updating will overwrite these changes. Continue? [y/N] (auto skip in 5 seconds)","Warning".styled(warn_style));
if !user_confirms()? {
#[cfg(feature = "log")]
info!("Update skipped for {}", repo_name.styled(highlight_style));
#[cfg(not(feature = "log"))]
println!("Update skipped for {}", repo_name.styled(highlight_style));

return Ok(());
}
}
Expand All @@ -96,14 +103,26 @@ pub fn download_plugin_from_github(repo_name: &str) -> Result<(), DownloadError>
repo.checkout_tree(fetch_commit_obj.as_object(), Some(&mut checkout_builder))?;
repo.set_head_detached(fetch_commit)?;

#[cfg(feature = "log")]
info!(
"Successfully updated to the latest version for {}",
repo_name.styled(highlight_style)
);
#[cfg(not(feature = "log"))]
println!(
"Successfully updated to the latest version for {}",
repo_name.styled(highlight_style)
);
} else {
#[cfg(feature = "log")]
info!("Update skipped for {}", repo_name.styled(highlight_style));
#[cfg(not(feature = "log"))]
println!("Update skipped for {}", repo_name.styled(highlight_style));
}
} else {
#[cfg(feature = "log")]
info!("{} is up to date.", repo_name.styled(highlight_style));
#[cfg(not(feature = "log"))]
println!("{} is up to date.", repo_name.styled(highlight_style));
}

Expand All @@ -129,7 +148,9 @@ fn user_confirms() -> Result<bool, DownloadError> {
match receiver.recv_timeout(Duration::from_secs(5)) {
Ok(result) => Ok(result),
Err(_) => {
print!("Timeout. ");
#[cfg(feature = "log")]
debug!("Timeout. ");

Ok(false)
}
}
Expand Down
83 changes: 69 additions & 14 deletions fluere-plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
use fluere_config::Config;
use fluereflow::FluereRecord;
use mlua::{Lua, Result};
use tokio::sync::{mpsc, Mutex};

use std::collections::HashSet;
use std::sync::Arc;

Expand All @@ -12,6 +7,14 @@ mod util;
use downloader::download_plugin_from_github;
use util::home_cache_path;

use fluere_config::Config;
use fluereflow::FluereRecord;
use mlua::{Lua, Result};
use tokio::sync::{mpsc, Mutex};

#[cfg(feature = "log")]
use log::{debug, error, info, warn};

pub struct PluginManager {
lua: Arc<Mutex<Lua>>,
sender: mpsc::Sender<FluereRecord>,
Expand All @@ -34,6 +37,9 @@ impl PluginManager {
}

pub async fn load_plugins(&self, config: &Config) -> Result<()> {
#[cfg(feature = "log")]
debug!("Loading plugins");

let plugins_clone = self.plugins.clone();
let mut plugins_guard = plugins_clone.lock().await;
for (name, plugin_config) in &config.plugins {
Expand All @@ -50,7 +56,10 @@ impl PluginManager {
let lua_clone = self.lua.clone();
let lua_guard = lua_clone.lock().await;
let lua = &*lua_guard;
// println!("lua path: {}", path);

#[cfg(feature = "log")]
debug!("Lua path: {}", path);

let lua_plugin_path =
format!("package.path = package.path .. \";{}/?.lua\"", path);
let _ = lua.load(lua_plugin_path).exec();
Expand All @@ -61,7 +70,9 @@ impl PluginManager {

let argument_table = lua.create_table()?;

// println!("extra argument details{:?}", plugin_config.extra_arguments);
#[cfg(feature = "log")]
debug!("extra argument details{:?}", plugin_config.extra_arguments);

for (key, value) in
plugin_config.extra_arguments.clone().unwrap().iter()
{
Expand Down Expand Up @@ -89,11 +100,22 @@ impl PluginManager {
//lua_guard.load(&code).exec().expect(format!("Error on plugin: {}", name).as_str());

plugins_guard.insert(name.clone());
#[cfg(feature = "log")]
info!("Loaded plugin {}", name);
#[cfg(not(feature = "log"))]
println!("Loaded plugin {}", name);
}
Err(err) => {
println!("Failed to read plugin: {}", name);
println!("Error: {}", err);
#[cfg(feature = "log")]
{
warn!("Failed to read plugin: {}", name);
error!("Error: {}", err);
}
#[cfg(not(feature = "log"))]
{
println!("Failed to read plugin: {}", name);
println!("Error: {}", err);
}
continue;
}
};
Expand Down Expand Up @@ -147,18 +169,37 @@ impl PluginManager {
Ok(())
}).expect(format!("Error on plugin: {}", name).as_str());*/
plugins_guard.insert(name.clone());
#[cfg(feature = "log")]
info!("Loaded plugin {}", name);
#[cfg(not(feature = "log"))]
println!("Loaded plugin {}", name);
}
Err(eri) => {
println!("Failed to read plugin: {}", name);
println!("Error: {}", eri);
#[cfg(feature = "log")]
{
warn!("Failed to read plugin: {}", name);
error!("Error: {}", eri);
}
#[cfg(not(feature = "log"))]
{
println!("Failed to read plugin: {}", name);
println!("Error: {}", eri);
}
continue;
}
}
}
Err(eri) => {
println!("Unable to download plugin: {}", name);
println!("Error: {}", eri);
#[cfg(feature = "log")]
{
warn!("Unable to download plugin: {}", name);
error!("Error: {}", eri);
}
#[cfg(not(feature = "log"))]
{
println!("Unable to download plugin: {}", name);
println!("Error: {}", eri);
}
}
}
}
Expand Down Expand Up @@ -233,6 +274,13 @@ impl PluginManager {
func.call::<mlua::Table<'_>, ()>(lua_table.clone())
.unwrap_or_else(|_| panic!("Error on plugin: {}", plugin_name));
} else {
#[cfg(feature = "log")]
error!(
"'process_data' function not found in plugin: {}",
plugin_name
);

#[cfg(not(feature = "log"))]
println!(
"'process_data' function not found in plugin: {}",
plugin_name
Expand All @@ -253,7 +301,7 @@ impl PluginManager {

pub async fn await_completion(&self, target_worker: Arc<Mutex<tokio::task::JoinHandle<()>>>) {
let worker_clone = target_worker.clone();
let _ = worker_clone.lock().await;
let worker = worker_clone.lock().await;

// Cleanup each plugin before exiting
let lua_clone = self.lua.clone();
Expand All @@ -272,8 +320,15 @@ impl PluginManager {
func.call::<(), ()>(())
.unwrap_or_else(|_| panic!("Error on plugin: {}", plugin_name));
} else {
#[cfg(feature = "log")]
warn!("cleanup function not found in plugin: {}", plugin_name);
#[cfg(not(feature = "log"))]
println!("cleanup function not found in plugin: {}", plugin_name);
}
}

drop(lua);
drop(plugins);
drop(worker);
}
}
Loading
Loading