Skip to content

Commit 6b3c6e9

Browse files
committed
feat: implement ngx_log_error! macro
1 parent 7ee10a0 commit 6b3c6e9

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

examples/upstream.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::slice;
1313

1414
use ngx::core::{Pool, Status};
1515
use ngx::ffi::{
16-
nginx_version, ngx_atoi, ngx_command_t, ngx_conf_log_error, ngx_conf_t, ngx_connection_t, ngx_event_free_peer_pt,
16+
nginx_version, ngx_atoi, ngx_command_t, ngx_conf_t, ngx_connection_t, ngx_event_free_peer_pt,
1717
ngx_event_get_peer_pt, ngx_http_module_t, ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt,
1818
ngx_http_upstream_init_round_robin, ngx_http_upstream_module, ngx_http_upstream_srv_conf_t, ngx_http_upstream_t,
1919
ngx_int_t, ngx_module_t, ngx_peer_connection_t, ngx_str_t, ngx_uint_t, NGX_CONF_NOARGS, NGX_CONF_TAKE1,
@@ -24,7 +24,10 @@ use ngx::http::{
2424
ngx_http_conf_get_module_srv_conf, ngx_http_conf_upstream_srv_conf_immutable,
2525
ngx_http_conf_upstream_srv_conf_mutable, HTTPModule, Merge, MergeConfigError, Request,
2626
};
27-
use ngx::{http_upstream_init_peer_pt, ngx_log_debug_http, ngx_log_debug_mask, ngx_null_command, ngx_string};
27+
use ngx::{
28+
http_upstream_init_peer_pt, ngx_conf_log_error, ngx_log_debug_http, ngx_log_debug_mask, ngx_null_command,
29+
ngx_string,
30+
};
2831

2932
#[derive(Clone, Copy, Debug)]
3033
#[repr(C)]
@@ -242,12 +245,7 @@ unsafe extern "C" fn ngx_http_upstream_init_custom(
242245
let maybe_conf: Option<*mut SrvConfig> =
243246
ngx_http_conf_upstream_srv_conf_mutable(us, &*addr_of!(ngx_http_upstream_custom_module));
244247
if maybe_conf.is_none() {
245-
ngx_conf_log_error(
246-
NGX_LOG_EMERG as usize,
247-
cf,
248-
0,
249-
"CUSTOM UPSTREAM no upstream srv_conf".as_bytes().as_ptr() as *const c_char,
250-
);
248+
ngx_conf_log_error!(NGX_LOG_EMERG, cf, "CUSTOM UPSTREAM no upstream srv_conf");
251249
return isize::from(Status::NGX_ERROR);
252250
}
253251
let hccf = maybe_conf.unwrap();
@@ -258,12 +256,7 @@ unsafe extern "C" fn ngx_http_upstream_init_custom(
258256

259257
let init_upstream_ptr = (*hccf).original_init_upstream.unwrap();
260258
if init_upstream_ptr(cf, us) != Status::NGX_OK.into() {
261-
ngx_conf_log_error(
262-
NGX_LOG_EMERG as usize,
263-
cf,
264-
0,
265-
"CUSTOM UPSTREAM failed calling init_upstream".as_bytes().as_ptr() as *const c_char,
266-
);
259+
ngx_conf_log_error!(NGX_LOG_EMERG, cf, "CUSTOM UPSTREAM failed calling init_upstream");
267260
return isize::from(Status::NGX_ERROR);
268261
}
269262

@@ -290,13 +283,12 @@ unsafe extern "C" fn ngx_http_upstream_commands_set_custom(
290283
let value: &[ngx_str_t] = slice::from_raw_parts((*(*cf).args).elts as *const ngx_str_t, (*(*cf).args).nelts);
291284
let n = ngx_atoi(value[1].data, value[1].len);
292285
if n == (NGX_ERROR as isize) || n == 0 {
293-
ngx_conf_log_error(
294-
NGX_LOG_EMERG as usize,
286+
ngx_conf_log_error!(
287+
NGX_LOG_EMERG,
295288
cf,
296-
0,
297-
"invalid value \"%V\" in \"%V\" directive".as_bytes().as_ptr() as *const c_char,
289+
"invalid value \"{}\" in \"{}\" directive",
298290
value[1],
299-
&(*cmd).name,
291+
&(*cmd).name
300292
);
301293
return usize::MAX as *mut c_char;
302294
}
@@ -334,13 +326,10 @@ impl HTTPModule for Module {
334326
let mut pool = Pool::from_ngx_pool((*cf).pool);
335327
let conf = pool.alloc_type::<SrvConfig>();
336328
if conf.is_null() {
337-
ngx_conf_log_error(
338-
NGX_LOG_EMERG as usize,
329+
ngx_conf_log_error!(
330+
NGX_LOG_EMERG,
339331
cf,
340-
0,
341332
"CUSTOM UPSTREAM could not allocate memory for config"
342-
.as_bytes()
343-
.as_ptr() as *const c_char,
344333
);
345334
return std::ptr::null_mut();
346335
}

src/log.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,38 @@ pub fn check_mask(mask: DebugMask, log_level: usize) -> bool {
1313
/// See [Logging](https://nginx.org/en/docs/dev/development_guide.html#logging)
1414
/// for available log levels.
1515
#[macro_export]
16+
macro_rules! ngx_log_error {
17+
( $level:expr, $log:expr, $($arg:tt)+ ) => {
18+
let log = $log;
19+
let level = $level as $crate::ffi::ngx_uint_t;
20+
if level < unsafe { (*log).log_level } {
21+
let message = ::std::format!($($arg)+);
22+
let message = message.as_bytes();
23+
unsafe {
24+
$crate::ffi::ngx_log_error_core(level, log, 0, c"%*s".as_ptr(), message.len(), message.as_ptr());
25+
}
26+
}
27+
}
28+
}
29+
30+
/// Write to logger with the context of currently processed configuration file.
31+
#[macro_export]
32+
macro_rules! ngx_conf_log_error {
33+
( $level:expr, $cf:expr, $($arg:tt)+ ) => {
34+
let cf: *mut $crate::ffi::ngx_conf_t = $cf;
35+
let level = $level as $crate::ffi::ngx_uint_t;
36+
if level < unsafe { (*(*cf).log).log_level } {
37+
let message = ::std::format!($($arg)+);
38+
let message = message.as_bytes();
39+
unsafe {
40+
$crate::ffi::ngx_conf_log_error(level, cf, 0, c"%*s".as_ptr(), message.len(), message.as_ptr());
41+
}
42+
}
43+
}
44+
}
45+
46+
/// Write to logger at debug level.
47+
#[macro_export]
1648
macro_rules! ngx_log_debug {
1749
( mask: $mask:expr, $log:expr, $($arg:tt)+ ) => {
1850
let log = $log;

0 commit comments

Comments
 (0)