Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(log): support nginx built without variadic macros #122

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading