From fe33c2eeec3d8f16e96eafa977c43b98fce9d914 Mon Sep 17 00:00:00 2001 From: Fred Clausen <43556888+fredclausen@users.noreply.github.com> Date: Sat, 27 May 2023 18:07:11 -0600 Subject: [PATCH] Switch logging to SDR-E Rust Logging common library --- Cargo.toml | 1 - rust/oxide-bin/Cargo.toml | 2 +- rust/oxide-bin/src/main.rs | 2 +- rust/oxide-logging/Cargo.toml | 11 ----- rust/oxide-logging/src/lib.rs | 92 ----------------------------------- 5 files changed, 2 insertions(+), 106 deletions(-) delete mode 100644 rust/oxide-logging/Cargo.toml delete mode 100644 rust/oxide-logging/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index ff2f79d..e336282 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,6 @@ members = [ "rust/oxide-config", "rust/oxide-decoders", "rust/oxide-helpers", - "rust/oxide-logging", "rust/oxide-output", "rust/oxide-rtlsdr", "rust/oxide-scanner", diff --git a/rust/oxide-bin/Cargo.toml b/rust/oxide-bin/Cargo.toml index 3955654..e07180c 100644 --- a/rust/oxide-bin/Cargo.toml +++ b/rust/oxide-bin/Cargo.toml @@ -15,8 +15,8 @@ rust-version.workspace = true log = "0.4.17" oxide-rtlsdr = { path = "../oxide-rtlsdr" } oxide-config = { path = "../oxide-config" } -oxide-logging = { path = "../oxide-logging" } oxide-scanner = { path = "../oxide-scanner" } oxide-decoders = { path = "../oxide-decoders" } +sdre-rust-logging = { git = "https://github.com/sdr-enthusiasts/sdre-rust-logging.git" } tokio = { version = "1.28.1", features = ["full", "tracing"] } array-init = "2.1.0" diff --git a/rust/oxide-bin/src/main.rs b/rust/oxide-bin/src/main.rs index 1ee8783..0bf11df 100644 --- a/rust/oxide-bin/src/main.rs +++ b/rust/oxide-bin/src/main.rs @@ -21,8 +21,8 @@ extern crate oxide_rtlsdr; use oxide_config::clap::Parser; use oxide_config::OxideInput; use oxide_decoders::ValidDecoderType; -use oxide_logging::SetupLogging; use oxide_rtlsdr::RtlSdr; +use sdre_rust_logging::SetupLogging; use tokio::time::{sleep, Duration}; #[tokio::main] diff --git a/rust/oxide-logging/Cargo.toml b/rust/oxide-logging/Cargo.toml deleted file mode 100644 index d4ff64c..0000000 --- a/rust/oxide-logging/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "oxide-logging" -version.workspace = true -edition.workspace = true - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -env_logger = "0.10.0" -chrono = "0.4.24" -log = "0.4.17" diff --git a/rust/oxide-logging/src/lib.rs b/rust/oxide-logging/src/lib.rs deleted file mode 100644 index 93df94d..0000000 --- a/rust/oxide-logging/src/lib.rs +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright (C) 2023 Fred Clausen - -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA - -extern crate chrono; -extern crate env_logger; -extern crate log; - -use chrono::Local; -use env_logger::fmt::Color; -use env_logger::Builder; -use log::LevelFilter; -use std::io::Write; - -pub trait SetupLogging { - fn set_logging_level(self) -> LevelFilter; - fn enable_logging(&self); -} - -impl SetupLogging for u8 { - fn set_logging_level(self) -> LevelFilter { - match self { - 0 => LevelFilter::Info, - 1 => LevelFilter::Debug, - 2..=u8::MAX => LevelFilter::Trace, - } - } - - fn enable_logging(&self) { - Builder::new() - .format(|buf, record| { - let mut level_style = buf.style(); - - if record.level() == log::Level::Info { - level_style.set_color(Color::Green).set_bold(true); - } else if record.level() == log::Level::Debug { - level_style.set_color(Color::Cyan).set_bold(true); - } else if record.level() == log::Level::Trace { - level_style.set_color(Color::Magenta).set_bold(true); - } else if record.level() == log::Level::Error { - level_style.set_color(Color::Red).set_bold(true); - } else if record.level() == log::Level::Warn { - level_style.set_color(Color::Yellow).set_bold(true); - } else { - level_style.set_color(Color::White).set_bold(true); - } - - writeln!( - buf, - "[{}][{}]{}", - level_style.value(format!("{: <5}", record.level())), - Local::now().format("%Y-%m-%dT%H:%M:%S"), - record.args() - ) - }) - .filter(None, self.set_logging_level()) - .init(); - } -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn test_set_logging_level() { - let info_level: u8 = 0; - let debug_level: u8 = 1; - let trace_level: u8 = 2; - let stupid_levels: u8 = 255; - let info_level_logging: LevelFilter = info_level.set_logging_level(); - let debug_level_logging: LevelFilter = debug_level.set_logging_level(); - let trace_level_logging: LevelFilter = trace_level.set_logging_level(); - let stupid_levels_logging: LevelFilter = stupid_levels.set_logging_level(); - assert_eq!(info_level_logging, LevelFilter::Info); - assert_eq!(debug_level_logging, LevelFilter::Debug); - assert_eq!(trace_level_logging, LevelFilter::Trace); - assert_eq!(stupid_levels_logging, LevelFilter::Trace); - } -}