Skip to content

Commit

Permalink
fix(log): support nginx built without variadic macros
Browse files Browse the repository at this point in the history
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`
```
  • Loading branch information
bavshin-f5 committed Jan 16, 2025
1 parent 7266aa8 commit 7635440
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
3 changes: 2 additions & 1 deletion nginx-sys/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -254,7 +255,7 @@ fn expand_definitions<T: AsRef<Path>>(includes: &[T]) -> Result<Vec<u8>, Box<dyn
writer,
"
#include <ngx_config.h>
#include <nginx.h>
#include <ngx_core.h>
RUST_CONF_NGINX_BUILD=NGINX_VER_BUILD
RUST_CONF_NGINX_VERSION=NGINX_VER
Expand Down
46 changes: 38 additions & 8 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -35,6 +35,41 @@ pub fn write_fmt<'a>(buf: &'a mut [MaybeUninit<u8>], 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)
Expand All @@ -47,9 +82,7 @@ macro_rules! ngx_log_error {
if level < unsafe { (*log).log_level } {
let mut buf = [const { ::core::mem::MaybeUninit::<u8>::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) };
}
}
}
Expand All @@ -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::<u8>::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)+ ) => {
Expand Down

0 comments on commit 7635440

Please sign in to comment.