write to log file

This commit is contained in:
elkowar 2020-10-17 20:23:32 +02:00
parent e00685edcd
commit 3fbb8eb237
4 changed files with 41 additions and 36 deletions

View file

@ -309,7 +309,7 @@ we will see that eww will run `<def name="main">` and not `<def name="clock">`.
### Widgets made in Eww ### Widgets made in Eww
![two](two.png) ![one](one.png) ![two](images/two.png) ![one](images/one.png)
## GTK ## GTK

View file

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View file

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 25 KiB

View file

@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
use std::{ use std::{
collections::HashMap, collections::HashMap,
io::{Read, Write}, io::{Read, Write},
os::unix::net, os::unix::{io::AsRawFd, net},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use structopt::StructOpt; use structopt::StructOpt;
@ -57,6 +57,11 @@ lazy_static::lazy_static! {
.map(|v| PathBuf::from(v)) .map(|v| PathBuf::from(v))
.unwrap_or_else(|_| PathBuf::from(std::env::var("HOME").unwrap()).join(".config")) .unwrap_or_else(|_| PathBuf::from(std::env::var("HOME").unwrap()).join(".config"))
.join("eww"); .join("eww");
static ref LOG_FILE: std::path::PathBuf = std::env::var("XDG_CACHE_HOME")
.map(|v| PathBuf::from(v))
.unwrap_or_else(|_| PathBuf::from(std::env::var("HOME").unwrap()).join(".cache"))
.join("eww.log");
} }
fn main() { fn main() {
@ -257,40 +262,40 @@ fn run_filewatch_thread<P: AsRef<Path>>(
Ok(hotwatch) Ok(hotwatch)
} }
/// detach the process from the terminal, also closing stdout and redirecting /// detach the process from the terminal, also redirecting stdout and stderr to
/// stderr into /dev/null /// LOG_FILE
fn do_detach() { fn do_detach() {
// detach from terminal // detach from terminal
let pid = unsafe { libc::fork() }; let pid = unsafe { libc::fork() };
if dbg!(pid) < 0 { if pid < 0 {
panic!("Phailed to Phork: {}", std::io::Error::last_os_error()); panic!("Phailed to Phork: {}", std::io::Error::last_os_error());
} }
if pid != 0 { if pid != 0 {
std::process::exit(0); std::process::exit(0);
} }
// close stdout to not spam output let file = std::fs::OpenOptions::new()
if unsafe { libc::isatty(1) } != 0 { .create(true)
unsafe { .append(true)
libc::close(1); .open(&*LOG_FILE)
} .expect(&format!(
} "Error opening log file ({}), for writing",
// close stderr to not spam output &*LOG_FILE.to_string_lossy()
if unsafe { libc::isatty(2) } != 0 { ));
unsafe { let fd = file.as_raw_fd();
let fd = libc::open(std::ffi::CString::new("/dev/null").unwrap().as_ptr(), libc::O_RDWR);
if fd < 0 { unsafe {
panic!("Phailed to open /dev/null?!: {}", std::io::Error::last_os_error()); if libc::isatty(1) != 0 {
} else { if libc::dup2(fd, libc::STDOUT_FILENO) < 0 {
if libc::dup2(fd, libc::STDERR_FILENO) < 0 { panic!("Phailed to dup stdout to log file: {:?}", std::io::Error::last_os_error());
panic!(
"Phailed to dup stderr phd to /dev/null: {:?}",
std::io::Error::last_os_error()
);
}
libc::close(fd);
} }
} }
if libc::isatty(2) != 0 {
if libc::dup2(fd, libc::STDERR_FILENO) < 0 {
panic!("Phailed to dup stderr to log file: {:?}", std::io::Error::last_os_error());
}
}
libc::close(fd);
} }
} }