From 7029c6035c8bb5ab8f8e45c255900edb5ffefb4f Mon Sep 17 00:00:00 2001 From: SecretX33 <4389757+SecretX33@users.noreply.github.com> Date: Sun, 27 Nov 2022 20:30:09 -0300 Subject: [PATCH] Removed log library to reduce binary size Set 'end_of_line' editorconfig to "LF" instead of "CRLF". --- .editorconfig | 2 +- Cargo.lock | 101 ---------------------------------------------- Cargo.toml | 9 +++-- src/log_macros.rs | 17 ++++++++ src/main.rs | 35 ++++++++-------- src/zs_log.rs | 46 --------------------- 6 files changed, 40 insertions(+), 170 deletions(-) create mode 100644 src/log_macros.rs delete mode 100644 src/zs_log.rs diff --git a/.editorconfig b/.editorconfig index fc96cc4..2bb5a9c 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,7 +2,7 @@ root = true [*] charset = utf-8 -end_of_line = crlf +end_of_line = lf indent_size = 4 indent_style = space insert_final_newline = false diff --git a/Cargo.lock b/Cargo.lock index c3acbd5..fc524e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,26 +20,6 @@ dependencies = [ "opaque-debug", ] -[[package]] -name = "aho-corasick" -version = "0.7.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" -dependencies = [ - "memchr", -] - -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - [[package]] name = "base64ct" version = "1.5.3" @@ -160,19 +140,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "env_logger" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - [[package]] name = "flate2" version = "1.0.25" @@ -211,15 +178,6 @@ dependencies = [ "digest", ] -[[package]] -name = "humantime" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" -dependencies = [ - "quick-error", -] - [[package]] name = "itoa" version = "1.0.4" @@ -241,21 +199,6 @@ version = "0.2.137" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" -[[package]] -name = "log" -version = "0.4.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "memchr" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" - [[package]] name = "miniz_oxide" version = "0.6.2" @@ -316,45 +259,12 @@ version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" -[[package]] -name = "pretty_env_logger" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d" -dependencies = [ - "env_logger", - "log", -] - -[[package]] -name = "quick-error" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" - [[package]] name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" -[[package]] -name = "regex" -version = "1.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.6.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" - [[package]] name = "same-file" version = "1.0.6" @@ -404,15 +314,6 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" -[[package]] -name = "termcolor" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" -dependencies = [ - "winapi-util", -] - [[package]] name = "threadpool" version = "1.8.1" @@ -527,9 +428,7 @@ dependencies = [ name = "zipsource" version = "0.1.0" dependencies = [ - "log", "pathdiff", - "pretty_env_logger", "size", "threadpool", "walkdir", diff --git a/Cargo.toml b/Cargo.toml index b17bee7..7377654 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,10 +6,13 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -log = "0.4.0" -pretty_env_logger = "0.4.0" walkdir = "2" zip = "0.6.3" pathdiff = "0.2.1" size = "0.4" -threadpool = "1.8.1" \ No newline at end of file +threadpool = "1.8.1" + +[profile.release] +opt-level = 3 +lto = true +incremental = true \ No newline at end of file diff --git a/src/log_macros.rs b/src/log_macros.rs new file mode 100644 index 0000000..55c8ade --- /dev/null +++ b/src/log_macros.rs @@ -0,0 +1,17 @@ +// Disable warnings + +#[allow(unused_macros)] + +macro_rules! log { + ($( $args:expr ),*) => { println!( $( $args ),* ); } +} + +// The debug version + +macro_rules! debug_log { + ($( $args:expr ),*) => { + if cfg!(debug_assertions) { + println!( $( $args ),* ); + } + } +} diff --git a/src/main.rs b/src/main.rs index 750b05e..0d4652b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod chars; -mod zs_log; +#[macro_use] +mod log_macros; use std::{env, fs, io, thread}; use std::collections::HashSet; @@ -10,7 +11,6 @@ use std::path::{Path, PathBuf}; use std::process::Command; use std::sync::{Arc, mpsc}; use std::sync::atomic::{AtomicU32, Ordering}; -use log::{debug, info, warn}; use size::{Size, Style}; use threadpool::ThreadPool; use walkdir::{WalkDir}; @@ -18,31 +18,28 @@ use zip::{CompressionMethod, ZipWriter}; use zip::result::ZipResult; use zip::write::FileOptions; use crate::chars::unescape; -use crate::zs_log::init_log; /// After some experimentation, on my machine this number was the fastest, no deep reason why /// it is this, though. const MAX_CONCURRENT_THREADS: usize = 9; fn main() { - init_log(); - - info!("Started collecting files"); + log!("Started collecting files"); let settings = get_settings(); - debug!("Settings: {:?}", settings); + debug_log!("Settings: {:?}", settings); let all_files = list_files_recursive(&settings.base_dir); let all_files_path = all_files.iter().map(|p| p.as_path()).collect(); - info!("Found a total of {} files in the folder.", all_files.len()); + log!("Found a total of {} files in the folder.", all_files.len()); let valid_files = filter_valid_files(&settings.base_dir, all_files_path); - info!("There are {} files to be zipped (out of {}, which means {} files were ignored)", valid_files.len(), all_files.len(), all_files.len() - valid_files.len()); + log!("There are {} files to be zipped (out of {}, which means {} files were ignored)", valid_files.len(), all_files.len(), all_files.len() - valid_files.len()); zip_files(&settings.zip_path, &settings.base_dir, valid_files).expect("Creation of zip failed"); let file_size = pretty_file_size(&settings.zip_path).expect("Could not read zip file info"); - info!("Zip was successfully created (file size is {})!", file_size.replace("iB", "B")) + log!("Zip was successfully created (file size is {})!", file_size.replace("iB", "B")); } fn get_settings() -> Settings { @@ -82,7 +79,7 @@ fn filter_valid_files<'a>(current_dir: &Path, all_files: HashSet<&'a Path>) -> H let chunks_amount = files_chunked.len(); let pool_size = calculate_ideal_parallelism(chunks_amount); - debug!("There are {} chunks of files to be processed (thread pool size: {})", chunks_amount, pool_size); + debug_log!("There are {} chunks of files to be processed (thread pool size: {})", chunks_amount, pool_size); let pool = ThreadPool::new(pool_size); let count = Arc::from(AtomicU32::new(0)); @@ -97,7 +94,7 @@ fn filter_valid_files<'a>(current_dir: &Path, all_files: HashSet<&'a Path>) -> H let files_path: HashSet<&Path> = files.iter().map(|p| p.as_path()).collect(); let mut command = build_check_ignore_command(¤t_dir, &files_path); let ignored_files = run_ignored_files_command(&mut command); - debug!("[{}] Chunk size: {}, ignored files size: {}", count.fetch_add(1, Ordering::Relaxed), files.len(), ignored_files.len()); + debug_log!("[{}] Chunk size: {}, ignored files size: {}", count.fetch_add(1, Ordering::Relaxed), files.len(), ignored_files.len()); tx.send(ignored_files).expect("channel will be there waiting for the pool"); }); @@ -110,7 +107,7 @@ fn filter_valid_files<'a>(current_dir: &Path, all_files: HashSet<&'a Path>) -> H let ignored_files: HashSet<_> = rx.iter().take(chunks_amount).flatten().collect(); let ignored_files: HashSet<&Path> = ignored_files.iter().map(|e| e.as_path()).collect(); - debug!("\nall_files: {:?}\nignored_files: {:?}\n", + debug_log!("\nall_files: {:?}\nignored_files: {:?}\n", all_files.iter().take(6).collect::>(), ignored_files.iter().take(6).collect::>() ); @@ -120,7 +117,7 @@ fn filter_valid_files<'a>(current_dir: &Path, all_files: HashSet<&'a Path>) -> H .filter(|&path| !ignored_files.contains(path) && !is_excluded_specially(current_dir, path)) .collect(); - debug!( + debug_log!( "ignored_files: {}, all_files_vector size: {}, valid_files size: {}", ignored_files.len(), all_files_vector.len(), @@ -160,7 +157,7 @@ fn is_excluded_specially(current_dir: &Path, file_to_check: &Path) -> bool { fn zip_files(zip_path: &Path, base_dir: &Path, files: HashSet<&Path>) -> ZipResult<()> { if zip_path.exists() { let filename = zip_path.file_name().and_then(|n| n.to_str()).unwrap(); - warn!("Removing old zip file: {}", filename); + log!("WARN: Removing old zip file: {}", filename); fs::remove_file(&zip_path).expect(&format!("Failed to delete zip file: {}", filename)); } let zip_file = File::create(&zip_path).unwrap(); @@ -176,7 +173,7 @@ fn zip_files(zip_path: &Path, base_dir: &Path, files: HashSet<&Path>) -> ZipResu zip.start_file(relative_path.to_str().unwrap(), options)?; zip.write_all(&*fs::read(x)?)?; - debug!("{}. Zipping file: {}", i + 1, x.to_str().unwrap_or("")); + debug_log!("{}. Zipping file: {}", i + 1, x.to_str().unwrap_or("")); } zip.finish().map(|_| ()) @@ -196,11 +193,11 @@ fn calculate_ideal_parallelism(job_amount: usize) -> usize { let available_parallelism = thread::available_parallelism(); if let Err(error) = &available_parallelism { - warn!("Could not determine the number of CPU cores this processor has, ZipSource will fallback to single-threaded mode, which can be quite slow.\nError message: {error}"); + log!("WARN: Could not determine the number of CPU cores this processor has, ZipSource will fallback to single-threaded mode, which can be quite slow.\nError message: {error}"); } let cores: usize = available_parallelism.ok().unwrap_or(NonZeroUsize::new(1).unwrap()).get(); - debug!("System Core Count: {cores}"); + debug_log!("System Core Count: {cores}"); MAX_CONCURRENT_THREADS.min(job_amount).max(1).min(cores) } @@ -208,4 +205,4 @@ fn calculate_ideal_parallelism(job_amount: usize) -> usize { struct Settings { base_dir: PathBuf, zip_path: PathBuf, -} \ No newline at end of file +} diff --git a/src/zs_log.rs b/src/zs_log.rs deleted file mode 100644 index ef5e0bc..0000000 --- a/src/zs_log.rs +++ /dev/null @@ -1,46 +0,0 @@ -use log::Level; -use pretty_env_logger::{formatted_builder}; -use pretty_env_logger::env_logger::fmt::{Color, Style, StyledValue}; -use pretty_env_logger::env_logger::Target; - -const LOG_ENV_VAR: &str = "RUST_LOG"; - -/// Initialize application logger. -pub fn init_log() { - let mut builder = formatted_builder(); - builder.target(Target::Stdout); - - builder.format(|f, record| { - use std::io::Write; - - let level = record.level(); - let args = record.args(); - let time_prefix = f.timestamp_millis(); - - let mut style = f.style(); - let level_prefix = colored_level(&mut style, level); - - match record.level() { - Level::Error | Level::Warn => writeln!(f, "{}: {}", level_prefix, args), - Level::Info => writeln!(f, "{args}"), - Level::Debug | Level::Trace => writeln!(f, "[{}] {}: {}", time_prefix, level_prefix, args), - } - }); - - match std::env::var(LOG_ENV_VAR) { - Ok(s) => builder.parse_filters(&s), - Err(_) => builder.parse_filters("info") - }; - - builder.init() -} - -fn colored_level<'a>(style: &'a mut Style, level: Level) -> StyledValue<'a, &'static str> { - match level { - Level::Trace => style.set_color(Color::Magenta).value("TRACE"), - Level::Debug => style.set_color(Color::Blue).value("DEBUG"), - Level::Info => style.set_color(Color::Green).value("INFO"), - Level::Warn => style.set_color(Color::Yellow).value("WARN"), - Level::Error => style.set_color(Color::Red).value("ERROR"), - } -}