Skip to content

Commit

Permalink
move recorder, logger to other mods
Browse files Browse the repository at this point in the history
  • Loading branch information
s1rius committed Dec 3, 2022
1 parent f0ac214 commit c7d14ec
Show file tree
Hide file tree
Showing 8 changed files with 834 additions and 796 deletions.
8 changes: 5 additions & 3 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ use std::io::{BufReader, BufWriter, Cursor, Read};
use std::thread;
use std::time::Duration;

use ezlog::EZLogger;
use ezlog::Level;
use ezlog::{
create_log, CipherKind, CompressKind, EZLogCallback, EZLogConfig, EZLogConfigBuilder, EZLogger,
EZRecord, EventPrinter, Header,
create_log, CipherKind, CompressKind, EZLogCallback, EZLogConfig, EZLogConfigBuilder, EZRecord,
EventPrinter, Header,
};
use log::{debug, error, info, trace, warn, LevelFilter};
use log::{Metadata, Record};
Expand Down Expand Up @@ -50,7 +52,7 @@ fn get_config() -> EZLogConfig {
let key = b"an example very very secret key.";
let nonce = b"unique nonce";
EZLogConfigBuilder::new()
.level(ezlog::Level::Trace)
.level(Level::Trace)
.dir_path(
dirs::download_dir()
.unwrap()
Expand Down
4 changes: 2 additions & 2 deletions ezlog-core/src/appender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};
use time::OffsetDateTime;

use crate::*;
use crate::{logger::Header, *};

pub trait AppenderInner: Write {
fn is_oversize(&self, buf_size: usize) -> bool;
Expand Down Expand Up @@ -361,7 +361,7 @@ impl Write for NopInner {
#[cfg(test)]
mod tests {

use std::fs::{File, OpenOptions};
use std::fs::{self, File, OpenOptions};
use std::io::{BufReader, Seek, SeekFrom};

use crate::config::EZLogConfigBuilder;
Expand Down
148 changes: 144 additions & 4 deletions ezlog-core/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
cmp,
cmp, fmt,
fs::{self, File, OpenOptions},
hash::{Hash, Hasher},
path::{Path, PathBuf},
Expand All @@ -11,9 +11,9 @@ use time::{format_description, Date, Duration, OffsetDateTime};
#[allow(unused_imports)]
use crate::EZLogger;
use crate::{
appender::rename_current_file, errors::LogError, event, CipherKind, CompressKind,
CompressLevel, Header, Level, Version, DEFAULT_LOG_FILE_SUFFIX, DEFAULT_LOG_NAME,
DEFAULT_MAX_LOG_SIZE, MIN_LOG_SIZE,
appender::rename_current_file, errors::LogError, event, logger::Header, CipherKind,
CompressKind, CompressLevel, Version, DEFAULT_LOG_FILE_SUFFIX, DEFAULT_LOG_NAME,
DEFAULT_MAX_LOG_SIZE, LOG_LEVEL_NAMES, MIN_LOG_SIZE,
};

pub const DATE_FORMAT: &str = "[year]_[month]_[day]";
Expand Down Expand Up @@ -344,6 +344,146 @@ pub(crate) fn parse_date_from_str(date_str: &str, case: &str) -> crate::Result<D
Ok(date)
}

/// Log level, used to filter log records
#[repr(usize)]
#[derive(Copy, Eq, Debug)]
pub enum Level {
/// The "error" level.
///
/// Designates very serious errors.
// This way these line up with the discriminants for LevelFilter below
// This works because Rust treats field-less enums the same way as C does:
// https://doc.rust-lang.org/reference/items/enumerations.html#custom-discriminant-values-for-field-less-enumerations
Error = 1,
/// The "warn" level.
///
/// Designates hazardous situations.
Warn,
/// The "info" level.
///
/// Designates useful information.
Info,
/// The "debug" level.
///
/// Designates lower priority information.
Debug,
/// The "trace" level.
///
/// Designates very low priority, often extremely verbose, information.
Trace,
}

impl Level {
pub fn from_usize(u: usize) -> Option<Level> {
match u {
1 => Some(Level::Error),
2 => Some(Level::Warn),
3 => Some(Level::Info),
4 => Some(Level::Debug),
5 => Some(Level::Trace),
_ => None,
}
}

/// Returns the most verbose logging level.
#[inline]
pub fn max() -> Level {
Level::Trace
}

/// Returns the string representation of the `Level`.
///
/// This returns the same string as the `fmt::Display` implementation.
pub fn as_str(&self) -> &'static str {
LOG_LEVEL_NAMES[*self as usize]
}

/// Iterate through all supported logging levels.
///
/// The order of iteration is from more severe to less severe log messages.
///
/// # Examples
///
/// ```
/// use log::Level;
///
/// let mut levels = Level::iter();
///
/// assert_eq!(Some(Level::Error), levels.next());
/// assert_eq!(Some(Level::Trace), levels.last());
/// ```
pub fn iter() -> impl Iterator<Item = Self> {
(1..6).map(|i| Self::from_usize(i).unwrap_or(Level::Error))
}
}

impl Clone for Level {
#[inline]
fn clone(&self) -> Level {
*self
}
}

impl PartialEq for Level {
#[inline]
fn eq(&self, other: &Level) -> bool {
*self as usize == *other as usize
}
}

impl PartialOrd for Level {
#[inline]
fn partial_cmp(&self, other: &Level) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}

#[inline]
fn lt(&self, other: &Level) -> bool {
(*self as usize) < *other as usize
}

#[inline]
fn le(&self, other: &Level) -> bool {
*self as usize <= *other as usize
}

#[inline]
fn gt(&self, other: &Level) -> bool {
*self as usize > *other as usize
}

#[inline]
fn ge(&self, other: &Level) -> bool {
*self as usize >= *other as usize
}
}

impl Ord for Level {
#[inline]
fn cmp(&self, other: &Level) -> cmp::Ordering {
(*self as usize).cmp(&(*other as usize))
}
}

#[cfg(feature = "log")]
impl From<log::Level> for Level {
fn from(log_level: log::Level) -> Self {
match log_level {
log::Level::Error => Level::Error,
log::Level::Warn => Level::Warn,
log::Level::Info => Level::Info,
log::Level::Debug => Level::Debug,
log::Level::Trace => Level::Trace,
}
}
}

impl fmt::Display for Level {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.pad(self.as_str())
}
}

#[cfg(test)]
mod tests {

Expand Down
2 changes: 2 additions & 0 deletions ezlog-core/src/ffi_c.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use libc::c_void;

use crate::config::Level;
use crate::events::EventPrinter;
use crate::recorder::EZRecordBuilder;
use crate::*;
use core::slice;
use libc::c_char;
Expand Down
2 changes: 1 addition & 1 deletion ezlog-core/src/ffi_java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use jni::{
signature::Primitive,
strings::JNIString,
sys::{jboolean, jbyteArray, jint, jobject, jobjectArray, jvalue, JNI_VERSION_1_6},
JNIEnv, JavaVM, AttachGuard,
AttachGuard, JNIEnv, JavaVM,
};
use libc::c_void;
use once_cell::sync::OnceCell;
Expand Down
Loading

0 comments on commit c7d14ec

Please sign in to comment.