diff --git a/Cargo.lock b/Cargo.lock index cf095a1..be9322c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -25,18 +25,15 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bumpalo" -version = "3.14.0" +version = "3.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec" +checksum = "a3b1be7772ee4501dba05acbe66bb1e8760f6a6c474a36035631638e4415f130" [[package]] name = "cc" -version = "1.0.83" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" -dependencies = [ - "libc", -] +checksum = "7f9fa1897e4325be0d68d48df6aa1a71ac2ed4d27723887e7754192705350730" [[package]] name = "cfg-if" @@ -46,16 +43,16 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.31" +version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38" +checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b" dependencies = [ "android-tzdata", "iana-time-zone", "js-sys", "num-traits", "wasm-bindgen", - "windows-targets 0.48.5", + "windows-targets", ] [[package]] @@ -66,9 +63,9 @@ checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" [[package]] name = "iana-time-zone" -version = "0.1.59" +version = "0.1.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6a67363e2aa4443928ce15e57ebae94fd8949958fd1223c4cfc0cd473ad7539" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" dependencies = [ "android_system_properties", "core-foundation-sys", @@ -89,18 +86,18 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.66" +version = "0.3.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca" +checksum = "406cda4b368d531c842222cf9d2600a9a4acce8d29423695379c6868a143a9ee" dependencies = [ "wasm-bindgen", ] [[package]] name = "libc" -version = "0.2.152" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "log" @@ -110,9 +107,9 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] name = "num-traits" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" dependencies = [ "autocfg", ] @@ -125,9 +122,9 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "proc-macro2" -version = "1.0.76" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ "unicode-ident", ] @@ -143,16 +140,16 @@ dependencies = [ [[package]] name = "scorched" -version = "0.5.1" +version = "0.5.2" dependencies = [ "chrono", ] [[package]] name = "syn" -version = "2.0.48" +version = "2.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" +checksum = "74f1bdc9872430ce9b75da68329d1c1746faf50ffac5f19e02b71e37ff881ffb" dependencies = [ "proc-macro2", "quote", @@ -167,9 +164,9 @@ checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "wasm-bindgen" -version = "0.2.89" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e" +checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -177,9 +174,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.89" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826" +checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b" dependencies = [ "bumpalo", "log", @@ -192,9 +189,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.89" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2" +checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -202,9 +199,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.89" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283" +checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66" dependencies = [ "proc-macro2", "quote", @@ -215,9 +212,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.89" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" +checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" [[package]] name = "windows-core" @@ -225,22 +222,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.52.0", -] - -[[package]] -name = "windows-targets" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" -dependencies = [ - "windows_aarch64_gnullvm 0.48.5", - "windows_aarch64_msvc 0.48.5", - "windows_i686_gnu 0.48.5", - "windows_i686_msvc 0.48.5", - "windows_x86_64_gnu 0.48.5", - "windows_x86_64_gnullvm 0.48.5", - "windows_x86_64_msvc 0.48.5", + "windows-targets", ] [[package]] @@ -249,93 +231,51 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" dependencies = [ - "windows_aarch64_gnullvm 0.52.0", - "windows_aarch64_msvc 0.52.0", - "windows_i686_gnu 0.52.0", - "windows_i686_msvc 0.52.0", - "windows_x86_64_gnu 0.52.0", - "windows_x86_64_gnullvm 0.52.0", - "windows_x86_64_msvc 0.52.0", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" - [[package]] name = "windows_aarch64_gnullvm" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" -[[package]] -name = "windows_aarch64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" - [[package]] name = "windows_aarch64_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" -[[package]] -name = "windows_i686_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" - [[package]] name = "windows_i686_gnu" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" -[[package]] -name = "windows_i686_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" - [[package]] name = "windows_i686_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" -[[package]] -name = "windows_x86_64_gnu" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" - [[package]] name = "windows_x86_64_gnu" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" - [[package]] name = "windows_x86_64_gnullvm" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" -[[package]] -name = "windows_x86_64_msvc" -version = "0.48.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" - [[package]] name = "windows_x86_64_msvc" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 367759e..48dca0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "scorched" description = "A simple logging library for scorching all those pesky bugs." -version = "0.5.1" +version = "0.5.2" edition = "2021" license = "Apache-2.0" [dependencies] -chrono = "0.4.31" +chrono = "0.4.34" diff --git a/src/lib.rs b/src/lib.rs index 556ffbf..a3cd2ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,12 @@ mod utils; mod test; -use std::{env, fmt::Debug, fs::OpenOptions, io::prelude::*}; +use std::{ + env, + fmt::Debug, + fs::{File, OpenOptions}, + io::prelude::*, +}; use utils::{importance_tags::*, time_utils}; @@ -26,7 +31,7 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION"); /// Changes the environment variable for logging path pub fn set_logging_path(path: &str) { - if path[path.len() - 1..] != *"/" { + if path[path.len() - 1..] != *"/" || path[path.len() - 1..] != *"\\" { let mut path = path.to_string(); path.push('/'); } @@ -52,7 +57,10 @@ pub fn log_this(data: LogData) { std::fs::create_dir_all( env::var("SCORCHED_LOG_PATH").unwrap_or_else(|_| "logs/".to_string()), ) - .log_expect(LogImportance::Error, "Failed to create logs folder"); + .log_expect( + LogImportance::Error, + "Failed to create full path to logs folder", + ); } let file = OpenOptions::new().append(true).create(true).open(format!( @@ -61,6 +69,7 @@ pub fn log_this(data: LogData) { time_utils::get_formatted_time(time_utils::TimeFormat::Date) )); + // Appends the prefix to the message if it exists let message = { match env::var("SCORCHED_LOG_PREFIX") { Ok(val) => format!("{} {}", val, data.message), @@ -68,80 +77,38 @@ pub fn log_this(data: LogData) { } }; - match data.importance { - LogImportance::Error => { - file.unwrap() - .write_all( - format!( - "{} [ERROR] {}\n", - time_utils::get_formatted_time(time_utils::TimeFormat::DateTime), - message - ) - .as_bytes(), - ) - .unwrap(); - println!( - "{} {} {}", - time_utils::get_formatted_time(time_utils::TimeFormat::Time), - error_tag(), - message - ); - } - LogImportance::Warning => { - file.unwrap() - .write_all( - format!( - "{} [WARNING] {}\n", - time_utils::get_formatted_time(time_utils::TimeFormat::DateTime), - message - ) - .as_bytes(), - ) - .unwrap(); - println!( - "{} {} {}", - time_utils::get_formatted_time(time_utils::TimeFormat::Time), - warning_tag(), - message - ); - } - LogImportance::Info => { - file.unwrap() - .write_all( - format!( - "{} [INFO] {}\n", - time_utils::get_formatted_time(time_utils::TimeFormat::DateTime), - message - ) - .as_bytes(), - ) - .unwrap(); - println!( - "{} {} {}", - time_utils::get_formatted_time(time_utils::TimeFormat::Time), - info_tag(), - message - ); - } - // Mainly unused, but still available - LogImportance::Debug => { - file.unwrap() - .write_all( - format!( - "{} [DEBUG] {}\n", - time_utils::get_formatted_time(time_utils::TimeFormat::DateTime), - message - ) - .as_bytes(), - ) - .unwrap(); - println!( - "{} {} {}", - time_utils::get_formatted_time(time_utils::TimeFormat::Time), - debug_tag(), + fn write_log(importance: LogImportance, message: &str, file: &mut File) { + let tag = match importance { + LogImportance::Error => (ERROR_TAG, "ERROR"), + LogImportance::Warning => (WARNING_TAG, "WARNING"), + LogImportance::Info => (INFO_TAG, "INFO"), + LogImportance::Debug => (DEBUG_TAG, "DEBUG"), + }; + + file.write_all( + format!( + "{} [{}] {}\n", + time_utils::get_formatted_time(time_utils::TimeFormat::DateTime), + tag.1, message - ); - } + ) + .as_bytes(), + ) + .unwrap(); + + println!( + "{} {} {}", + time_utils::get_formatted_time(time_utils::TimeFormat::Time), + tag.0, + message + ); + } + + match data.importance { + LogImportance::Error => write_log(LogImportance::Error, &message, &mut file.unwrap()), + LogImportance::Warning => write_log(LogImportance::Warning, &message, &mut file.unwrap()), + LogImportance::Info => write_log(LogImportance::Info, &message, &mut file.unwrap()), + LogImportance::Debug => write_log(LogImportance::Debug, &message, &mut file.unwrap()), } } diff --git a/src/macros.rs b/src/macros.rs index 501f5ed..ba8bade 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,4 +1,3 @@ - /// logf!() is a macro that allows you to log without needing to explicitly format the log message #[macro_export] macro_rules! logf { diff --git a/src/utils/importance_tags.rs b/src/utils/importance_tags.rs index fe0c3d0..b3b7a2a 100644 --- a/src/utils/importance_tags.rs +++ b/src/utils/importance_tags.rs @@ -1,22 +1,14 @@ /// This file contains the tags for the different levels of importance. -/// These are pasted in front of the message when logging. +/// These are used in front of the message when logging and are colored. /// Error tag displays red. -pub(crate) const fn error_tag() -> &'static str { - "\x1b[31m[ERROR]\x1b[0m" -} +pub(crate) const ERROR_TAG: &str = "\x1b[31m[ERROR]\x1b[0m"; /// Warning tag displays yellow. -pub(crate) const fn warning_tag() -> &'static str { - "\x1b[33m[WARNING]\x1b[0m" -} +pub(crate) const WARNING_TAG: &str = "\x1b[33m[WARNING]\x1b[0m"; /// Info tag displays light gray. -pub(crate) const fn info_tag() -> &'static str { - "\x1b[37m[INFO]\x1b[0m" -} +pub(crate) const INFO_TAG: &str = "\x1b[37m[INFO]\x1b[0m"; /// Debug tag displays pink. -pub(crate) const fn debug_tag() -> &'static str { - "\x1b[35m[DEBUG]\x1b[0m" -} +pub(crate) const DEBUG_TAG: &str = "\x1b[35m[DEBUG]\x1b[0m"; \ No newline at end of file