From 7635440a1f9cd404bc37b970e2ae78c057983bad Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Sat, 11 Jan 2025 18:23:41 -0800 Subject: [PATCH] fix(log): support nginx built without variadic macros Fixed the error below, caused by a different signature of `ngx_log_error_core` when neither `NGX_HAVE_C99_VARIADIC_MACROS` nor nor `NGX_HAVE_GCC_VARIADIC_MACROS` is detected. An example of affected configuration is MinGW with clang or gcc, where we skip most of the compiler feature checks. ``` error[E0061]: this function takes 5 arguments but 6 arguments were supplied --> examples\awssig.rs:312:9 | 312 | ngx_log_debug_http!(request, "headers {:?}", headers); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unexpected argument #6 of type `*const u8` | note: expected `*mut i8`, found `usize` ``` --- nginx-sys/build/main.rs | 3 ++- src/log.rs | 46 ++++++++++++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/nginx-sys/build/main.rs b/nginx-sys/build/main.rs index 0e63870..d7661f5 100644 --- a/nginx-sys/build/main.rs +++ b/nginx-sys/build/main.rs @@ -25,6 +25,7 @@ const NGX_CONF_FEATURES: &[&str] = &[ "have_epollrdhup", "have_file_aio", "have_kqueue", + "have_variadic_macros", "http_cache", "http_dav", "http_gzip", @@ -254,7 +255,7 @@ fn expand_definitions>(includes: &[T]) -> Result, Box -#include +#include RUST_CONF_NGINX_BUILD=NGINX_VER_BUILD RUST_CONF_NGINX_VERSION=NGINX_VER diff --git a/src/log.rs b/src/log.rs index 3eb18e4..9572437 100644 --- a/src/log.rs +++ b/src/log.rs @@ -2,7 +2,7 @@ use core::cmp; use core::fmt::{self, Write}; use core::mem::MaybeUninit; -use crate::ffi::NGX_MAX_ERROR_STR; +use crate::ffi::{self, ngx_err_t, ngx_log_t, ngx_uint_t, NGX_MAX_ERROR_STR}; /// Size of the static buffer used to format log messages. /// @@ -35,6 +35,41 @@ pub fn write_fmt<'a>(buf: &'a mut [MaybeUninit], args: fmt::Arguments<'_>) - } } +/// Writes the provided buffer to the nginx logger at a specified level. +/// +/// # Safety +/// Requires a valid log pointer. +#[inline] +pub unsafe fn log_error(level: ngx_uint_t, log: *mut ngx_log_t, err: ngx_err_t, buf: &[u8]) { + unsafe { + #[cfg(ngx_feature = "have_variadic_macros")] + ffi::ngx_log_error_core(level, log, err, c"%*s".as_ptr(), buf.len(), buf.as_ptr()); + #[cfg(not(ngx_feature = "have_variadic_macros"))] + ffi::ngx_log_error(level, log, err, c"%*s".as_ptr(), buf.len(), buf.as_ptr()); + } +} + +/// Writes the provided buffer to the nginx logger at the debug level. +/// +/// # Safety +/// Requires a valid log pointer. +#[inline] +pub unsafe fn log_debug(log: *mut ngx_log_t, err: ngx_err_t, buf: &[u8]) { + unsafe { + #[cfg(ngx_feature = "have_variadic_macros")] + ffi::ngx_log_error_core( + ffi::NGX_LOG_DEBUG as _, + log, + err, + c"%*s".as_ptr(), + buf.len(), + buf.as_ptr(), + ); + #[cfg(not(ngx_feature = "have_variadic_macros"))] + ffi::ngx_log_debug_core(log, err, c"%*s".as_ptr(), buf.len(), buf.as_ptr()); + } +} + /// Write to logger at a specified level. /// /// See [Logging](https://nginx.org/en/docs/dev/development_guide.html#logging) @@ -47,9 +82,7 @@ macro_rules! ngx_log_error { if level < unsafe { (*log).log_level } { let mut buf = [const { ::core::mem::MaybeUninit::::uninit() }; $crate::log::LOG_BUFFER_SIZE]; let message = $crate::log::write_fmt(&mut buf, format_args!($($arg)+)); - unsafe { - $crate::ffi::ngx_log_error_core(level, log, 0, c"%*s".as_ptr(), message.len(), message.as_ptr()); - } + unsafe { $crate::log::log_error(level, log, 0, message) }; } } } @@ -76,12 +109,9 @@ macro_rules! ngx_log_debug { ( mask: $mask:expr, $log:expr, $($arg:tt)+ ) => { let log = $log; if $crate::log::check_mask($mask, unsafe { (*log).log_level }) { - let level = $crate::ffi::NGX_LOG_DEBUG as $crate::ffi::ngx_uint_t; let mut buf = [const { ::core::mem::MaybeUninit::::uninit() }; $crate::log::LOG_BUFFER_SIZE]; let message = $crate::log::write_fmt(&mut buf, format_args!($($arg)+)); - unsafe { - $crate::ffi::ngx_log_error_core(level, log, 0, c"%*s".as_ptr(), message.len(), message.as_ptr()); - } + unsafe { $crate::log::log_debug(log, 0, message) }; } }; ( $log:expr, $($arg:tt)+ ) => {