Files
LLeMbas-TTY/config/src/daemon.rs
T
Wez Furlong 955b37ff64 mux server: redirect stdout,stderr to log file on Windows
This got lost somewhere along the way.

Importantly, we need to explicitly set the pty to use invalid stdio
handles for the spawned child in order to avoid a weird situation
where eg: cmd or powershell would end up writing to the log file
despite it being spawned into its own PTY.

refs: #1358
2021-12-05 10:11:43 -08:00

56 lines
1.4 KiB
Rust

use crate::*;
use std::fs::{File, OpenOptions};
use std::path::PathBuf;
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct DaemonOptions {
pub pid_file: Option<PathBuf>,
pub stdout: Option<PathBuf>,
pub stderr: Option<PathBuf>,
}
impl_lua_conversion!(DaemonOptions);
fn open_log(path: PathBuf) -> anyhow::Result<File> {
create_user_owned_dirs(
path.parent()
.ok_or_else(|| anyhow!("path {} has no parent dir!?", path.display()))?,
)?;
let mut options = OpenOptions::new();
options.write(true).create(true).append(true);
options
.open(&path)
.map_err(|e| anyhow!("failed to open log stream: {}: {}", path.display(), e))
}
impl DaemonOptions {
#[cfg_attr(windows, allow(dead_code))]
pub fn pid_file(&self) -> PathBuf {
self.pid_file
.as_ref()
.cloned()
.unwrap_or_else(|| RUNTIME_DIR.join("pid"))
}
pub fn stdout(&self) -> PathBuf {
self.stdout
.as_ref()
.cloned()
.unwrap_or_else(|| RUNTIME_DIR.join("log"))
}
pub fn stderr(&self) -> PathBuf {
self.stderr
.as_ref()
.cloned()
.unwrap_or_else(|| RUNTIME_DIR.join("log"))
}
pub fn open_stdout(&self) -> anyhow::Result<File> {
open_log(self.stdout())
}
pub fn open_stderr(&self) -> anyhow::Result<File> {
open_log(self.stderr())
}
}