Skip to content

Commit

Permalink
feat: implement ngx_log_error! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
bavshin-f5 committed Jan 4, 2025
1 parent 979736c commit 908f681
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
37 changes: 13 additions & 24 deletions examples/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::slice;

use ngx::core::{Pool, Status};
use ngx::ffi::{
nginx_version, ngx_atoi, ngx_command_t, ngx_conf_log_error, ngx_conf_t, ngx_connection_t, ngx_event_free_peer_pt,
nginx_version, ngx_atoi, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_free_peer_pt,
ngx_event_get_peer_pt, ngx_http_module_t, ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt,
ngx_http_upstream_init_round_robin, ngx_http_upstream_module, ngx_http_upstream_srv_conf_t, ngx_http_upstream_t,
ngx_int_t, ngx_module_t, ngx_peer_connection_t, ngx_str_t, ngx_uint_t, NGX_CONF_NOARGS, NGX_CONF_TAKE1,
Expand All @@ -24,7 +24,10 @@ use ngx::http::{
ngx_http_conf_get_module_srv_conf, ngx_http_conf_upstream_srv_conf_immutable,
ngx_http_conf_upstream_srv_conf_mutable, HTTPModule, Merge, MergeConfigError, Request,
};
use ngx::{http_upstream_init_peer_pt, ngx_log_debug_http, ngx_log_debug_mask, ngx_null_command, ngx_string};
use ngx::{
http_upstream_init_peer_pt, ngx_conf_log_error, ngx_log_debug_http, ngx_log_debug_mask, ngx_null_command,
ngx_string,
};

#[derive(Clone, Copy, Debug)]
#[repr(C)]
Expand Down Expand Up @@ -242,12 +245,7 @@ unsafe extern "C" fn ngx_http_upstream_init_custom(
let maybe_conf: Option<*mut SrvConfig> =
ngx_http_conf_upstream_srv_conf_mutable(us, &*addr_of!(ngx_http_upstream_custom_module));
if maybe_conf.is_none() {
ngx_conf_log_error(
NGX_LOG_EMERG as usize,
cf,
0,
"CUSTOM UPSTREAM no upstream srv_conf".as_bytes().as_ptr() as *const c_char,
);
ngx_conf_log_error!(NGX_LOG_EMERG, cf, "CUSTOM UPSTREAM no upstream srv_conf");
return isize::from(Status::NGX_ERROR);
}
let hccf = maybe_conf.unwrap();
Expand All @@ -258,12 +256,7 @@ unsafe extern "C" fn ngx_http_upstream_init_custom(

let init_upstream_ptr = (*hccf).original_init_upstream.unwrap();
if init_upstream_ptr(cf, us) != Status::NGX_OK.into() {
ngx_conf_log_error(
NGX_LOG_EMERG as usize,
cf,
0,
"CUSTOM UPSTREAM failed calling init_upstream".as_bytes().as_ptr() as *const c_char,
);
ngx_conf_log_error!(NGX_LOG_EMERG, cf, "CUSTOM UPSTREAM failed calling init_upstream");
return isize::from(Status::NGX_ERROR);
}

Expand All @@ -290,13 +283,12 @@ unsafe extern "C" fn ngx_http_upstream_commands_set_custom(
let value: &[ngx_str_t] = slice::from_raw_parts((*(*cf).args).elts as *const ngx_str_t, (*(*cf).args).nelts);
let n = ngx_atoi(value[1].data, value[1].len);
if n == (NGX_ERROR as isize) || n == 0 {
ngx_conf_log_error(
NGX_LOG_EMERG as usize,
ngx_conf_log_error!(
NGX_LOG_EMERG,
cf,
0,
"invalid value \"%V\" in \"%V\" directive".as_bytes().as_ptr() as *const c_char,
"invalid value \"{}\" in \"{}\" directive",
value[1],
&(*cmd).name,
&(*cmd).name
);
return usize::MAX as *mut c_char;
}
Expand Down Expand Up @@ -334,13 +326,10 @@ impl HTTPModule for Module {
let mut pool = Pool::from_ngx_pool((*cf).pool);
let conf = pool.alloc_type::<SrvConfig>();
if conf.is_null() {
ngx_conf_log_error(
NGX_LOG_EMERG as usize,
ngx_conf_log_error!(
NGX_LOG_EMERG,
cf,
0,
"CUSTOM UPSTREAM could not allocate memory for config"
.as_bytes()
.as_ptr() as *const c_char,
);
return std::ptr::null_mut();
}
Expand Down
32 changes: 32 additions & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,38 @@ pub fn check_mask(mask: DebugMask, log_level: usize) -> bool {
/// See [Logging](https://nginx.org/en/docs/dev/development_guide.html#logging)
/// for available log levels.
#[macro_export]
macro_rules! ngx_log_error {
( $level:expr, $log:expr, $($arg:tt)+ ) => {
let log = $log;
let level = $level as $crate::ffi::ngx_uint_t;
if level < unsafe { (*log).log_level } {
let message = ::std::format!($($arg)+);
let message = message.as_bytes();
unsafe {
$crate::ffi::ngx_log_error_core(level, log, 0, c"%*s".as_ptr(), message.len(), message.as_ptr());
}
}
}
}

/// Write to logger with the context of currently processed configuration file.
#[macro_export]
macro_rules! ngx_conf_log_error {
( $level:expr, $cf:expr, $($arg:tt)+ ) => {
let cf: *mut $crate::ffi::ngx_conf_t = $cf;
let level = $level as $crate::ffi::ngx_uint_t;
if level < unsafe { (*(*cf).log).log_level } {
let message = ::std::format!($($arg)+);
let message = message.as_bytes();
unsafe {
$crate::ffi::ngx_conf_log_error(level, cf, 0, c"%*s".as_ptr(), message.len(), message.as_ptr());
}
}
}
}

/// Write to logger at debug level.
#[macro_export]
macro_rules! ngx_log_debug {
( mask: $mask:expr, $log:expr, $($arg:tt)+ ) => {
let log = $log;
Expand Down

0 comments on commit 908f681

Please sign in to comment.