~goorzhel/radm

7b22db1474e4019759faee2a93b4e6d613e414a6 — Antonio Gurgel 3 months ago 13ee578
Implement init_log_file and init_logger
5 files changed, 56 insertions(+), 7 deletions(-)

M Cargo.lock
M Cargo.toml
M res/rstdm.1.md
M src/cli.rs
M src/main.rs
M Cargo.lock => Cargo.lock +19 -0
@@ 552,6 552,7 @@ dependencies = [
 "nix",
 "pam",
 "pwd",
 "simplelog",
 "syslog",
 "xdg",
]


@@ 569,6 570,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"

[[package]]
name = "simplelog"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48dfff04aade74dd495b007c831cd6f4e0cee19c344dd9dc0884c0289b70a786"
dependencies = [
 "log",
 "termcolor",
 "time",
]

[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 667,9 679,16 @@ dependencies = [
 "itoa",
 "libc",
 "num_threads",
 "time-macros",
]

[[package]]
name = "time-macros"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42657b1a6f4d817cda8e7a0ace261fe0cc946cf3a80314390b22cc61ae080792"

[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"

M Cargo.toml => Cargo.toml +1 -0
@@ 10,6 10,7 @@ configparser = "3.0"
console = "0.15"
log = "0.4"
nix = "0.15"
simplelog = "0.12.0"
syslog = "6.0"
xdg = "2.4"


M res/rstdm.1.md => res/rstdm.1.md +3 -0
@@ 22,6 22,9 @@ rstdm \[-dv\] \[-f \<TTY\>\] \[-s \<PATH\>\]...
-f, --focus-on-start \<TTY\>
: Focus the given TTY when rstdm starts.

-l, --log-file \<PATH\>
: Log to this path instead of syslog.

-h, --help
: Print help information.


M src/cli.rs => src/cli.rs +31 -5
@@ 1,10 1,14 @@
//! Command-line interface.

use std::{fs::read_dir, path::PathBuf};
use std::{
    fs::{read_dir, OpenOptions},
    path::{Path, PathBuf},
};

use anyhow::{anyhow, Result};
use anyhow::{anyhow, Context, Result};
use clap::Parser;
use log::LevelFilter;
use simplelog::WriteLogger;
use syslog::{BasicLogger, Facility, Formatter3164};
use xdg::BaseDirectories;



@@ 27,6 31,9 @@ pub struct Options {
    /// Include this folder when searching for sessions (can be used more than once).
    #[clap(short, long, name = "PATH")]
    pub session_dirs: Vec<PathBuf>,
    /// Log to this path instead of syslog.
    #[clap(short, long, name = "PATH")]
    pub log_file: Option<String>,
}

impl Options {


@@ 71,7 78,7 @@ impl Options {
}

/// Connects to a running syslog instance.
pub fn get_syslog(opts: &Options) -> Result<()> {
pub fn init_syslog(verbosity: LevelFilter) -> Result<()> {
    let formatter = Formatter3164 {
        facility: Facility::LOG_AUTH,
        hostname: None,


@@ 81,11 88,30 @@ pub fn get_syslog(opts: &Options) -> Result<()> {
    let logger =
        syslog::unix(formatter).map_err(|e| anyhow!("Couldn't connect to syslog: {}", e))?;
    log::set_boxed_logger(Box::new(BasicLogger::new(logger)))?;
    log::set_max_level(opts.verbosity());
    debug!("Logger initialized");
    log::set_max_level(verbosity);
    debug!("Syslog initialized");
    Ok(())
}

/// Initializes a file as a log sink.
pub fn init_log_file(path: &Path, verbosity: LevelFilter) -> Result<()> {
    let file = OpenOptions::new()
        .append(true)
        .create(true)
        .open(&path)
        .with_context(|| anyhow!("Couldn't open {}", &path.display()))?;
    WriteLogger::init(verbosity, Default::default(), file)?;
    debug!("Logfile {} initialized", path.display());
    Ok(())
}

pub fn init_logger(opts: &Options) -> Result<()> {
    match &opts.log_file {
        Some(path) => init_log_file(Path::new(&path), opts.verbosity()),
        None => init_syslog(opts.verbosity()),
    }
}

#[derive(clap::ArgEnum, Debug, Clone)]
/// Whether a session should be run with D-Bus.
pub enum Dbus {

M src/main.rs => src/main.rs +2 -2
@@ 20,7 20,7 @@ mod user;

use crate::{
    auth::{login, Authenticator},
    cli::{get_syslog, Options},
    cli::{init_logger, Options},
    session::find_sessions,
    system::{drop_privileges, prepare_xdg_runtime},
    tui::{prompt, EchoInput},


@@ 37,7 37,7 @@ fn main() {

fn run() -> Result<()> {
    let opts = Options::new();
    get_syslog(&opts)?;
    init_logger(&opts)?;

    info!("Starting rstdm.");
    println!(include_str!("wordmark.txt"));