From 37fed01642e67028cc2280d45a9ccebc7b4d4c07 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 3 Jul 2023 12:57:41 -0700 Subject: [PATCH] FLOG to stop depending on the ffi Prior to this commit, FLOG used the ffi bridge to get the output fd. Invert this: have fish set the output fd within main. This allows FLOG to be used in pure Rust tests. --- fish-rust/src/ffi.rs | 1 - fish-rust/src/ffi_init.rs | 6 ++++++ fish-rust/src/flog.rs | 20 ++++++++++++++++---- src/fish.cpp | 1 + 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/fish-rust/src/ffi.rs b/fish-rust/src/ffi.rs index 54403653a..95f4f567c 100644 --- a/fish-rust/src/ffi.rs +++ b/fish-rust/src/ffi.rs @@ -71,7 +71,6 @@ include_cpp! { generate!("make_pipes_ffi") - generate!("get_flog_file_fd") generate!("log_extra_to_flog_file") generate!("fish_wcwidth") diff --git a/fish-rust/src/ffi_init.rs b/fish-rust/src/ffi_init.rs index 1e01b3cf5..71e8ed629 100644 --- a/fish-rust/src/ffi_init.rs +++ b/fish-rust/src/ffi_init.rs @@ -13,6 +13,7 @@ mod ffi2 { extern "Rust" { fn rust_init(); fn rust_activate_flog_categories_by_pattern(wc_ptr: wcharz_t); + fn rust_set_flog_file_fd(fd: i32); fn rust_invalidate_numeric_locale(); } } @@ -29,6 +30,11 @@ fn rust_activate_flog_categories_by_pattern(wc_ptr: wcharz_t) { crate::flog::activate_flog_categories_by_pattern(wc_ptr.into()); } +/// FFI bridge for setting FLOG file descriptor. +fn rust_set_flog_file_fd(fd: i32) { + crate::flog::set_flog_file_fd(fd as libc::c_int); +} + /// FFI bridge to invalidate cached locale bits. fn rust_invalidate_numeric_locale() { locale::invalidate_numeric_locale(); diff --git a/fish-rust/src/flog.rs b/fish-rust/src/flog.rs index 56232c786..d6cac1c67 100644 --- a/fish-rust/src/flog.rs +++ b/fish-rust/src/flog.rs @@ -1,11 +1,12 @@ -use crate::ffi::{get_flog_file_fd, wildcard_match}; +use crate::ffi::wildcard_match; use crate::parse_util::parse_util_unescape_wildcards; use crate::wchar::{widestrs, wstr, WString}; use crate::wchar_ext::WExt; use crate::wchar_ffi::WCharToFFI; +use libc::c_int; use std::io::Write; -use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd}; -use std::sync::atomic::Ordering; +use std::os::unix::io::{FromRawFd, IntoRawFd}; +use std::sync::atomic::{AtomicI32, Ordering}; #[rustfmt::skip::macros(category)] #[widestrs] @@ -162,7 +163,7 @@ pub trait FloggableDebug: std::fmt::Debug { /// Write to our FLOG file. pub fn flog_impl(s: &str) { - let fd = get_flog_file_fd().0 as RawFd; + let fd = get_flog_file_fd(); if fd < 0 { return; } @@ -240,3 +241,14 @@ pub fn activate_flog_categories_by_pattern(wc_ptr: &wstr) { } } } + +/// The flog output fd. Defaults to stderr. A value < 0 disables flog. +static FLOG_FD: AtomicI32 = AtomicI32::new(libc::STDERR_FILENO); + +pub fn set_flog_file_fd(fd: c_int) { + FLOG_FD.store(fd, Ordering::Relaxed); +} + +pub fn get_flog_file_fd() -> c_int { + FLOG_FD.load(Ordering::Relaxed) +} diff --git a/src/fish.cpp b/src/fish.cpp index eb0c258e3..fb7ccb83e 100644 --- a/src/fish.cpp +++ b/src/fish.cpp @@ -467,6 +467,7 @@ int main(int argc, char **argv) { set_cloexec(fileno(debug_output)); setlinebuf(debug_output); set_flog_output_file(debug_output); + rust_set_flog_file_fd(get_flog_file_fd()); } // No-exec is prohibited when in interactive mode.