Skip to content

Commit

Permalink
Simplify ffi quic_set_logger (Tencent#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaofei0800 authored Dec 21, 2023
1 parent a0448ea commit fd338f6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
1 change: 1 addition & 0 deletions cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ exclude = ["MAX_CID_LEN", "MIN_CLIENT_INITIAL_LEN"]
"TlsConfigSelectMethods" = "quic_tls_config_select_methods_t"
"TlsConfigSelectorContext" = "quic_tls_config_select_context_t"
"CongestionControlAlgorithm" = "quic_congestion_control_algorithm"
"LevelFilter" = "quic_log_level"
"Http3Connection" = "http3_conn_t"
"Http3Config" = "http3_config_t"
"Http3Context" = "http3_context_t"
Expand Down
36 changes: 32 additions & 4 deletions include/tquic.h
Original file line number Diff line number Diff line change
Expand Up @@ -911,16 +911,44 @@ int http3_take_priority_update(struct http3_conn_t *conn,
void *argp),
void *argp);

/**
* An enum representing the available verbosity level filters of the logger.
*/
typedef enum quic_log_level {
/**
* A level lower than all log levels.
*/
QUIC_LOG_LEVEL_OFF,
/**
* Corresponds to the `Error` log level.
*/
QUIC_LOG_LEVEL_ERROR,
/**
* Corresponds to the `Warn` log level.
*/
QUIC_LOG_LEVEL_WARN,
/**
* Corresponds to the `Info` log level.
*/
QUIC_LOG_LEVEL_INFO,
/**
* Corresponds to the `Debug` log level.
*/
QUIC_LOG_LEVEL_DEBUG,
/**
* Corresponds to the `Trace` log level.
*/
QUIC_LOG_LEVEL_TRACE,
} quic_log_level;

/**
* Set logger.
* `cb` is a callback function that will be called for each log message.
* `line` is a null-terminated log message and `argp` is user-defined data that will be passed to
* the callback.
* `level` is the log level filter, valid values are "OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE".
* `level` represents the log level.
*/
int quic_set_logger(void (*cb)(const uint8_t *line, void *argp),
void *argp,
const char *level);
void quic_set_logger(void (*cb)(const uint8_t *line, void *argp), void *argp, quic_log_level level);

typedef enum http3_error {
HTTP3_NO_ERROR = 0,
Expand Down
23 changes: 5 additions & 18 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1651,30 +1651,17 @@ fn headers_from_ptr<'a>(ptr: *const Header, len: size_t) -> Vec<h3::HeaderRef<'a
/// `cb` is a callback function that will be called for each log message.
/// `line` is a null-terminated log message and `argp` is user-defined data that will be passed to
/// the callback.
/// `level` is the log level filter, valid values are "OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE".
/// `level` represents the log level.
#[no_mangle]
pub extern "C" fn quic_set_logger(
cb: extern "C" fn(line: *const u8, argp: *mut c_void),
argp: *mut c_void,
level: *const c_char,
) -> c_int {
level: log::LevelFilter,
) {
let argp = atomic::AtomicPtr::new(argp);
let logger = Box::new(Logger { cb, argp });

if log::set_boxed_logger(logger).is_err() {
return -1;
}

if level.is_null() {
return -1;
}

let level = unsafe { ffi::CStr::from_ptr(level).to_str().unwrap_or_default() };
if let Ok(level_filter) = log::LevelFilter::from_str(level) {
log::set_max_level(level_filter);
}

0
let _ = log::set_boxed_logger(logger);
log::set_max_level(level);
}

#[repr(C)]
Expand Down

0 comments on commit fd338f6

Please sign in to comment.