Skip to content

Commit

Permalink
initial implementation of the logging feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ph0llux committed Nov 15, 2023
1 parent d5496ac commit 943d2b4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ ed25519-dalek = { version = "2.0", features = [ "rand_core", "digest" ] }
redb = "1.0.5"
# optional deps for features
serde = { version = "1.0", features = ["derive"], optional = true }
log = { version = "0.4.6", optional = true }
hex = { version = "0.4.3", optional = true }

[features]
default = []
serde = ["dep:serde", "dep:hex"]
log = ["dep:log", "dep:hex"]

[dev-dependencies]
hex = "0.4.3"
Expand Down
9 changes: 9 additions & 0 deletions src/lib/file/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ use crate::{
DEFAULT_FOOTER_VERSION_FILE_FOOTER,
};

#[cfg(feature = "log")]
use crate::{
hashes_to_log,
};

// - external
use digest::DynDigest;
use time::{OffsetDateTime};
Expand Down Expand Up @@ -267,6 +272,10 @@ impl FileEncoder {
hash_value.set_hash(hash.to_vec());
hash_values.push(hash_value);
}

#[cfg(feature = "log")]
hashes_to_log(self.object_header.object_number, Some(self.file_header.file_number), &hash_values);

let hash_header = HashHeader::new(DEFAULT_HEADER_VERSION_HASH_HEADER, hash_values);
let footer = FileFooter::new(
DEFAULT_FOOTER_VERSION_FILE_FOOTER,
Expand Down
13 changes: 13 additions & 0 deletions src/lib/hashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use serde::{
Deserialize,
Serialize,
};
#[cfg(feature = "log")]
use log::{info};

/// Defines all hashing algorithms, which are implemented in zff.
#[repr(u8)]
Expand Down Expand Up @@ -78,4 +80,15 @@ impl Hash {
pub fn default_hashtype() -> HashType {
HashType::Blake3
}
}

#[cfg(feature = "log")]
pub(crate) fn hashes_to_log(object_no: u64, file_no: Option<u64>, values: &Vec<crate::header::HashValue>) {
for value in values {
if let Some(file_no) = file_no {
info!("{} hash for object {object_no} / file {file_no} finalized: {}", value.hash_type(), hex::encode(value.hash()));
} else {
info!("{} hash for object {object_no} finalized: {}", value.hash_type(), hex::encode(value.hash()));
}
}
}
14 changes: 12 additions & 2 deletions src/lib/object/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ use crate::{
DEFAULT_FOOTER_VERSION_OBJECT_FOOTER_LOGICAL,
};

#[cfg(feature = "log")]
use crate::{
hashes_to_log,
};

use crate::{
header::{
ObjectHeader,
Expand Down Expand Up @@ -296,6 +301,10 @@ impl<R: Read> PhysicalObjectEncoder<R> {
};
hash_values.push(hash_value);
}

#[cfg(feature = "log")]
hashes_to_log(self.obj_header.object_number, None, &hash_values);

let hash_header = HashHeader::new(DEFAULT_HEADER_VERSION_HASH_HEADER, hash_values);
let footer = ObjectFooterPhysical::new(
DEFAULT_FOOTER_VERSION_OBJECT_FOOTER_PHYSICAL,
Expand All @@ -306,6 +315,7 @@ impl<R: Read> PhysicalObjectEncoder<R> {
self.initial_chunk_number,
self.current_chunk_number - self.initial_chunk_number,
hash_header);

if let Some(encryption_key) = &self.encryption_key {
let encryption_information = EncryptionInformation {
encryption_key: encryption_key.to_vec(),
Expand Down Expand Up @@ -501,7 +511,6 @@ impl LogicalObjectEncoder {
return Ok(file_encoder.get_encoded_header());
}

//todo: find a more efficient way than copy the offset.
let mut current_offset = current_offset;

let mut data = Vec::new();
Expand Down Expand Up @@ -582,4 +591,5 @@ impl LogicalObjectEncoder {
self.encryption_key.clone()
}

}
}

0 comments on commit 943d2b4

Please sign in to comment.