Skip to content

Commit 06ec0e8

Browse files
committed
feat: add a method to obtain global log pointer
This is a slightly more ergonomic variant of (*nginx_sys::ngx_cycle).log with safety invariants being documented.
1 parent e017d3d commit 06ec0e8

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/log.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use core::cmp;
22
use core::fmt::{self, Write};
33
use core::mem::MaybeUninit;
4+
use core::ptr::NonNull;
45

56
use crate::ffi::{self, ngx_err_t, ngx_log_t, ngx_uint_t, NGX_MAX_ERROR_STR};
67

@@ -11,6 +12,19 @@ use crate::ffi::{self, ngx_err_t, ngx_log_t, ngx_uint_t, NGX_MAX_ERROR_STR};
1112
pub const LOG_BUFFER_SIZE: usize =
1213
NGX_MAX_ERROR_STR as usize - b"1970/01/01 00:00:00 [info] 1#1: ".len();
1314

15+
/// Obtains a pointer to the global (cycle) log object.
16+
///
17+
/// The returned pointer is tied to the current cycle lifetime, and will be invalidated by a
18+
/// configuration reload in the master process or in a single-process mode. If you plan to store it,
19+
/// make sure that your storage is also tied to the cycle lifetime (e.g. module configuration or
20+
/// connection/request data).
21+
///
22+
/// The function may panic if you call it before the main() in nginx creates an initial cycle.
23+
#[inline(always)]
24+
pub fn ngx_cycle_log() -> NonNull<ngx_log_t> {
25+
NonNull::new(unsafe { (*nginx_sys::ngx_cycle).log }).expect("global logger")
26+
}
27+
1428
/// Utility function to provide typed checking of the mask's field state.
1529
#[inline(always)]
1630
pub fn check_mask(mask: DebugMask, log_level: usize) -> bool {

0 commit comments

Comments
 (0)