From d7f0aa363b948c4696c06161b5e9c9403afeeaa7 Mon Sep 17 00:00:00 2001 From: s1rius Date: Sat, 14 Oct 2023 17:31:59 +0800 Subject: [PATCH] add import fmt --- examples/android_preview/src/lib.rs | 7 ++- examples/hello_world.rs | 31 +++++++++--- examples/nest_log/src/main.rs | 11 ++++- ezlog-core/src/appender.rs | 29 ++++++++--- ezlog-core/src/compress.rs | 11 ++++- ezlog-core/src/config.rs | 62 ++++++++++++++++++----- ezlog-core/src/crypto.rs | 10 +++- ezlog-core/src/decode.rs | 40 ++++++++++++--- ezlog-core/src/errors.rs | 13 +++-- ezlog-core/src/events.rs | 3 +- ezlog-core/src/ffi_c.rs | 15 +++--- ezlog-core/src/ffi_java.rs | 49 ++++++++++++++---- ezlog-core/src/init.rs | 13 ++++- ezlog-core/src/lib.rs | 74 ++++++++++++++++++---------- ezlog-core/src/logger.rs | 59 ++++++++++++++++------ ezlog-core/src/recorder.rs | 12 ++++- ezlog-core/src/thread_name.rs | 1 + ezlog-core/tests/integration_test.rs | 5 +- ezlogcli/src/main.rs | 21 ++++++-- rustfmt.toml | 3 ++ 20 files changed, 361 insertions(+), 108 deletions(-) create mode 100644 rustfmt.toml diff --git a/examples/android_preview/src/lib.rs b/examples/android_preview/src/lib.rs index 72f0001..30bb9d5 100644 --- a/examples/android_preview/src/lib.rs +++ b/examples/android_preview/src/lib.rs @@ -1,5 +1,10 @@ use ezlog::{ - CipherKind, CompressKind, EZLogCallback, EZLogConfig, EZLogConfigBuilder, EZRecordBuilder, + CipherKind, + CompressKind, + EZLogCallback, + EZLogConfig, + EZLogConfigBuilder, + EZRecordBuilder, DEFAULT_LOG_NAME, }; use rand::Rng; diff --git a/examples/hello_world.rs b/examples/hello_world.rs index db63489..a168509 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -1,16 +1,35 @@ use std::fs::OpenOptions; -use std::io::{BufReader, BufWriter, Cursor, Read}; - +use std::io::{ + BufReader, + BufWriter, + Cursor, + Read, +}; use std::thread; use std::time::Duration; use ezlog::Level; use ezlog::{ - create_log, CipherKind, CompressKind, EZLogCallback, EZLogConfig, EZLogConfigBuilder, - EventPrinter, Header, + create_log, + CipherKind, + CompressKind, + EZLogCallback, + EZLogConfig, + EZLogConfigBuilder, + EventPrinter, + Header, +}; +use ezlog::{ + EZLogger, + EZMsg, +}; +use log::{ + debug, + error, + info, + trace, + warn, }; -use ezlog::{EZLogger, EZMsg}; -use log::{debug, error, info, trace, warn}; use rand::Rng; use time::OffsetDateTime; diff --git a/examples/nest_log/src/main.rs b/examples/nest_log/src/main.rs index 0ad02da..7ca9fb0 100644 --- a/examples/nest_log/src/main.rs +++ b/examples/nest_log/src/main.rs @@ -2,8 +2,15 @@ use std::thread::sleep; use std::time::Duration; use ezlog::EZLogConfigBuilder; -use log::{error, info, warn}; -use log::{LevelFilter, Log}; +use log::{ + error, + info, + warn, +}; +use log::{ + LevelFilter, + Log, +}; extern crate log; diff --git a/ezlog-core/src/appender.rs b/ezlog-core/src/appender.rs index 4ccc76f..d7d52b4 100644 --- a/ezlog-core/src/appender.rs +++ b/ezlog-core/src/appender.rs @@ -1,12 +1,22 @@ use std::{ fs::OpenOptions, - io::{BufReader, BufWriter, Error, ErrorKind}, + io::{ + BufReader, + BufWriter, + Error, + ErrorKind, + }, path::PathBuf, rc::Rc, }; + use time::OffsetDateTime; -use crate::{events::event, logger::Header, *}; +use crate::{ + events::event, + logger::Header, + *, +}; pub trait AppenderInner: Write { /// check have enough space to write record @@ -441,14 +451,21 @@ impl Write for NopInner { #[cfg(test)] mod tests { - use std::fs::{self, File, OpenOptions}; - use std::io::{BufReader, Seek, SeekFrom}; + use std::fs::{ + self, + File, + OpenOptions, + }; + use std::io::{ + BufReader, + Seek, + SeekFrom, + }; use time::Duration; - use crate::config::EZLogConfigBuilder; - use super::*; + use crate::config::EZLogConfigBuilder; fn create_all_feature_config() -> EZLogConfigBuilder { let key = b"an example very very secret key."; diff --git a/ezlog-core/src/compress.rs b/ezlog-core/src/compress.rs index d74f71e..9ba1cc3 100644 --- a/ezlog-core/src/compress.rs +++ b/ezlog-core/src/compress.rs @@ -1,6 +1,13 @@ -use std::io::{Read, Write}; +use std::io::{ + Read, + Write, +}; -use crate::{CompressLevel, Compression, Decompression}; +use crate::{ + CompressLevel, + Compression, + Decompression, +}; pub struct ZlibCodec { level: flate2::Compression, diff --git a/ezlog-core/src/config.rs b/ezlog-core/src/config.rs index a7a82fe..f303aed 100644 --- a/ezlog-core/src/config.rs +++ b/ezlog-core/src/config.rs @@ -2,20 +2,47 @@ use std::{ cmp, collections::hash_map::DefaultHasher, fmt, - fs::{self, File, OpenOptions}, - hash::{Hash, Hasher}, - path::{Path, PathBuf}, + fs::{ + self, + File, + OpenOptions, + }, + hash::{ + Hash, + Hasher, + }, + path::{ + Path, + PathBuf, + }, }; -use memmap2::{MmapMut, MmapOptions}; -use time::{format_description, Date, Duration, OffsetDateTime}; +use memmap2::{ + MmapMut, + MmapOptions, +}; +use time::{ + format_description, + Date, + Duration, + OffsetDateTime, +}; use crate::events::Event; #[allow(unused_imports)] use crate::EZLogger; use crate::{ - errors::LogError, events::event, logger::Header, CipherKind, CompressKind, CompressLevel, - Version, DEFAULT_LOG_FILE_SUFFIX, DEFAULT_LOG_NAME, DEFAULT_MAX_LOG_SIZE, LOG_LEVEL_NAMES, + errors::LogError, + events::event, + logger::Header, + CipherKind, + CompressKind, + CompressLevel, + Version, + DEFAULT_LOG_FILE_SUFFIX, + DEFAULT_LOG_NAME, + DEFAULT_MAX_LOG_SIZE, + LOG_LEVEL_NAMES, MIN_LOG_SIZE, }; @@ -548,10 +575,23 @@ impl fmt::Display for Level { #[cfg(test)] mod tests { - use std::fs::{self, OpenOptions}; - - use crate::{appender::EZAppender, CipherKind, CompressKind, EZLogConfigBuilder}; - use time::{macros::datetime, Duration, OffsetDateTime}; + use std::fs::{ + self, + OpenOptions, + }; + + use time::{ + macros::datetime, + Duration, + OffsetDateTime, + }; + + use crate::{ + appender::EZAppender, + CipherKind, + CompressKind, + EZLogConfigBuilder, + }; #[test] fn test_config_cipher_hash() { diff --git a/ezlog-core/src/crypto.rs b/ezlog-core/src/crypto.rs index 15928b3..ad169e1 100644 --- a/ezlog-core/src/crypto.rs +++ b/ezlog-core/src/crypto.rs @@ -1,8 +1,14 @@ -use aead::{Aead, KeyInit}; +use aead::{ + Aead, + KeyInit, +}; use aes_gcm_siv::Nonce; use crate::errors::LogError; -use crate::{Decryptor, Encryptor}; +use crate::{ + Decryptor, + Encryptor, +}; pub struct Aes256GcmSiv { // 96-bits; unique per message diff --git a/ezlog-core/src/decode.rs b/ezlog-core/src/decode.rs index d48b1e9..3c45716 100644 --- a/ezlog-core/src/decode.rs +++ b/ezlog-core/src/decode.rs @@ -1,11 +1,27 @@ -use std::io::{self, BufRead, Cursor, Write}; +use std::io::{ + self, + BufRead, + Cursor, + Write, +}; -use byteorder::{BigEndian, ReadBytesExt}; +use byteorder::{ + BigEndian, + ReadBytesExt, +}; use integer_encoding::VarIntReader; use crate::{ - errors::LogError, Compress, Cryptor, EZLogger, Header, NonceGenFn, Result, Version, - RECORD_SIGNATURE_END, RECORD_SIGNATURE_START, + errors::LogError, + Compress, + Cryptor, + EZLogger, + Header, + NonceGenFn, + Result, + Version, + RECORD_SIGNATURE_END, + RECORD_SIGNATURE_START, }; #[inline] @@ -170,10 +186,20 @@ pub fn decode_record_content( #[cfg(test)] mod tests { - use crate::decode::decode_logs_count; - use crate::{decode, EZLogger, EZRecordBuilder, Header}; use std::fs; - use std::io::{BufReader, Cursor, Read}; + use std::io::{ + BufReader, + Cursor, + Read, + }; + + use crate::decode::decode_logs_count; + use crate::{ + decode, + EZLogger, + EZRecordBuilder, + Header, + }; #[cfg(feature = "decode")] fn create_all_feature_config() -> crate::EZLogConfig { diff --git a/ezlog-core/src/errors.rs b/ezlog-core/src/errors.rs index f3a9058..533444e 100644 --- a/ezlog-core/src/errors.rs +++ b/ezlog-core/src/errors.rs @@ -1,6 +1,12 @@ -use std::{ffi::NulError, io}; +use std::{ + ffi::NulError, + io, +}; -use crossbeam_channel::{RecvError, TrySendError}; +use crossbeam_channel::{ + RecvError, + TrySendError, +}; use thiserror::Error; #[derive(Error, Debug)] @@ -64,9 +70,10 @@ mod tests { #[test] fn test_error() { - use crate::errors::LogError; use std::io; + use crate::errors::LogError; + let err = LogError::IoError(io::Error::new(io::ErrorKind::Other, "test")); assert_eq!(err.to_string(), "io error: test"); } diff --git a/ezlog-core/src/events.rs b/ezlog-core/src/events.rs index c4cec0b..c611b96 100644 --- a/ezlog-core/src/events.rs +++ b/ezlog-core/src/events.rs @@ -128,9 +128,10 @@ pub(crate) fn android_print(record: std::fmt::Arguments) { android_logger::log(&s); } -use crate::errors::LogError; pub(crate) use println_with_time; +use crate::errors::LogError; + struct NopEvent; impl EventListener for NopEvent { fn on_event(&self, _event: Event, _desc: &str) {} diff --git a/ezlog-core/src/ffi_c.rs b/ezlog-core/src/ffi_c.rs index 6ea2bb6..1fe374b 100644 --- a/ezlog-core/src/ffi_c.rs +++ b/ezlog-core/src/ffi_c.rs @@ -1,3 +1,11 @@ +use core::slice; +use std::ffi::CStr; +use std::ffi::CString; +use std::ffi::NulError; + +use libc::c_char; +use libc::c_uchar; +use libc::c_uint; use libc::c_void; use time::Duration; @@ -5,13 +13,6 @@ use crate::config::Level; use crate::events::EventPrinter; use crate::recorder::EZRecordBuilder; use crate::*; -use core::slice; -use libc::c_char; -use libc::c_uchar; -use libc::c_uint; -use std::ffi::CStr; -use std::ffi::CString; -use std::ffi::NulError; /// Init ezlog, must call before any other function #[no_mangle] diff --git a/ezlog-core/src/ffi_java.rs b/ezlog-core/src/ffi_java.rs index d50cd34..1b0a26f 100644 --- a/ezlog-core/src/ffi_java.rs +++ b/ezlog-core/src/ffi_java.rs @@ -1,23 +1,52 @@ use std::sync::Arc; -use crate::errors::LogError; -use crate::events::Event::{self, *}; -use crate::{ - event, set_boxed_callback, thread_name, CipherKind, CompressKind, CompressLevel, - EZLogConfigBuilder, EZRecordBuilder, Level, +use jni::objects::{ + JByteArray, + JObjectArray, + JValueGen, }; -use jni::objects::{JByteArray, JObjectArray, JValueGen}; use jni::sys::jlong; use jni::{ errors::JniError, - objects::{GlobalRef, JClass, JObject, JString, JValue}, + objects::{ + GlobalRef, + JClass, + JObject, + JString, + JValue, + }, strings::JNIString, - sys::{jboolean, jint, JNI_VERSION_1_6}, - JNIEnv, JavaVM, + sys::{ + jboolean, + jint, + JNI_VERSION_1_6, + }, + JNIEnv, + JavaVM, }; use libc::c_void; use once_cell::sync::OnceCell; -use time::{Duration, OffsetDateTime}; +use time::{ + Duration, + OffsetDateTime, +}; + +use crate::errors::LogError; +use crate::events::Event::{ + self, + *, +}; +use crate::{ + event, + set_boxed_callback, + thread_name, + CipherKind, + CompressKind, + CompressLevel, + EZLogConfigBuilder, + EZRecordBuilder, + Level, +}; static JVM: OnceCell> = OnceCell::new(); diff --git a/ezlog-core/src/init.rs b/ezlog-core/src/init.rs index d7e52bb..34bc75b 100644 --- a/ezlog-core/src/init.rs +++ b/ezlog-core/src/init.rs @@ -1,7 +1,13 @@ use std::ptr; use crate::{ - EZLogCallback, EZMsg, EZRecord, EventListener, EventPrinter, Formatter, LogService, + EZLogCallback, + EZMsg, + EZRecord, + EventListener, + EventPrinter, + Formatter, + LogService, }; /// InitBuilder is used to init ezlog @@ -302,7 +308,10 @@ impl Formatter for FormatterProxy { pub struct EZLog {} #[cfg(feature = "log")] -use log::{Metadata, Record}; +use log::{ + Metadata, + Record, +}; #[cfg(feature = "log")] impl log::Log for EZLog { diff --git a/ezlog-core/src/lib.rs b/ezlog-core/src/lib.rs index 25feb8c..513f5cf 100644 --- a/ezlog-core/src/lib.rs +++ b/ezlog-core/src/lib.rs @@ -63,40 +63,47 @@ mod ffi_c; #[allow(non_snake_case)] mod ffi_java; +use std::error::Error; +use std::path::PathBuf; +use std::{ + collections::HashMap, + hash::Hash, + io::{ + self, + Cursor, + Read, + Write, + }, + mem::MaybeUninit, + sync::Once, + thread, +}; + +use crossbeam_channel::{ + Sender, + TrySendError, +}; +use memmap2::MmapMut; +use time::Duration; +use time::OffsetDateTime; + pub use self::config::EZLogConfig; pub use self::config::EZLogConfigBuilder; pub use self::config::Level; pub use self::errors::LogError; +pub(crate) use self::events::event; pub use self::events::Event; pub use self::events::EventListener; pub use self::events::EventPrinter; pub use self::init::InitBuilder; pub use self::init::MsgHandler; +pub use self::logger::create_compress; +pub use self::logger::create_cryptor; pub use self::logger::EZLogger; pub use self::logger::Header; -pub use self::logger::create_cryptor; -pub use self::logger::create_compress; pub use self::recorder::EZRecord; pub use self::recorder::EZRecordBuilder; -pub(crate) use self::events::event; - -use crossbeam_channel::{Sender, TrySendError}; -use memmap2::MmapMut; -use time::Duration; -use time::OffsetDateTime; - -use std::error::Error; -use std::path::PathBuf; -use std::{ - collections::HashMap, - hash::Hash, - io::{self, Cursor, Read, Write}, - mem::MaybeUninit, - sync::Once, - thread, -}; - /// A [EZLogger] default name. current is "default". pub const DEFAULT_LOG_NAME: &str = "default"; pub(crate) const FILE_SIGNATURE: &[u8; 2] = b"ez"; @@ -893,19 +900,32 @@ impl From for u8 { #[cfg(test)] mod tests { - use crate::recorder::EZRecordBuilder; - use flate2::{bufread::ZlibDecoder, write::ZlibEncoder, Compression}; - use std::io::{Read, Write}; + use std::io::{ + Read, + Write, + }; + + #[cfg(feature = "decode")] + use aead::{ + Aead, + KeyInit, + }; + use flate2::{ + bufread::ZlibDecoder, + write::ZlibEncoder, + Compression, + }; use time::OffsetDateTime; + use crate::recorder::EZRecordBuilder; use crate::Header; use crate::{ - config::EZLogConfigBuilder, EZLogConfig, RECORD_SIGNATURE_END, RECORD_SIGNATURE_START, + config::EZLogConfigBuilder, + EZLogConfig, + RECORD_SIGNATURE_END, + RECORD_SIGNATURE_START, }; - #[cfg(feature = "decode")] - use aead::{Aead, KeyInit}; - fn create_config() -> EZLogConfig { EZLogConfig::default() } diff --git a/ezlog-core/src/logger.rs b/ezlog-core/src/logger.rs index 238729a..465f657 100644 --- a/ezlog-core/src/logger.rs +++ b/ezlog-core/src/logger.rs @@ -1,24 +1,55 @@ -use byteorder::{BigEndian, WriteBytesExt}; -use integer_encoding::VarIntWriter; use std::io::Read; use std::path::PathBuf; - -use std::{fs, io}; -use std::{io::Write, rc::Rc}; - -use crate::crypto::{Aes128GcmSiv, Aes256GcmSiv}; -use crate::events::Event::{self}; -use crate::{ - appender::EZAppender, compress::ZlibCodec, errors::LogError, CipherKind, Compress, - CompressKind, Cryptor, EZLogConfig, EZRecord, RECORD_SIGNATURE_END, RECORD_SIGNATURE_START, +use std::{ + fs, + io, }; -use crate::{errors, event, NonceGenFn, V1_LOG_HEADER_SIZE}; -use crate::{Version, V2_LOG_HEADER_SIZE}; +use std::{ + io::Write, + rc::Rc, +}; + use byteorder::ReadBytesExt; +use byteorder::{ + BigEndian, + WriteBytesExt, +}; +use integer_encoding::VarIntWriter; use time::OffsetDateTime; #[cfg(feature = "decode")] -use crate::crypto::{Aes128Gcm, Aes256Gcm}; +use crate::crypto::{ + Aes128Gcm, + Aes256Gcm, +}; +use crate::crypto::{ + Aes128GcmSiv, + Aes256GcmSiv, +}; +use crate::events::Event::{self,}; +use crate::{ + appender::EZAppender, + compress::ZlibCodec, + errors::LogError, + CipherKind, + Compress, + CompressKind, + Cryptor, + EZLogConfig, + EZRecord, + RECORD_SIGNATURE_END, + RECORD_SIGNATURE_START, +}; +use crate::{ + errors, + event, + NonceGenFn, + V1_LOG_HEADER_SIZE, +}; +use crate::{ + Version, + V2_LOG_HEADER_SIZE, +}; type Result = std::result::Result; diff --git a/ezlog-core/src/recorder.rs b/ezlog-core/src/recorder.rs index 207508f..9f6ddd3 100644 --- a/ezlog-core/src/recorder.rs +++ b/ezlog-core/src/recorder.rs @@ -1,14 +1,22 @@ use std::{ collections::hash_map::DefaultHasher, - hash::{Hash, Hasher}, + hash::{ + Hash, + Hasher, + }, thread, }; -use crate::{EZLogConfig, Level, DEFAULT_LOG_NAME}; #[cfg(feature = "log")] use log::Record; use time::OffsetDateTime; +use crate::{ + EZLogConfig, + Level, + DEFAULT_LOG_NAME, +}; + /// Single Log record #[derive(Debug, Clone)] pub struct EZRecord { diff --git a/ezlog-core/src/thread_name.rs b/ezlog-core/src/thread_name.rs index aaadfd5..1b3579c 100644 --- a/ezlog-core/src/thread_name.rs +++ b/ezlog-core/src/thread_name.rs @@ -57,6 +57,7 @@ fn get_name() -> String { #[cfg(target_os = "windows")] fn get_name() -> String { use std::thread; + use windows::core::HRESULT; use windows::core::PWSTR; use windows_sys::Win32::System::Threading; diff --git a/ezlog-core/tests/integration_test.rs b/ezlog-core/tests/integration_test.rs index f90509f..2c75744 100644 --- a/ezlog-core/tests/integration_test.rs +++ b/ezlog-core/tests/integration_test.rs @@ -1,6 +1,9 @@ use ezlog::Event; use ezlog::LogError; -use ezlog::{EZLogConfigBuilder, EZRecord}; +use ezlog::{ + EZLogConfigBuilder, + EZRecord, +}; use time::OffsetDateTime; #[test] diff --git a/ezlogcli/src/main.rs b/ezlogcli/src/main.rs index 8a960ea..7018944 100644 --- a/ezlogcli/src/main.rs +++ b/ezlogcli/src/main.rs @@ -1,13 +1,21 @@ use std::{ fs::OpenOptions, - io::{BufReader, BufWriter, Cursor, Read}, + io::{ + BufReader, + BufWriter, + Cursor, + Read, + }, path::PathBuf, }; use anyhow::Context; use clap::Parser; pub use ezlog::*; -use serde::{Deserialize, Serialize}; +use serde::{ + Deserialize, + Serialize, +}; #[derive(Parser, Debug)] #[clap(author, version, about, long_about = None)] @@ -154,7 +162,10 @@ pub fn main() -> anyhow::Result<()> { mod tests { use std::path::PathBuf; - use assert_cmd::prelude::{OutputAssertExt, OutputOkExt}; + use assert_cmd::prelude::{ + OutputAssertExt, + OutputOkExt, + }; #[test] fn test_help() { @@ -166,7 +177,9 @@ mod tests { .unwrap() .command() .arg("--help") - .unwrap().assert().success(); + .unwrap() + .assert() + .success(); } #[test] diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..25d4176 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,3 @@ +reorder_imports = true +imports_layout = "Vertical" +group_imports = "StdExternalCrate" \ No newline at end of file