Skip to content

Commit

Permalink
style: rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
SkuldNorniern committed Mar 13, 2024
1 parent 1815f48 commit 3bb31fb
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 54 deletions.
2 changes: 0 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,3 @@ pub fn cli_template() -> Command {
),
)
}


62 changes: 45 additions & 17 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::fs::File;
use std::io::{ Write, stdout, stderr};
use std::io::{stderr, stdout, Write};
use std::path::PathBuf;

use chrono::Local; // Import the Local struct from the chrono crate
use log::{Level, Record, Metadata, Log };
use log::{Level, Log, Metadata, Record};

pub enum Logstdout{
pub enum Logstdout {
Stdout,
StdErr,
}
Expand All @@ -21,7 +21,7 @@ impl Logger {
pub fn new(write_to_file: bool, file_path: Option<PathBuf>) -> Self {
let mut path = file_path;
if path.is_none() {
path = Some(PathBuf::from(
path = Some(PathBuf::from(
#[cfg(target_os = "linux")]
"/var/log/fluere/fluere.log",
#[cfg(target_os = "windows")]
Expand All @@ -30,20 +30,30 @@ impl Logger {
"/Library/Logs/fluere/fluere.log",
#[cfg(target_os = "bsd")]
"/var/log/fluere/fluere.log",
#[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos", target_os = "bsd")))]
#[cfg(not(any(
target_os = "linux",
target_os = "windows",
target_os = "macos",
target_os = "bsd"
)))]
"/var/log/fluere/fluere.log",
));
}
let mut file = None;
if write_to_file {
file = Some(File::create(path.as_ref().unwrap()).unwrap());
}
Logger {write_to_file: true, write_to_std: None, severity: Level::Info , file}
}
Logger {
write_to_file: true,
write_to_std: None,
severity: Level::Info,
file,
}
}

// pub fn log(&mut self, severity: Level, message: &str) {
// let timestamp = Local::now(); // Get the current timestamp using Local::now()
// let log_message = format!("{:?} {}: {}", timestamp, severity, message); // Format the timestamp and append it to the log message
// let timestamp = Local::now(); // Get the current timestamp using Local::now()
// let log_message = format!("{:?} {}: {}", timestamp, severity, message); // Format the timestamp and append it to the log message
// }
}

Expand All @@ -55,24 +65,42 @@ impl Log for Logger {
fn log(&self, record: &Record) {
let timestamp = Local::now();

if self.write_to_std.as_ref().is_some() && record.level() <= self.severity{
if self.write_to_std.as_ref().is_some() && record.level() <= self.severity {
match self.write_to_std.as_ref().unwrap() {
Logstdout::Stdout => {
writeln!(stdout(), "[{}]: {} {}",timestamp, record.level(), record.args()).unwrap();
writeln!(
stdout(),
"[{}]: {}: {}",
timestamp,
record.level(),
record.args()
)
.unwrap();
}
Logstdout::StdErr => {
writeln!(stderr(), "[{}]: {} {}",timestamp, record.level(), record.args()).unwrap();
writeln!(
stderr(),
"[{}]: {}: {}",
timestamp,
record.level(),
record.args()
)
.unwrap();
}
}
}

if self.write_to_file {

writeln!(self.file.as_ref().unwrap(), "[{}]: {} {}",timestamp, record.level(), record.args()).unwrap();
writeln!(
self.file.as_ref().unwrap(),
"[{}]: {}: {}",
timestamp,
record.level(),
record.args()
)
.unwrap();
}
}

fn flush(&self) {

}
fn flush(&self) {}
}
49 changes: 48 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,65 @@
// It also supports live capture and conversion of NetFlow data.
// This file contains the main function which parses the command line arguments and calls the appropriate functions based on the arguments.

pub mod cli;
pub mod logger;
pub mod net;
pub mod plugin;
pub mod types;
pub mod utils;
pub mod cli;

use pnet::datalink;
// use env_logger::{init, Logger};
use log::Level;

use crate::logger::Logger;
use crate::net::list_devices;

use std::fmt::Display;
use std::process::exit;

enum Mode {
Offline,
Online,
Live,
Pcap,
}
impl Display for Mode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Mode::Offline => write!(f, "Offline"),
Mode::Online => write!(f, "Online"),
Mode::Live => write!(f, "Live"),
Mode::Pcap => write!(f, "Pcap"),
}
}
}

struct Fluere {
interface: String,
args: types::Args,
mode: Mode,
logger: Logger,
verbose: Level,
}
impl Fluere {
fn new(
interface: String,
args: types::Args,
mode: Mode,
logger: Logger,
verbose: Level,
) -> Fluere {
Fluere {
interface,
args,
mode,
logger,
verbose,
}
}
}

