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

@ -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 `<script-var>` tag
#### The `<script-var>` tag
Allows you to create a script that eww runs.
Useful for creating volume sliders or anything similar.
Example:
Example:
```xml
<variables>
<script-var name="date" interval="5s">
@ -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:
</box>
</def>
```
Is the custom widget. As we can see by the
```xml
Is the custom widget. As we can see by the
```xml
<def name="clock">
```
the widget is called `clock.`Or referenced `<clock>`
@ -204,7 +204,7 @@ So if we look at:
</box>
</def>
```
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
<script-var name="date">
date
@ -309,7 +309,7 @@ we will see that eww will run `<def name="main">` and not `<def name="clock">`.
### Widgets made in Eww
![two](two.png) ![one](one.png)
![two](images/two.png) ![one](images/one.png)
## 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::{
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<P: AsRef<Path>>(
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);
}
}