From 710932df060183a478f9d496e05cad692505a9a2 Mon Sep 17 00:00:00 2001 From: Liu Jiang Date: Sun, 31 May 2020 03:17:12 +0800 Subject: [PATCH] netlink: use features for slog and agent handler Use features to enable/disable slog and agent handler on demand. This helps to reduce dependency chains if slog/agent handler is unused. Signed-off-by: Liu Jiang --- src/agent/Cargo.lock | 2 -- src/agent/netlink/Cargo.toml | 15 +++++++++------ src/agent/netlink/src/agent_handler.rs | 1 + src/agent/netlink/src/lib.rs | 25 +++++++++++++++++++------ 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/agent/Cargo.lock b/src/agent/Cargo.lock index 2c1eb6362f..8676366594 100644 --- a/src/agent/Cargo.lock +++ b/src/agent/Cargo.lock @@ -257,8 +257,6 @@ dependencies = [ "protocols", "scan_fmt", "slog", - "slog-async", - "slog-json", "slog-scope", ] diff --git a/src/agent/netlink/Cargo.toml b/src/agent/netlink/Cargo.toml index 0e91983a4c..a14dc66f6d 100644 --- a/src/agent/netlink/Cargo.toml +++ b/src/agent/netlink/Cargo.toml @@ -9,10 +9,13 @@ edition = "2018" [dependencies] libc = "0.2.58" nix = "0.17.0" -protobuf = "=2.14.0" -protocols = { path = "../protocols" } -slog = { version = "2.5.2", features = ["dynamic-keys", "max_level_trace", "release_max_level_info"] } -slog-json = "2.3.0" -slog-async = "2.3.0" -slog-scope = "4.1.2" + +protobuf = { version = "=2.14.0", optional = true } +protocols = { path = "../protocols", optional = true } +slog = { version = "2.5.2", features = ["dynamic-keys", "max_level_trace", "release_max_level_info"], optional = true } +slog-scope = { version = "4.1.2", optional = true } scan_fmt = "0.2.3" + +[features] +with-log = ["slog", "slog-scope"] +with-agent-handler = ["protobuf", "protocols"] diff --git a/src/agent/netlink/src/agent_handler.rs b/src/agent/netlink/src/agent_handler.rs index 84b50b21df..a5260f448e 100644 --- a/src/agent/netlink/src/agent_handler.rs +++ b/src/agent/netlink/src/agent_handler.rs @@ -9,6 +9,7 @@ use protobuf::RepeatedField; use protocols::types::{ARPNeighbor, IPAddress, IPFamily, Interface, Route}; +#[cfg(feature = "with-log")] // Convenience macro to obtain the scope logger macro_rules! sl { () => { diff --git a/src/agent/netlink/src/lib.rs b/src/agent/netlink/src/lib.rs index d302fd8f35..bd26243d1b 100644 --- a/src/agent/netlink/src/lib.rs +++ b/src/agent/netlink/src/lib.rs @@ -9,17 +9,18 @@ #![allow(unused_parens)] #![allow(unused_unsafe)] -mod agent_handler; - extern crate libc; extern crate nix; + +#[cfg(feature = "with-agent-handler")] extern crate protobuf; +#[cfg(feature = "with-agent-handler")] extern crate protocols; +#[cfg(feature = "with-log")] #[macro_use] extern crate slog; -extern crate slog_async; -extern crate slog_json; +#[cfg(feature = "with-log")] extern crate slog_scope; #[macro_use] @@ -33,8 +34,7 @@ use std::mem; use std::net::{Ipv4Addr, Ipv6Addr}; use std::str::FromStr; -type Result = std::result::Result; - +#[cfg(feature = "with-log")] // Convenience macro to obtain the scope logger macro_rules! sl { () => { @@ -42,6 +42,19 @@ macro_rules! sl { }; } +#[cfg(not(feature = "with-log"))] +#[macro_export] +macro_rules! info { + ($l:expr, #$tag:expr, $($args:tt)*) => {}; + ($l:expr, $($args:tt)*) => {}; +} + +#[cfg(feature = "with-agent-handler")] +mod agent_handler; + +/// Specialized version of std::result::Result for Netlink related operations. +pub type Result = std::result::Result; + // define the struct, const, etc needed by netlink operations pub type __s8 = libc::c_char;