// This is the main function of the application.
// It gets the command line arguments, parses them, and calls the appropriate functions based on the arguments.
#[tokio::main]
Expand Down
2 changes: 1 addition & 1 deletion src/net/capture.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pcap::{Active, Address, Capture, Device, Error as PcapError};

use std::time::Instant;
use std::fmt;
use std::time::Instant;

#[derive(Debug)]
pub enum DeviceError {
Expand Down
4 changes: 2 additions & 2 deletions src/net/packet_pcap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::fs;

use tokio::time::sleep;

use crate::net::find_device;
use crate::net::CaptureDevice;
use crate::types::Args;
use crate::utils::cur_time_file;
use crate::net::CaptureDevice;
use crate::net::find_device;

use std::time::{Duration, Instant};

Expand Down
124 changes: 93 additions & 31 deletions src/utils/log.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,106 @@
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::io::{stderr, stdout, Write};
use std::path::PathBuf;

use chrono::Local; // Import the Local struct from the chrono crate
use log::{Level, Log, Metadata, Record};

pub struct Log {
file: File,
pub enum Logstdout {
Stdout,
StdErr,
}

impl Log {
#[cfg(target_os = "linux")]
pub fn new() -> Self {
let path = "/var/log/fluere/fluere.log";
let file = File::create(&path).expect("Failed to create log file");
Log { file }
}
pub struct Logger {
write_to_file: bool,
write_to_std: Option<Logstdout>,
severity: Level,
file: Option<File>,
}

#[cfg(target_os = "windows")]
pub fn new() -> Self {
let path = "C:\\Program Files\\fluere\\fluere.log";
let file = File::create(&path).expect("Failed to create log file");
Log { file }
impl Logger {
pub fn new(write_to_file: bool, file_path: Option<PathBuf>) -> Self {
let mut path = file_path;
if path.is_none() {
path = Some(PathBuf::from(
#[cfg(target_os = "linux")]
"/var/log/fluere/fluere.log",
#[cfg(target_os = "windows")]
"C:\\Program Files\\fluere\\fluere.log",
#[cfg(target_os = "macos")]
"/Library/Logs/fluere/fluere.log",
#[cfg(target_os = "bsd")]
"/var/log/fluere/fluere.log",
#[cfg(not(any(
target_os = "linux",
target_os = "windows",
target_os = "macos",
target_os = "bsd"
)))]
"/var/log/fluere/fluere.log",
));
}
let mut file = None;
if write_to_file {
file = Some(File::create(path.as_ref().unwrap()).unwrap());
}
Logger {
write_to_file: true,
write_to_std: None,
severity: Level::Info,
file,
}
}

#[cfg(target_os = "macos")]
pub fn new() -> Self {
let path = "/Library/Logs/fluere/fluere.log";
let file = File::create(&path).expect("Failed to create log file");
Log { file }
}
// pub fn log(&mut self, severity: Level, message: &str) {
// let timestamp = Local::now(); // Get the current timestamp using Local::now()
// let log_message = format!("{:?} {}: {}", timestamp, severity, message); // Format the timestamp and append it to the log message
// }
}

#[cfg(target_os = "bsd")]
pub fn new() -> Self {
let path = "/var/log/fluere/fluere.log";
let file = File::create(&path).expect("Failed to create log file");
Log { file }
impl Log for Logger {
fn enabled(&self, _metadata: &Metadata) -> bool {
true
}

pub fn log(&mut self, severity: Severity, message: &str) {
let timestamp = Local::now(); // Get the current timestamp using Local::now()
let log_message = format!("{:?} {}: {}", timestamp, severity, message); // Format the timestamp and append it to the log message
self.file.write_all(log_message.as_bytes()).expect("Failed to write to log file");
fn log(&self, record: &Record) {
let timestamp = Local::now();

if self.write_to_std.as_ref().is_some() && record.level() <= self.severity {
match self.write_to_std.as_ref().unwrap() {
Logstdout::Stdout => {
write!(
stdout(),
"[{}]: {} {}",
timestamp,
record.level(),
record.args()
)
.unwrap();
}
Logstdout::StdErr => {
write!(
stderr(),
"[{}]: {} {}",
timestamp,
record.level(),
record.args()
)
.unwrap();
}
}
}

if self.write_to_file {
writeln!(
self.file.as_ref().unwrap(),
"[{}]: {} {}",
timestamp,
record.level(),
record.args()
)
.unwrap();
}
}

fn flush(&self) {}
}

0 comments on commit 3bb31fb

Please sign in to comment.