Skip to content

Commit

Permalink
Add file logger (#5)
Browse files Browse the repository at this point in the history
The copy will put the program into background, in which case we also
need ways to debug.

And bump the version for the public release.
  • Loading branch information
beeender authored Jan 7, 2025
1 parent 52c62c9 commit 20809f9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 40 deletions.
62 changes: 31 additions & 31 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "richclip"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
authors = ["beeender"]
build = "build.rs"
Expand All @@ -9,13 +9,13 @@ build = "build.rs"
anyhow = "1.0.94"
clap = "4.5.23"
daemonize = "0.5.0"
env_logger = "0.11.5"
log = "0.4.22"
wayrs-client = "1.1.3"
wayrs-protocols = { version = "0.14.4", features = ["wlr-data-control-unstable-v1"] }
libc = "0.2.168"
xcb = { version = "1.5.0", features = [] }
x11rb = { version = "0.13.1", features = [] }
simplelog = "0.12.2"

[build-dependencies]
vergen-git2 = { version = "1.0.2", features = ["build", "cargo"] }
47 changes: 40 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
extern crate clap;
extern crate daemonize;
extern crate env_logger;
extern crate log;
extern crate simplelog;

mod clipboard;
mod protocol;

use clap::{Arg, ArgMatches, Command, value_parser};
use clap::{value_parser, Arg, ArgMatches, Command};
use daemonize::Daemonize;
use std::env;
use std::fs::File;
use std::io::{stdin, stdout};
use std::str::FromStr;

enum Backend {
Wayland,
Expand Down Expand Up @@ -98,8 +100,39 @@ fn cli() -> Command {
)
}

fn init_logger() {
use simplelog::{
ColorChoice, CombinedLogger, ConfigBuilder, LevelFilter, SharedLogger, TermLogger,
TerminalMode, WriteLogger,
};

let log_path = env::var("RICHCLIP_LOG_FILE").unwrap_or("".to_string());
let level_str = env::var("RICHCLIP_LOG_LEVEL").unwrap_or("Warn".to_string());
let level = LevelFilter::from_str(&level_str).unwrap_or(log::LevelFilter::Warn);
let config = ConfigBuilder::default()
.set_time_offset_to_local()
.unwrap()
.build();
let mut loggers: Vec<Box<dyn SharedLogger>> = vec![TermLogger::new(
level,
config.clone(),
TerminalMode::Mixed,
ColorChoice::Auto,
)];
if !log_path.is_empty() {
let log_file = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(log_path)
.expect("Cannot open the log file at '$RICHCLIP_LOG_FILE'");
loggers.push(WriteLogger::new(LevelFilter::Debug, config, log_file));
}
CombinedLogger::init(loggers).unwrap();
}

fn main() {
env_logger::init();
init_logger();

let matches = cli().get_matches();
match matches.subcommand() {
Some(("copy", sub_matches)) => {
Expand Down Expand Up @@ -129,14 +162,14 @@ fn do_copy(arg_matches: &ArgMatches) {
// descriptor isn't kept alive, and chdir to the root, to prevent blocking file systems from
// being unmounted.
// The above is copied from wl-clipboard.
let in_null = File::create("/dev/null").unwrap();
let out_null = File::create("/dev/null").unwrap();
let err_null = File::create("/dev/null").unwrap();

if ! foreground {
if !foreground {
let daemonize = Daemonize::new()
.working_directory("/") // prevent blocking fs from being unmounted.
.stdout(in_null)
.stderr(out_null);
.stdout(out_null)
.stderr(err_null);

// wl-clipboard does this
ignore_sighub();
Expand Down

0 comments on commit 20809f9

Please sign in to comment.