Skip to content

Commit

Permalink
Refactor logging statements to use log crate instead of tracing crate
Browse files Browse the repository at this point in the history
  • Loading branch information
LeChatP committed Oct 20, 2024
1 parent ef8e710 commit 0d9daec
Show file tree
Hide file tree
Showing 29 changed files with 64 additions and 103 deletions.
2 changes: 1 addition & 1 deletion rar-common/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use capctl::CapSet;
use serde_json::Value;
use strum::EnumIs;
#[cfg(feature = "finder")]
use tracing::debug;
use log::debug;

#[cfg(feature = "finder")]
use crate::database::finder::{Cred, ExecSettings, FilterMatcher, TaskMatch, UserMin};
Expand Down
6 changes: 3 additions & 3 deletions rar-common/src/database/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use nix::{
#[cfg(feature = "pcre2")]
use pcre2::bytes::RegexBuilder;
use strum::EnumIs;
use tracing::{debug, warn};
use log::{debug, warn};

use crate::database::{
options::{Opt, OptStack},
Expand Down Expand Up @@ -488,7 +488,7 @@ fn is_root(actortype: &SActorType) -> bool {
fn groups_contains_root(list: Option<&SGroups>) -> bool {
if let Some(list) = list {
match list {
SGroups::Single(group) => is_root(group),
SGroups::Single(group) => is_root(&group),
SGroups::Multiple(groups) => groups.iter().any(is_root),
}
} else {
Expand Down Expand Up @@ -703,7 +703,7 @@ impl CredMatcher for Rc<RefCell<SRole>> {
}
}
SActor::Unknown(element) => {
let min = PluginManager::notify_user_matcher(&as_borrow!(self), user, element);
let min = PluginManager::notify_user_matcher(&as_borrow!(self), user, &element);
if !min.is_no_match() {
return Some(min);
}
Expand Down
2 changes: 1 addition & 1 deletion rar-common/src/database/migration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::error::Error;

use semver::Version;
use tracing::debug;
use log::debug;

use crate::version::PACKAGE_VERSION;

Expand Down
2 changes: 1 addition & 1 deletion rar-common/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::version::PACKAGE_VERSION;
use chrono::Duration;
use linked_hash_set::LinkedHashSet;
use serde::{de, Deserialize, Serialize};
use tracing::debug;
use log::debug;

use self::{migration::Migration, options::EnvKey, structs::SConfig, versionning::Versioning};

Expand Down
4 changes: 2 additions & 2 deletions rar-common/src/database/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Map, Value};
use strum::{Display, EnumIs, EnumIter, FromRepr};

use tracing::debug;
use log::debug;
#[cfg(feature = "finder")]
use tracing::warn;
use log::warn;

use crate::rc_refcell;

Expand Down
2 changes: 1 addition & 1 deletion rar-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const ROOTASROLE: &str = "target/rootasrole.json";
use std::{cell::RefCell, error::Error, ffi::OsStr, path::PathBuf, rc::Rc};

use serde::{Deserialize, Serialize};
use tracing::debug;
use log::debug;

pub mod api;
pub mod database;
Expand Down
2 changes: 1 addition & 1 deletion rar-common/src/plugin/hashchecker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
};
use nix::unistd::{access, AccessFlags};
use serde::{Deserialize, Serialize};
use tracing::{debug, warn};
use log::{debug, warn};

use libc::FS_IOC_GETFLAGS;
use sha2::Digest;
Expand Down
2 changes: 1 addition & 1 deletion rar-common/src/plugin/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};

use serde::Deserialize;
use tracing::{debug, warn};
use log::{debug, warn};

#[derive(Deserialize)]
pub struct Parents(Vec<String>);
Expand Down
49 changes: 11 additions & 38 deletions rar-common/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{
env,
error::Error,
ffi::CString,
fs::File,
io,
os::{fd::AsRawFd, unix::fs::MetadataExt},
Expand All @@ -13,8 +12,7 @@ use capctl::{Cap, CapSet, ParseCapError};
use libc::{FS_IOC_GETFLAGS, FS_IOC_SETFLAGS};
use serde::Serialize;
use strum::EnumIs;
use tracing::{debug, warn, Level};
use tracing_subscriber::util::SubscriberInitExt;
use log::{debug, warn};

#[cfg(feature = "finder")]
use crate::api::PluginManager;
Expand Down Expand Up @@ -257,45 +255,20 @@ pub fn final_path(path: &str) -> PathBuf {
}

#[cfg(debug_assertions)]
pub fn subsribe(tool: &str) {
use std::io;
let identity = CString::new(tool).unwrap();
let options = syslog_tracing::Options::LOG_PID;
let facility = syslog_tracing::Facility::Auth;
let _syslog = syslog_tracing::Syslog::new(identity, options, facility).unwrap();
tracing_subscriber::fmt()
.with_max_level(Level::DEBUG)
.with_file(true)
.with_line_number(true)
.with_writer(io::stdout)
.finish()
pub fn subsribe(tool: &str) -> Result<(), Box<dyn Error>> {
env_logger::Builder::from_default_env()
.format_module_path(true)
.init();
Ok(())
}

#[cfg(not(debug_assertions))]
pub fn subsribe(tool: &str) {
use std::panic::set_hook;

let identity = CString::new(tool).unwrap();
let options = syslog_tracing::Options::LOG_PID;
let facility = syslog_tracing::Facility::Auth;
let syslog = syslog_tracing::Syslog::new(identity, options, facility).unwrap();
tracing_subscriber::fmt()
.compact()
.with_max_level(Level::WARN)
.with_file(false)
.with_timer(false)
.with_line_number(false)
.with_target(false)
.without_time()
.with_writer(syslog)
.finish()
.init();
set_hook(Box::new(|info| {
if let Some(s) = info.payload().downcast_ref::<String>() {
println!("{}", s);
}
}));
pub fn subsribe(tool: &str) -> Result<(), Box<dyn Error>> {
use env_logger::Env;
use syslog::{BasicLogger, Facility, Formatter3164};
use log::LevelFilter;
syslog::init(Facility::LOG_AUTH, LevelFilter::Info, Some(tool))?;
Ok(())
}

pub fn drop_effective() -> Result<(), capctl::Error> {
Expand Down
15 changes: 4 additions & 11 deletions src/chsr/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use data::{Cli, Inputs, Rule};
use pair::recurse_pair;
use pest::Parser;
use process::process_input;
use tracing::debug;
use log::debug;
use usage::print_usage;

use crate::util::escape_parser_string_vec;
Expand Down Expand Up @@ -58,18 +58,11 @@ mod tests {
use super::*;
use capctl::Cap;
use chrono::TimeDelta;
use tracing::error;
use tracing_subscriber::util::SubscriberInitExt;
use log::error;
use test_log::test;


fn setup(name: &str) {
use std::io;
let _ = tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.with_file(true)
.with_line_number(true)
.with_writer(io::stdout)
.finish()
.try_init();
//Write json test json file
let path = format!("{}.{}", ROOTASROLE, name);
let mut file = std::fs::File::create(path.clone()).unwrap_or_else(|_| {
Expand Down
2 changes: 1 addition & 1 deletion src/chsr/cli/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use capctl::{Cap, CapSet};
use chrono::Duration;
use linked_hash_set::LinkedHashSet;
use pest::iterators::Pair;
use tracing::{debug, warn};
use log::{debug, warn};

use crate::cli::data::{RoleType, TaskType};
use rar_common::database::{
Expand Down
2 changes: 1 addition & 1 deletion src/chsr/cli/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{cell::RefCell, error::Error, rc::Rc};

use json::*;

use tracing::debug;
use log::debug;

use rar_common::{
database::{
Expand Down
2 changes: 1 addition & 1 deletion src/chsr/cli/process/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
};

use linked_hash_set::LinkedHashSet;
use tracing::debug;
use log::debug;

use crate::cli::data::{InputAction, RoleType, SetListType, TaskType, TimeoutOpt};

Expand Down
2 changes: 1 addition & 1 deletion src/chsr/cli/usage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::error::Error;

use const_format::formatcp;
use tracing::debug;
use log::debug;

use super::data::Rule;
use crate::util::underline;
Expand Down
4 changes: 2 additions & 2 deletions src/chsr/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rar_common::{
util::{drop_effective, read_effective, subsribe},
Storage,
};
use tracing::{debug, error};
use log::{debug, error};

mod cli;
mod util;
Expand All @@ -20,7 +20,7 @@ const ROOTASROLE: &str = "target/rootasrole.json";
fn main() -> Result<(), Box<dyn std::error::Error>> {
use rar_common::{get_settings, StorageMethod};

subsribe("chsr");
subsribe("chsr")?;
drop_effective()?;
register_plugins();
let settings = get_settings(ROOTASROLE).expect("Error on config read");
Expand Down
4 changes: 2 additions & 2 deletions src/sr/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rar_common::util::escape_parser_string;
use pam::PAM_PROMPT;
use pty_process::blocking::{Command, Pty};
use std::{cell::RefCell, error::Error, io::stdout, os::fd::AsRawFd, rc::Rc};
use tracing::{debug, error};
use log::{debug, error};

use rar_common::plugin::register_plugins;
use rar_common::{
Expand Down Expand Up @@ -187,7 +187,7 @@ where
fn main() -> Result<(), Box<dyn Error>> {
use crate::{pam::check_auth, ROOTASROLE};

subsribe("sr");
subsribe("sr")?;
drop_effective()?;
register_plugins();
let args = std::env::args();
Expand Down
2 changes: 1 addition & 1 deletion src/sr/pam/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use pam_client::{Context, ConversationHandler, ErrorCode, Flag};
use pcre2::bytes::RegexBuilder;
use tracing::{debug, error, info, warn};
use log::{debug, error, info, warn};

use crate::timeout;
use rar_common::{
Expand Down
2 changes: 1 addition & 1 deletion src/sr/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use nix::{
sys::signal::kill,
};
use serde::{Deserialize, Serialize};
use tracing::debug;
use log::debug;

use rar_common::{
database::{
Expand Down
4 changes: 2 additions & 2 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ strum = { version = "0.26.3", features = ["derive"] }
capctl = "0.2.4"
nix = { version = "0.29.0", features = ["user","process", "signal", "fs"] }
glob = "0.3.1"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
log = "0.4.22"
env_logger = "0.11.5"

[features]
default = ["cli", "ebpf", "deploy"]
Expand Down
8 changes: 5 additions & 3 deletions xtask/src/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::Path;
use anyhow::Context;
use nix::unistd::{getresuid, getuid};
use strum::EnumIs;
use tracing::{info, warn};
use log::{info, warn};

use crate::util::{
files_are_equal, toggle_lock_config, ImmutableLock, OsTarget, SettingsFile, ROOTASROLE,
Expand Down Expand Up @@ -232,7 +232,7 @@ fn retrieve_real_user() -> Result<Option<nix::unistd::User>, anyhow::Error> {
}
}

pub fn default_pam_path(os: &OsTarget) -> &'static str {
pub fn pam_config(os: &OsTarget) -> &'static str {
match os {
OsTarget::Debian | OsTarget::Ubuntu => {
include_str!("../../resources/debian/deb_sr_pam.conf")
Expand All @@ -245,7 +245,9 @@ pub fn default_pam_path(os: &OsTarget) -> &'static str {
fn deploy_pam_config(os: &OsTarget) -> io::Result<u64> {
if fs::metadata(PAM_CONFIG_PATH).is_err() {
info!("Deploying PAM configuration file");
return fs::copy(default_pam_path(os), PAM_CONFIG_PATH);
let mut pam_conf = File::create(PAM_CONFIG_PATH)?;
pam_conf.write_all(pam_config(os).as_bytes())?;
pam_conf.sync_all()?;
}
Ok(0)
}
Expand Down
2 changes: 1 addition & 1 deletion xtask/src/deploy/debian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use anyhow::Context;
use tracing::debug;
use log::debug;

use crate::{
installer::{self, dependencies::install_dependencies, InstallDependenciesOptions, Profile},
Expand Down
11 changes: 8 additions & 3 deletions xtask/src/installer/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{fs, os::unix, process::Command};

use anyhow::Context;
use tracing::debug;
use log::debug;

use crate::util::change_dir_to_git_root;
use crate::{installer::Toolchain, util::change_dir_to_git_root};

use super::BuildOptions;

Expand All @@ -12,8 +12,13 @@ fn build_binary(
options: &BuildOptions,
additionnal_args: Vec<&str>,
) -> Result<(), anyhow::Error> {

let toolchain = format!("+{}", options.toolchain);
let mut args = vec![&toolchain, "build", "--bin", name];
let mut args = if options.toolchain == Toolchain::default() {
vec!["build", "--bin", name]
} else {
vec![&toolchain, "build", "--bin", name]
};
if options.profile.is_release() {
args.push("--release");
}
Expand Down
2 changes: 1 addition & 1 deletion xtask/src/installer/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::process::ExitStatus;
use anyhow::Context;
use capctl::CapState;
use nix::unistd::geteuid;
use tracing::info;
use log::info;

use crate::{installer::OsTarget, util::get_os};

Expand Down
Loading

0 comments on commit 0d9daec

Please sign in to comment.