From 3fbb8eb237b0d4db03269dd61ada06e93de2a952 Mon Sep 17 00:00:00 2001 From: elkowar <5300871+elkowar@users.noreply.github.com> Date: Sat, 17 Oct 2020 20:23:32 +0200 Subject: [PATCH] write to log file --- docs/config-docs.md | 24 ++++++++--------- docs/{ => images}/one.png | Bin docs/{ => images}/two.png | Bin src/main.rs | 53 +++++++++++++++++++++----------------- 4 files changed, 41 insertions(+), 36 deletions(-) rename docs/{ => images}/one.png (100%) rename docs/{ => images}/two.png (100%) diff --git a/docs/config-docs.md b/docs/config-docs.md index 1868f93..1e8bb4f 100644 --- a/docs/config-docs.md +++ b/docs/config-docs.md @@ -32,7 +32,7 @@ Configured in XML and themed using CSS, it is easy to customize and provides all - [Something isn't styled correctly!](#something-isnt-styled-correctly) -## How to install Eww +## How to install Eww ### Prerequisites @@ -112,12 +112,12 @@ You can then reference it in your widgets by doing: To change the value of the variable, and thus change the UI, you can run `eww update banana "I like apples"` -#### The `` tag +#### The `` tag Allows you to create a script that eww runs. Useful for creating volume sliders or anything similar. -Example: +Example: ```xml @@ -135,12 +135,12 @@ and then reference it by doing: The `interval="5s"` part says how long time it should take before Eww runs the command again. Here are the available times you can set: -| Shortened | Full name | -| :------------- | :----------:| -| ms | Miliseconds | +| Shortened | Full name | +| :------------- | :----------:| +| ms | Miliseconds | | s | Seconds | -| m | Minutes | -| h | Hours | +| m | Minutes | +| h | Hours | #### Tail If you don't want a set interval and instead want it to tail (run the script when it detects a change is present) you can simply remove the `interval="5s"` so it becomes: @@ -189,8 +189,8 @@ This part: ``` -Is the custom widget. As we can see by the -```xml +Is the custom widget. As we can see by the +```xml ``` the widget is called `clock.`Or referenced `` @@ -204,7 +204,7 @@ So if we look at: ``` -we can see that we assign `{{my_time}}` to be `{{date}}` and if we look at +we can see that we assign `{{my_time}}` to be `{{date}}` and if we look at ```xml date @@ -309,7 +309,7 @@ we will see that eww will run `` and not ``. ### Widgets made in Eww -![two](two.png) ![one](one.png) +![two](images/two.png) ![one](images/one.png) ## GTK diff --git a/docs/one.png b/docs/images/one.png similarity index 100% rename from docs/one.png rename to docs/images/one.png diff --git a/docs/two.png b/docs/images/two.png similarity index 100% rename from docs/two.png rename to docs/images/two.png diff --git a/src/main.rs b/src/main.rs index 9d09e9a..79f5700 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, io::{Read, Write}, - os::unix::net, + os::unix::{io::AsRawFd, net}, path::{Path, PathBuf}, }; use structopt::StructOpt; @@ -57,6 +57,11 @@ lazy_static::lazy_static! { .map(|v| PathBuf::from(v)) .unwrap_or_else(|_| PathBuf::from(std::env::var("HOME").unwrap()).join(".config")) .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() { @@ -257,40 +262,40 @@ fn run_filewatch_thread>( Ok(hotwatch) } -/// detach the process from the terminal, also closing stdout and redirecting -/// stderr into /dev/null +/// detach the process from the terminal, also redirecting stdout and stderr to +/// LOG_FILE fn do_detach() { // detach from terminal let pid = unsafe { libc::fork() }; - if dbg!(pid) < 0 { + if pid < 0 { panic!("Phailed to Phork: {}", std::io::Error::last_os_error()); } if pid != 0 { std::process::exit(0); } - // close stdout to not spam output - if unsafe { libc::isatty(1) } != 0 { - unsafe { - libc::close(1); - } - } - // close stderr to not spam output - if unsafe { libc::isatty(2) } != 0 { - unsafe { - let fd = libc::open(std::ffi::CString::new("/dev/null").unwrap().as_ptr(), libc::O_RDWR); - if fd < 0 { - panic!("Phailed to open /dev/null?!: {}", std::io::Error::last_os_error()); - } else { - if libc::dup2(fd, libc::STDERR_FILENO) < 0 { - panic!( - "Phailed to dup stderr phd to /dev/null: {:?}", - std::io::Error::last_os_error() - ); - } - libc::close(fd); + let file = std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(&*LOG_FILE) + .expect(&format!( + "Error opening log file ({}), for writing", + &*LOG_FILE.to_string_lossy() + )); + let fd = file.as_raw_fd(); + + unsafe { + if libc::isatty(1) != 0 { + if libc::dup2(fd, libc::STDOUT_FILENO) < 0 { + panic!("Phailed to dup stdout to log file: {:?}", std::io::Error::last_os_error()); } } + 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); } }