Skip to content

Commit 6750c80

Browse files
committed
refactor: deduplicate log macro bodies
1 parent c3c31ec commit 6750c80

File tree

2 files changed

+35
-85
lines changed

2 files changed

+35
-85
lines changed

examples/upstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ 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::log::DebugMask;
2827
use ngx::{http_upstream_init_peer_pt, ngx_log_debug_http, ngx_log_debug_mask, ngx_null_command, ngx_string};
2928

3029
#[derive(Clone, Copy, Debug)]

src/log.rs

Lines changed: 35 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ pub fn check_mask(mask: DebugMask, log_level: usize) -> bool {
1414
/// for available log levels.
1515
#[macro_export]
1616
macro_rules! ngx_log_debug {
17-
( $log:expr, $($arg:tt)* ) => {
18-
let log_level = unsafe { (*$log).log_level };
19-
if log_level != 0 {
17+
( mask: $mask:expr, $log:expr, $($arg:tt)+ ) => {
18+
let log = $log;
19+
if $crate::log::check_mask($mask, unsafe { (*log).log_level }) {
2020
let level = $crate::ffi::NGX_LOG_DEBUG as $crate::ffi::ngx_uint_t;
2121
let fmt = ::std::ffi::CString::new("%s").unwrap();
22-
let c_message = ::std::ffi::CString::new(format!($($arg)*)).unwrap();
22+
let c_message = ::std::ffi::CString::new(format!($($arg)+)).unwrap();
2323
unsafe {
24-
$crate::ffi::ngx_log_error_core(level, $log, 0, fmt.as_ptr(), c_message.as_ptr());
24+
$crate::ffi::ngx_log_error_core(level, log, 0, fmt.as_ptr(), c_message.as_ptr());
2525
}
2626
}
27+
};
28+
( $log:expr, $($arg:tt)+ ) => {
29+
$crate::ngx_log_debug!(mask: $crate::log::DebugMask::All, $log, $($arg)+);
2730
}
2831
}
2932

@@ -32,9 +35,9 @@ macro_rules! ngx_log_debug {
3235
/// [`NGX_LOG_DEBUG_HTTP`]: https://nginx.org/en/docs/dev/development_guide.html#logging
3336
#[macro_export]
3437
macro_rules! ngx_log_debug_http {
35-
( $request:expr, $($arg:tt)* ) => {
38+
( $request:expr, $($arg:tt)+ ) => {
3639
let log = unsafe { (*$request.connection()).log };
37-
$crate::ngx_log_debug!(log, $($arg)*);
40+
$crate::ngx_log_debug!(mask: $crate::log::DebugMask::Http, log, $($arg)+);
3841
}
3942
}
4043

@@ -48,83 +51,27 @@ macro_rules! ngx_log_debug_http {
4851
/// masks.
4952
#[macro_export]
5053
macro_rules! ngx_log_debug_mask {
51-
( DebugMask::Core, $log:expr, $($arg:tt)* ) => ({
52-
let log_level = unsafe { (*$log).log_level };
53-
if $crate::log::check_mask(DebugMask::Core, log_level) {
54-
let level = $crate::ffi::NGX_LOG_DEBUG as $crate::ffi::ngx_uint_t;
55-
let fmt = ::std::ffi::CString::new("%s").unwrap();
56-
let c_message = ::std::ffi::CString::new(format!($($arg)*)).unwrap();
57-
unsafe {
58-
$crate::ffi::ngx_log_error_core(level, $log, 0, fmt.as_ptr(), c_message.as_ptr());
59-
}
60-
}
61-
});
62-
( DebugMask::Alloc, $log:expr, $($arg:tt)* ) => ({
63-
let log_level = unsafe { (*$log).log_level };
64-
if $crate::log::check_mask(DebugMask::Alloc, log_level) {
65-
let level = $crate::ffi::NGX_LOG_DEBUG as $crate::ffi::ngx_uint_t;
66-
let fmt = ::std::ffi::CString::new("%s").unwrap();
67-
let c_message = ::std::ffi::CString::new(format!($($arg)*)).unwrap();
68-
unsafe {
69-
$crate::ffi::ngx_log_error_core(level, $log, 0, fmt.as_ptr(), c_message.as_ptr());
70-
}
71-
}
72-
});
73-
( DebugMask::Mutex, $log:expr, $($arg:tt)* ) => ({
74-
let log_level = unsafe { (*$log).log_level };
75-
if $crate::log::check_mask(DebugMask::Mutex, log_level) {
76-
let level = $crate::ffi::NGX_LOG_DEBUG as $crate::ffi::ngx_uint_t;
77-
let fmt = ::std::ffi::CString::new("%s").unwrap();
78-
let c_message = ::std::ffi::CString::new(format!($($arg)*)).unwrap();
79-
unsafe {
80-
$crate::ffi::ngx_log_error_core(level, $log, 0, fmt.as_ptr(), c_message.as_ptr());
81-
}
82-
}
83-
});
84-
( DebugMask::Event, $log:expr, $($arg:tt)* ) => ({
85-
let log_level = unsafe { (*$log).log_level };
86-
if $crate::log::check_mask(DebugMask::Event, log_level) {
87-
let level = $crate::ffi::NGX_LOG_DEBUG as $crate::ffi::ngx_uint_t;
88-
let fmt = ::std::ffi::CString::new("%s").unwrap();
89-
let c_message = ::std::ffi::CString::new(format!($($arg)*)).unwrap();
90-
unsafe {
91-
$crate::ffi::ngx_log_error_core(level, $log, 0, fmt.as_ptr(), c_message.as_ptr());
92-
}
93-
}
94-
});
95-
( DebugMask::Http, $log:expr, $($arg:tt)* ) => ({
96-
let log_level = unsafe { (*$log).log_level };
97-
if $crate::log::check_mask(DebugMask::Http, log_level) {
98-
let level = $crate::ffi::NGX_LOG_DEBUG as $crate::ffi::ngx_uint_t;
99-
let fmt = ::std::ffi::CString::new("%s").unwrap();
100-
let c_message = ::std::ffi::CString::new(format!($($arg)*)).unwrap();
101-
unsafe {
102-
$crate::ffi::ngx_log_error_core(level, $log, 0, fmt.as_ptr(), c_message.as_ptr());
103-
}
104-
}
105-
});
106-
( DebugMask::Mail, $log:expr, $($arg:tt)* ) => ({
107-
let log_level = unsafe { (*$log).log_level };
108-
if $crate::log::check_mask(DebugMask::Mail, log_level) {
109-
let level = $crate::ffi::NGX_LOG_DEBUG as $crate::ffi::ngx_uint_t;
110-
let fmt = ::std::ffi::CString::new("%s").unwrap();
111-
let c_message = ::std::ffi::CString::new(format!($($arg)*)).unwrap();
112-
unsafe {
113-
$crate::ffi::ngx_log_error_core(level, $log, 0, fmt.as_ptr(), c_message.as_ptr());
114-
}
115-
}
116-
});
117-
( DebugMask::Stream, $log:expr, $($arg:tt)* ) => ({
118-
let log_level = unsafe { (*$log).log_level };
119-
if $crate::log::check_mask(DebugMask::Stream, log_level) {
120-
let level = $crate::ffi::NGX_LOG_DEBUG as $crate::ffi::ngx_uint_t;
121-
let fmt = ::std::ffi::CString::new("%s").unwrap();
122-
let c_message = ::std::ffi::CString::new(format!($($arg)*)).unwrap();
123-
unsafe {
124-
$crate::ffi::ngx_log_error_core(level, $log, 0, fmt.as_ptr(), c_message.as_ptr());
125-
}
126-
}
127-
});
54+
( DebugMask::Core, $log:expr, $($arg:tt)+ ) => {
55+
$crate::ngx_log_debug!(mask: $crate::log::DebugMask::Core, $log, $($arg)+);
56+
};
57+
( DebugMask::Alloc, $log:expr, $($arg:tt)+ ) => {
58+
$crate::ngx_log_debug!(mask: $crate::log::DebugMask::Alloc, $log, $($arg)+);
59+
};
60+
( DebugMask::Mutex, $log:expr, $($arg:tt)+ ) => {
61+
$crate::ngx_log_debug!(mask: $crate::log::DebugMask::Mutex, $log, $($arg)+);
62+
};
63+
( DebugMask::Event, $log:expr, $($arg:tt)+ ) => {
64+
$crate::ngx_log_debug!(mask: $crate::log::DebugMask::Event, $log, $($arg)+);
65+
};
66+
( DebugMask::Http, $log:expr, $($arg:tt)+ ) => {
67+
$crate::ngx_log_debug!(mask: $crate::log::DebugMask::Http, $log, $($arg)+);
68+
};
69+
( DebugMask::Mail, $log:expr, $($arg:tt)+ ) => {
70+
$crate::ngx_log_debug!(mask: $crate::log::DebugMask::Mail, $log, $($arg)+);
71+
};
72+
( DebugMask::Stream, $log:expr, $($arg:tt)+ ) => {
73+
$crate::ngx_log_debug!(mask: $crate::log::DebugMask::Stream, $log, $($arg)+);
74+
};
12875
}
12976

13077
/// Debug masks for use with [`ngx_log_debug_mask`], these represent the only accepted values for
@@ -145,6 +92,8 @@ pub enum DebugMask {
14592
Mail,
14693
/// Aligns to the NGX_LOG_DEBUG_STREAM mask.
14794
Stream,
95+
/// Aligns to the NGX_LOG_DEBUG_ALL mask.
96+
All,
14897
}
14998

15099
impl TryFrom<u32> for DebugMask {
@@ -159,6 +108,7 @@ impl TryFrom<u32> for DebugMask {
159108
crate::ffi::NGX_LOG_DEBUG_HTTP => Ok(DebugMask::Http),
160109
crate::ffi::NGX_LOG_DEBUG_MAIL => Ok(DebugMask::Mail),
161110
crate::ffi::NGX_LOG_DEBUG_STREAM => Ok(DebugMask::Stream),
111+
crate::ffi::NGX_LOG_DEBUG_ALL => Ok(DebugMask::All),
162112
_ => Err(0),
163113
}
164114
}
@@ -174,6 +124,7 @@ impl From<DebugMask> for u32 {
174124
DebugMask::Http => crate::ffi::NGX_LOG_DEBUG_HTTP,
175125
DebugMask::Mail => crate::ffi::NGX_LOG_DEBUG_MAIL,
176126
DebugMask::Stream => crate::ffi::NGX_LOG_DEBUG_STREAM,
127+
DebugMask::All => crate::ffi::NGX_LOG_DEBUG_ALL,
177128
}
178129
}
179130
}

0 commit comments

Comments
 (0)