Skip to content

Commit

Permalink
update: more rust log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
dnomd343 committed Mar 29, 2023
1 parent a3ca39a commit 75af2d2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
4 changes: 4 additions & 0 deletions include/constant.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#define ASSET_CHINA_IP "china-ip.txt"
#define ASSET_CHINA_LIST "chinalist.txt"

#define RUST_LOG_INFO 0
#define RUST_LOG_DEBUG 1
#define RUST_LOG_TRACE 2

#define EXIT_NORMAL 0
#define EXIT_FORK_ERROR 1
#define EXIT_EXEC_ERROR 2
Expand Down
15 changes: 9 additions & 6 deletions src/assets/src/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::io::Write;
use std::env::set_var;
use log::{debug, warn};
use std::path::PathBuf;
use std::fs::OpenOptions;
use std::os::raw::c_char;
use log::{debug, trace, warn};
use std::ffi::{CStr, CString};
use crate::fetch::asset_fetch;

Expand All @@ -29,12 +29,15 @@ unsafe fn load_c_string_list(ptr: *const *const c_char) -> Vec<String> {
string_list
}

/// Initialize the rust module log, enable trace level log when verbose is not `0`.
/// Initialize the rust module log, using info level log when verbose is `0`,
/// enable debug level when verbose is `1`, and others for trace level.
#[no_mangle]
pub unsafe extern "C" fn assets_log_init(verbose: u8, prefix: *const c_char) {
if verbose == FALSE { // bool value `FALSE`
if verbose == 0 { // info level
set_var("RUST_LOG", "info");
} else {
} else if verbose == 1 { // debug level
set_var("RUST_LOG", "debug");
} else { // trace level
set_var("RUST_LOG", "trace");
}
let log_prefix = load_c_string(prefix);
Expand Down Expand Up @@ -68,8 +71,8 @@ pub async unsafe extern "C" fn asset_update(
let name = load_c_string(file); // import c-style string
let sources = load_c_string_list(sources);
let assets_dir = load_c_string(assets_dir);
trace!("Working folder is `{}`", assets_dir);
trace!("Updating `{}` from {:?}", name, sources);
debug!("Working folder is `{}`", assets_dir);
debug!("Updating `{}` from {:?}", name, sources);

let assets_dir = PathBuf::from(&assets_dir);
let is_remote = |src: &str| {
Expand Down
10 changes: 7 additions & 3 deletions src/cleardns.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ void init(int argc, char *argv[]) { // return config file
}

void cleardns() { // cleardns service
uint8_t rust_log_level = RUST_LOG_INFO; // using info level as default
if (settings.verbose || settings.debug) {
LOG_LEVEL = LOG_DEBUG; // enable debug log level
assets_log_init(TRUE, LOG_PREFIX);
} else {
assets_log_init(FALSE, LOG_PREFIX);
rust_log_level = RUST_LOG_DEBUG;
if (settings.debug) {
rust_log_level = RUST_LOG_TRACE; // enable rust trace log
}
}
assets_log_init(rust_log_level, LOG_PREFIX); // setting rust log level

create_folder(EXPOSE_DIR);
create_folder(WORK_DIR);
chdir(EXPOSE_DIR);
Expand Down

0 comments on commit 75af2d2

Please sign in to comment.