Skip to content

Commit aefeb69

Browse files
committed
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` ```
1 parent 7266aa8 commit aefeb69

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

nginx-sys/build/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const NGX_CONF_FEATURES: &[&str] = &[
2525
"have_epollrdhup",
2626
"have_file_aio",
2727
"have_kqueue",
28+
"have_variadic_macros",
2829
"http_cache",
2930
"http_dav",
3031
"http_gzip",
@@ -254,7 +255,7 @@ fn expand_definitions<T: AsRef<Path>>(includes: &[T]) -> Result<Vec<u8>, Box<dyn
254255
writer,
255256
"
256257
#include <ngx_config.h>
257-
#include <nginx.h>
258+
#include <ngx_core.h>
258259
259260
RUST_CONF_NGINX_BUILD=NGINX_VER_BUILD
260261
RUST_CONF_NGINX_VERSION=NGINX_VER

src/log.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::cmp;
22
use core::fmt::{self, Write};
33
use core::mem::MaybeUninit;
44

5-
use crate::ffi::NGX_MAX_ERROR_STR;
5+
use crate::ffi::{self, ngx_err_t, ngx_log_t, ngx_uint_t, NGX_MAX_ERROR_STR};
66

77
/// Size of the static buffer used to format log messages.
88
///
@@ -35,6 +35,41 @@ pub fn write_fmt<'a>(buf: &'a mut [MaybeUninit<u8>], args: fmt::Arguments<'_>) -
3535
}
3636
}
3737

38+
/// Writes the provided buffer to the nginx logger at a specified level.
39+
///
40+
/// # Safety
41+
/// Requires a valid log pointer.
42+
#[inline]
43+
pub unsafe fn log_error(level: ngx_uint_t, log: *mut ngx_log_t, err: ngx_err_t, buf: &[u8]) {
44+
unsafe {
45+
#[cfg(ngx_feature = "have_variadic_macros")]
46+
ffi::ngx_log_error_core(level, log, err, c"%*s".as_ptr(), buf.len(), buf.as_ptr());
47+
#[cfg(not(ngx_feature = "have_variadic_macros"))]
48+
ffi::ngx_log_error(level, log, err, c"%*s".as_ptr(), buf.len(), buf.as_ptr());
49+
}
50+
}
51+
52+
/// Writes the provided buffer to the nginx logger at the debug level.
53+
///
54+
/// # Safety
55+
/// Requires a valid log pointer.
56+
#[inline]
57+
pub unsafe fn log_debug(log: *mut ngx_log_t, err: ngx_err_t, buf: &[u8]) {
58+
unsafe {
59+
#[cfg(ngx_feature = "have_variadic_macros")]
60+
ffi::ngx_log_error_core(
61+
ffi::NGX_LOG_DEBUG as _,
62+
log,
63+
err,
64+
c"%*s".as_ptr(),
65+
buf.len(),
66+
buf.as_ptr(),
67+
);
68+
#[cfg(not(ngx_feature = "have_variadic_macros"))]
69+
ffi::ngx_log_debug_core(log, err, c"%*s".as_ptr(), buf.len(), buf.as_ptr());
70+
}
71+
}
72+
3873
/// Write to logger at a specified level.
3974
///
4075
/// See [Logging](https://nginx.org/en/docs/dev/development_guide.html#logging)
@@ -47,9 +82,7 @@ macro_rules! ngx_log_error {
4782
if level < unsafe { (*log).log_level } {
4883
let mut buf = [const { ::core::mem::MaybeUninit::<u8>::uninit() }; $crate::log::LOG_BUFFER_SIZE];
4984
let message = $crate::log::write_fmt(&mut buf, format_args!($($arg)+));
50-
unsafe {
51-
$crate::ffi::ngx_log_error_core(level, log, 0, c"%*s".as_ptr(), message.len(), message.as_ptr());
52-
}
85+
unsafe { $crate::log::log_error(level, log, 0, message) };
5386
}
5487
}
5588
}
@@ -76,12 +109,9 @@ macro_rules! ngx_log_debug {
76109
( mask: $mask:expr, $log:expr, $($arg:tt)+ ) => {
77110
let log = $log;
78111
if $crate::log::check_mask($mask, unsafe { (*log).log_level }) {
79-
let level = $crate::ffi::NGX_LOG_DEBUG as $crate::ffi::ngx_uint_t;
80112
let mut buf = [const { ::core::mem::MaybeUninit::<u8>::uninit() }; $crate::log::LOG_BUFFER_SIZE];
81113
let message = $crate::log::write_fmt(&mut buf, format_args!($($arg)+));
82-
unsafe {
83-
$crate::ffi::ngx_log_error_core(level, log, 0, c"%*s".as_ptr(), message.len(), message.as_ptr());
84-
}
114+
unsafe { $crate::log::log_debug(log, 0, message) };
85115
}
86116
};
87117
( $log:expr, $($arg:tt)+ ) => {

0 commit comments

Comments
 (0)