Skip to content

Commit

Permalink
decode string to record array
Browse files Browse the repository at this point in the history
  • Loading branch information
s1rius committed Nov 21, 2023
1 parent 8cf2d75 commit 0d37b3f
Show file tree
Hide file tree
Showing 10 changed files with 410 additions and 141 deletions.
23 changes: 3 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
[package]
name = "ezlog_space"
version = "0.1.0"
edition = "2021"

[workspace]
members = ["ezlogcli", "ezlog-core", "examples/android_preview", "examples/nest_log"]
members = ["ezlogcli", "ezlog-core", "examples/android_preview", "examples/nest_log", "examples/hello_world"]
resolver = "2"

# https://github.com/johnthagen/min-sized-rust
[profile.release]
opt-level = "z" # Optimize for size.
lto = true # Enable Link Time Optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
# panic = 'abort' # Abort on panic
# strip = true # Strip symbols from binary*

[dependencies]
ezlog = {path = "ezlog-core", features = ["decode"]}
log = "0.4.17"
time = { version = "0.3", default-features = false, features = ["formatting"] }

[dev-dependencies]
dirs = "5.0"
rand = "0.8"

[[example]]
name = "hello_world"
path = "examples/hello_world.rs"
crate-type = ["bin"]
# strip = true # Strip symbols from binary*
14 changes: 14 additions & 0 deletions examples/hello_world/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ezlog = { path = "../../ezlog-core", features = ["log", "decode"]}
log = "0.4"
env_logger = "0"
dirs = "5.0"
time = { version = "0.3", default-features = false, features = ["macros"] }
rand = "0.8"
78 changes: 46 additions & 32 deletions examples/hello_world.rs → examples/hello_world/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use std::io::{
BufWriter,
Cursor,
Read,
Write,
};
use std::thread;
use std::time::Duration;

use ezlog::EZMsg;
use ezlog::Level;
use ezlog::{
create_log,
Expand All @@ -19,16 +21,13 @@ use ezlog::{
EventPrinter,
Header,
};
use ezlog::{
EZLogger,
EZMsg,
};
use log::{
debug,
error,
info,
trace,
warn,
LevelFilter,
};
use rand::Rng;
use time::OffsetDateTime;
Expand All @@ -37,10 +36,10 @@ static EVENT_LISTENER: EventPrinter = EventPrinter;

pub fn main() {
println!("start");
ezlog::InitBuilder::new()
let ezlog = ezlog::InitBuilder::new()
.with_layer_fn(|msg| {
if let EZMsg::Record(recode) = msg {
println!("{}", ezlog::format(&recode));
println!("{}", ezlog::format(recode));
}
})
.with_event_listener(&EVENT_LISTENER)
Expand All @@ -51,26 +50,45 @@ pub fn main() {

create_log(log_config);

log::set_boxed_logger(Box::new(ezlog))
.map(|()| log::set_max_level(LevelFilter::Trace))
.unwrap();

trace!("1. create default log");
debug!("2. debug ez log");
info!("3. now have a log");
warn!("4. test log to file");
error!("5. log complete");

for i in 0..100 {
for i in 0..10 {
trace!("{}{}", i, random_string(300));
}

ezlog::flush(ezlog::DEFAULT_LOG_NAME);

println!("end");

thread::sleep(Duration::from_secs(1));
read_log_file_rewrite();

ezlog::set_boxed_callback(Box::new(SimpleCallback));
ezlog::request_log_files_for_date(
ezlog::DEFAULT_LOG_NAME,
OffsetDateTime::now_utc(),
OffsetDateTime::now_utc(),
);
println!("end");

thread::sleep(Duration::from_secs(1));
read_log_file_rewrite();
}

struct SimpleCallback;

impl EZLogCallback for SimpleCallback {
fn on_fetch_success(&self, name: &str, date: &str, logs: &[&str]) {
println!("{} {} {}", name, date, logs.join(" "));
}
fn on_fetch_fail(&self, name: &str, date: &str, err: &str) {
println!("{} {} {}", name, date, err);
}
}

fn get_config() -> EZLogConfig {
Expand Down Expand Up @@ -122,30 +140,26 @@ fn read_log_file_rewrite() {

let mut writer = BufWriter::new(plaintext_log);

let mut compression = ezlog::create_compress(&log_config);
let mut cryptor = ezlog::create_cryptor(&log_config).unwrap();
let compression = ezlog::create_compress(&log_config);
let cryptor = ezlog::create_cryptor(&log_config).unwrap();
let header = Header::decode(&mut cursor).unwrap();
ezlog::decode::decode_body_and_write(
&mut cursor,
&mut writer,
&header.version(),
&mut compression,
&mut cryptor,
&header,
)
.unwrap();
}

struct SimpleCallback;
let my_closure = move |data: &Vec<u8>, flag: bool| {
writer.write_all(data).unwrap();
writer.write_all(b"\n").unwrap();
if flag {
writer.flush().unwrap();
}
};

impl EZLogCallback for SimpleCallback {
fn on_fetch_success(&self, name: &str, date: &str, logs: &[&str]) {
println!("fetch success {} {} {}", name, date, logs.join(" "));
}

fn on_fetch_fail(&self, name: &str, date: &str, err: &str) {
println!("fetch fail {} {} {}", name, date, err);
}
ezlog::decode::decode_with_fn(
&mut cursor,
header.version(),
&compression,
&cryptor,
&header,
my_closure,
);
}

const S: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.:;!@#$%^&*()_+-";
Expand All @@ -158,5 +172,5 @@ fn random_string(length: u32) -> String {
let c = chars.nth(index).unwrap();
owned_string.push(c);
}
return owned_string;
owned_string
}
6 changes: 3 additions & 3 deletions examples/nest_log/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "simple_logger"
name = "nest_log"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ezlog = { path = "../../ezlog-core", features = ["log"]}
log = "0.4.0"
env_logger = "0.10.0"
log = "0.4"
env_logger = "0"
dirs = "5.0"
3 changes: 2 additions & 1 deletion ezlog-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ crate-type = ["lib", "cdylib", "staticlib"]
[features]
default = []
log = ["dep:log"]
decode = ["dep:aes-gcm"]
decode = ["dep:aes-gcm", "dep:regex", "dep:log"]
android_logger = ["log","dep:android_logger"]

[dependencies]
Expand All @@ -35,6 +35,7 @@ thiserror = "1"
integer-encoding = "3.0"
bitflags = "2.0.1"
android_logger = { version = "0.13", optional = true }
regex = { version = "1", optional = true}

[target.'cfg(target_os = "android")'.dependencies]
jni = "0.21.0"
Expand Down
12 changes: 12 additions & 0 deletions ezlog-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,18 @@ impl Level {
}
}

impl std::str::FromStr for Level {
fn from_str(s: &str) -> Result<Self, Self::Err> {
let a = LOG_LEVEL_NAMES
.iter()
.position(|&name| name == s)
.and_then(Self::from_usize);
a.ok_or(LogError::Parse("unknown level".to_string()))
}

type Err = LogError;
}

impl Clone for Level {
#[inline]
fn clone(&self) -> Level {
Expand Down
Loading

0 comments on commit 0d37b3f

Please sign in to comment.