Browse Source

feat: new log format of rust modules

dev
Dnomd343 2 years ago
parent
commit
337706c499
  1. 2
      include/utils/assets.h
  2. 25
      src/Cargo.lock
  3. 1
      src/assets/Cargo.toml
  4. 6
      src/assets/src/fetch.rs
  5. 23
      src/assets/src/ffi.rs
  6. 4
      src/cleardns.c

2
include/utils/assets.h

@ -23,7 +23,7 @@ uint32_t assets_size(asset **asset_list);
void assets_append(asset ***asset_list, asset *res); void assets_append(asset ***asset_list, asset *res);
/// Rust assets interface /// Rust assets interface
void assets_log_init(uint8_t verbose); void assets_log_init(uint8_t verbose, const char *prefix);
uint8_t asset_update(const char *file, char *const *sources, const char *assets_dir); uint8_t asset_update(const char *file, char *const *sources, const char *assets_dir);
#endif #endif

25
src/Cargo.lock

@ -51,6 +51,7 @@ checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800"
name = "assets" name = "assets"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono",
"env_logger", "env_logger",
"log", "log",
"reqwest", "reqwest",
@ -154,8 +155,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f" checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f"
dependencies = [ dependencies = [
"iana-time-zone", "iana-time-zone",
"js-sys",
"num-integer", "num-integer",
"num-traits", "num-traits",
"time",
"wasm-bindgen",
"winapi", "winapi",
] ]
@ -428,7 +432,7 @@ dependencies = [
"cfg-if", "cfg-if",
"js-sys", "js-sys",
"libc", "libc",
"wasi", "wasi 0.11.0+wasi-snapshot-preview1",
"wasm-bindgen", "wasm-bindgen",
] ]
@ -733,7 +737,7 @@ checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9"
dependencies = [ dependencies = [
"libc", "libc",
"log", "log",
"wasi", "wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys 0.45.0", "windows-sys 0.45.0",
] ]
@ -1255,6 +1259,17 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "time"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a"
dependencies = [
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"winapi",
]
[[package]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"
@ -1485,6 +1500,12 @@ dependencies = [
"try-lock", "try-lock",
] ]
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"

1
src/assets/Cargo.toml

@ -9,6 +9,7 @@ crate-type = ["staticlib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
chrono = "0.4.23"
env_logger = "0.10.0" env_logger = "0.10.0"
log = "0.4.17" log = "0.4.17"
reqwest = { version = "0.11.14", features = ["deflate", "gzip", "brotli"] } reqwest = { version = "0.11.14", features = ["deflate", "gzip", "brotli"] }

6
src/assets/src/fetch.rs

@ -41,14 +41,14 @@ async fn http_fetch(url: &str, timeout: u64) -> Result<Vec<String>, String> {
let client = ClientBuilder::new(reqwest::Client::new()) let client = ClientBuilder::new(reqwest::Client::new())
.with(RetryTransientMiddleware::new_with_policy(retry_policy)) .with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build(); .build();
debug!("Start downloading `{}`", url); info!("Start downloading `{}`", url);
match client.get(url) match client.get(url)
.timeout(Duration::from_secs(timeout)) .timeout(Duration::from_secs(timeout))
.send().await { .send().await {
Ok(response) => { Ok(response) => {
match response.text().await { match response.text().await {
Ok(text) => { Ok(text) => {
info!("Remote file `{}` download success", url); debug!("Remote file `{}` download success", url);
Ok(asset_tidy(&text)) Ok(asset_tidy(&text))
}, },
Err(err) => Err(format!("http content error: {}", err)) Err(err) => Err(format!("http content error: {}", err))
@ -66,7 +66,7 @@ async fn local_fetch(path: &str) -> Result<Vec<String>, String> {
if let Err(err) = file.read_to_string(&mut text) { if let Err(err) = file.read_to_string(&mut text) {
return Err(format!("file `{}` read failed: {}", path, err)); return Err(format!("file `{}` read failed: {}", path, err));
}; };
info!("Local file `{}` read success", path); debug!("Local file `{}` read success", path);
Ok(asset_tidy(&text)) Ok(asset_tidy(&text))
}, },
Err(err) => Err(format!("file `{}` open failed: {}", path, err)), Err(err) => Err(format!("file `{}` open failed: {}", path, err)),

23
src/assets/src/ffi.rs

@ -31,13 +31,32 @@ unsafe fn load_c_string_list(ptr: *const *const c_char) -> Vec<String> {
/// Initialize the rust module log, enable trace level log when verbose is not `0`. /// Initialize the rust module log, enable trace level log when verbose is not `0`.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn assets_log_init(verbose: u8) { pub unsafe extern "C" fn assets_log_init(verbose: u8, prefix: *const c_char) {
if verbose == FALSE { // bool value `FALSE` if verbose == FALSE { // bool value `FALSE`
set_var("RUST_LOG", "info"); set_var("RUST_LOG", "info");
} else { } else {
set_var("RUST_LOG", "trace"); set_var("RUST_LOG", "trace");
} }
env_logger::init(); let log_prefix = load_c_string(prefix);
env_logger::builder()
.target(env_logger::Target::Stderr)
.format(move |buf, record| {
let prefix = format!(
"\x1b[36m[{}]\x1b[0m \x1b[90m{}\x1b[0m \x1b[35m[{}]\x1b[0m",
log_prefix,
chrono::Local::now().format("%Y-%m-%d %H:%M:%S%.3f").to_string(),
record.module_path().unwrap()
);
let level = format!("{}[{}]\x1b[0m", match record.level() {
log::Level::Trace => "\x1b[39m",
log::Level::Debug => "\x1b[36m",
log::Level::Info => "\x1b[32m",
log::Level::Warn => "\x1b[33m",
log::Level::Error => "\x1b[31m",
}, record.level());
write!(buf, "{} {} {:?}\n", prefix, level, record.args())
})
.init();
} }
/// Update the specified resource file, return `0` on failure. /// Update the specified resource file, return `0` on failure.

4
src/cleardns.c

@ -66,9 +66,9 @@ void init(int argc, char *argv[]) { // return config file
void cleardns() { // cleardns service void cleardns() { // cleardns service
if (settings.verbose || settings.debug) { if (settings.verbose || settings.debug) {
LOG_LEVEL = LOG_DEBUG; // enable debug log level LOG_LEVEL = LOG_DEBUG; // enable debug log level
assets_log_init(TRUE); assets_log_init(TRUE, LOG_PREFIX);
} else { } else {
assets_log_init(FALSE); assets_log_init(FALSE, LOG_PREFIX);
} }
create_folder(EXPOSE_DIR); create_folder(EXPOSE_DIR);
create_folder(WORK_DIR); create_folder(WORK_DIR);

Loading…
Cancel
Save