1
0
Fork 0
forked from wry/wry

portal: start portal automatically with compositor

This commit is contained in:
Julian Orth 2024-03-19 21:00:36 +01:00
parent 39d1e49672
commit 4d6f226254
14 changed files with 331 additions and 125 deletions

View file

@ -1,8 +1,9 @@
use {
crate::utils::{errorfmt::ErrorFmt, oserror::OsError},
backtrace::Backtrace,
bstr::{BStr, BString, ByteSlice},
bstr::BString,
log::{Level, Log, Metadata, Record},
parking_lot::Mutex,
std::{
cell::Cell,
fs::DirBuilder,
@ -10,12 +11,12 @@ use {
os::unix::{ffi::OsStringExt, fs::DirBuilderExt},
ptr,
sync::{
atomic::{AtomicU32, Ordering::Relaxed},
atomic::{AtomicI32, AtomicU32, Ordering::Relaxed},
Arc,
},
time::SystemTime,
},
uapi::{c, format_ustr, Errno, Fd, OwnedFd},
uapi::{c, format_ustr, Errno, Fd, OwnedFd, Ustring},
};
thread_local! {
@ -24,8 +25,9 @@ thread_local! {
pub struct Logger {
level: AtomicU32,
path: BString,
file: OwnedFd,
path: Mutex<Arc<BString>>,
_file: Mutex<OwnedFd>,
file_fd: AtomicI32,
}
impl Logger {
@ -37,67 +39,31 @@ impl Logger {
fatal!("Error: Could not dup stderr: {}", ErrorFmt(e));
}
};
Self::install(level, b"", file)
Self::install(level, b"STDERR", file)
}
pub fn install_compositor(level: Level) -> Arc<Self> {
let log_dir = create_log_dir();
let (path, file) = 'file: {
for i in 0.. {
let file_name = format_ustr!(
"{}/jay-{}-{}.txt",
log_dir,
humantime::format_rfc3339_millis(SystemTime::now()),
i,
);
match uapi::open(
&file_name,
c::O_CREAT | c::O_EXCL | c::O_CLOEXEC | c::O_WRONLY,
0o644,
) {
Ok(f) => break 'file (file_name, f),
Err(Errno(c::EEXIST)) => {}
Err(e) => {
let e: OsError = e.into();
fatal!("Error: Could not create log file: {}", ErrorFmt(e));
}
}
}
unreachable!();
};
std::panic::set_hook(Box::new(|p| {
if let Some(loc) = p.location() {
log::error!(
"Panic at {} line {} column {}",
loc.file(),
loc.line(),
loc.column()
);
} else {
log::error!("Panic at unknown location");
}
if let Some(msg) = p.payload().downcast_ref::<&str>() {
log::error!("Message: {}", msg);
}
if let Some(msg) = p.payload().downcast_ref::<String>() {
log::error!("Message: {}", msg);
}
log::error!("Backtrace:\n{:?}", Backtrace::new());
}));
let (path, file) = open_log_file("jay");
Self::install(level, path.as_bytes(), file)
}
pub fn install_pipe(file: OwnedFd, level: Level) -> Arc<Self> {
Self::install(level, b"PIPE", file)
}
fn install(level: Level, path: &[u8], file: OwnedFd) -> Arc<Self> {
let slf = Arc::new(Self {
level: AtomicU32::new(level as _),
path: path.to_vec().into(),
file,
path: Mutex::new(Arc::new(path.to_vec().into())),
file_fd: AtomicI32::new(file.raw()),
_file: Mutex::new(file),
});
log::set_boxed_logger(Box::new(LogWrapper {
logger: slf.clone(),
}))
.unwrap();
log::set_max_level(level.to_level_filter());
set_panic_hook();
slf
}
@ -106,17 +72,57 @@ impl Logger {
log::set_max_level(level.to_level_filter());
}
pub fn path(&self) -> &BStr {
self.path.as_bstr()
pub fn path(&self) -> Arc<BString> {
self.path.lock().clone()
}
pub fn redirect(&self, ty: &str) {
let (file, fd) = open_log_file(ty);
log::info!("Redirecting logs to {}", file.display());
*self.path.lock() = Arc::new(file.as_bytes().into());
self.file_fd.store(fd.raw(), Relaxed);
*self._file.lock() = fd;
}
pub fn write_raw(&self, buf: &[u8]) {
let mut fd = Fd::new(self.file_fd.load(Relaxed));
let _ = fd.write_all(buf);
}
}
fn create_log_dir() -> BString {
pub fn open_log_file(ty: &str) -> (Ustring, OwnedFd) {
let log_dir = create_log_dir(ty);
for i in 0.. {
let file_name = format_ustr!(
"{}/{ty}-{}-{}.txt",
log_dir,
humantime::format_rfc3339_millis(SystemTime::now()),
i,
);
match uapi::open(
&file_name,
c::O_CREAT | c::O_EXCL | c::O_CLOEXEC | c::O_WRONLY,
0o644,
) {
Ok(f) => return (file_name, f),
Err(Errno(c::EEXIST)) => {}
Err(e) => {
let e: OsError = e.into();
fatal!("Error: Could not create log file: {}", ErrorFmt(e));
}
}
}
unreachable!()
}
fn create_log_dir(ty: &str) -> BString {
let mut log_dir = match dirs::data_local_dir() {
Some(d) => d,
None => fatal!("Error: $HOME is not set"),
};
log_dir.push("jay");
log_dir.push("logs");
log_dir.push(ty);
let res = DirBuilder::new()
.recursive(true)
.mode(0o755)
@ -131,6 +137,28 @@ fn create_log_dir() -> BString {
log_dir.into_os_string().into_vec().into()
}
fn set_panic_hook() {
std::panic::set_hook(Box::new(|p| {
if let Some(loc) = p.location() {
log::error!(
"Panic at {} line {} column {}",
loc.file(),
loc.line(),
loc.column()
);
} else {
log::error!("Panic at unknown location");
}
if let Some(msg) = p.payload().downcast_ref::<&str>() {
log::error!("Message: {}", msg);
}
if let Some(msg) = p.payload().downcast_ref::<String>() {
log::error!("Message: {}", msg);
}
log::error!("Backtrace:\n{:?}", Backtrace::new());
}));
}
struct LogWrapper {
logger: Arc<Logger>,
}
@ -170,7 +198,7 @@ impl Log for LogWrapper {
record.args(),
)
};
let mut fd = Fd::new(self.logger.file.raw());
let mut fd = Fd::new(self.logger.file_fd.load(Relaxed));
let _ = fd.write_all(buffer);
}