Skip to content

Commit 4605e4e

Browse files
committed
feat: Add flag for verbose logging, supress most of messages as debug level
Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
1 parent feacc95 commit 4605e4e

6 files changed

Lines changed: 233 additions & 158 deletions

File tree

src/azure.rs

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl AzureDriver {
1010
}
1111
}
1212

13-
use crate::{get_config_content, ReceivedFile};
13+
use crate::{debug_log, get_config_content, ReceivedFile};
1414
use axum::http::{HeaderName, HeaderValue};
1515
use azure_storage::StorageCredentials;
1616
use azure_storage_blobs::container::operations::BlobItem;
@@ -23,15 +23,6 @@ use serde::Deserialize;
2323
use std::fs::read_to_string;
2424
use std::fs::File;
2525
use std::io::Read;
26-
use std::env;
27-
28-
macro_rules! debug_log {
29-
($($arg:tt)*) => {
30-
if env::var("STORAGE_DEBUG").is_ok() {
31-
println!($($arg)*);
32-
}
33-
};
34-
}
3526
use std::io::Write;
3627
use std::sync::Arc;
3728
use tempfile::Builder;
@@ -81,12 +72,17 @@ fn get_azure_credentials(name: &str) -> AzureConfig {
8172
fn calculate_checksum(filename: &String, data: &[u8]) {
8273
let hash = sha2_512::default().update(data).finalize();
8374
let digest = hash.digest();
84-
println!("File: {} Checksum: {}", filename, digest.to_hex_lowercase());
75+
debug_log!("File: {} Checksum: {}", filename, digest.to_hex_lowercase());
8576
}
8677

8778
/// Write file to Azure blob storage
8879
/// TBD: Rework, do not keep whole file as Vec<u8> in memory!!!
89-
async fn write_file_to_blob(filename: String, data: Vec<u8>, cont_type: String, owner_email: Option<String>) -> &'static str {
80+
async fn write_file_to_blob(
81+
filename: String,
82+
data: Vec<u8>,
83+
cont_type: String,
84+
owner_email: Option<String>,
85+
) -> &'static str {
9086
let azure_cfg = Arc::new(get_azure_credentials("azure"));
9187

9288
let storage_account = azure_cfg.account.as_str();
@@ -130,7 +126,7 @@ async fn write_file_to_blob(filename: String, data: Vec<u8>, cont_type: String,
130126
match blob_client.put_block(block_id, buffer).await {
131127
Ok(_) => {
132128
total_bytes_uploaded += bytes_read;
133-
println!("Uploaded {} bytes", total_bytes_uploaded);
129+
debug_log!("Uploaded {} bytes", total_bytes_uploaded);
134130
}
135131
Err(e) => {
136132
eprintln!("Error uploading block: {:?}", e);
@@ -150,29 +146,34 @@ async fn write_file_to_blob(filename: String, data: Vec<u8>, cont_type: String,
150146
.await
151147
{
152148
Ok(_) => {
153-
println!("Block list uploaded");
149+
debug_log!("Block list uploaded");
154150
let blob_url_res = blob_client.url();
155151
match blob_url_res {
156152
Ok(blob_url) => {
157-
println!("Blob URL: {}", blob_url);
153+
debug_log!("Blob URL: {}", blob_url);
158154
}
159155
Err(e) => {
160156
eprintln!("Error getting blob URL: {:?}", e);
161157
}
162158
}
163-
159+
164160
// Set owner tag if email is provided
165161
if let Some(email) = owner_email {
166162
let mut tags = Tags::new();
167163
let sanitized = sanitize_tag_component(&email);
168164
if sanitized != email {
169165
debug_log!(
170166
"Sanitized owner tag value from '{}' to '{}'",
171-
email, sanitized
167+
email,
168+
sanitized
172169
);
173170
}
174171
// Ensure non-empty value
175-
let final_value = if sanitized.is_empty() { "_".to_string() } else { sanitized };
172+
let final_value = if sanitized.is_empty() {
173+
"_".to_string()
174+
} else {
175+
sanitized
176+
};
176177
tags.insert("owner".to_string(), final_value);
177178
match blob_client.set_tags(tags).await {
178179
Ok(_) => {
@@ -275,9 +276,10 @@ async fn get_file_from_blob(filename: String) -> ReceivedFile {
275276
break;
276277
}
277278
std::thread::sleep(std::time::Duration::from_millis(1000));
278-
println!(
279+
debug_log!(
279280
"Waiting for headers file {} to exist: {} seconds",
280-
cache_filename_headers, seconds
281+
cache_filename_headers,
282+
seconds
281283
);
282284
}
283285

@@ -320,7 +322,11 @@ async fn get_file_from_blob(filename: String) -> ReceivedFile {
320322
}
321323
}
322324
}
323-
debug_log!("Downloading blob to cache file {} from {}", cache_filename, blob_url);
325+
debug_log!(
326+
"Downloading blob to cache file {} from {}",
327+
cache_filename,
328+
blob_url
329+
);
324330
let client = Client::new();
325331
let response = client.get(blob_url).send().await;
326332
match response {
@@ -382,17 +388,17 @@ async fn azure_set_filename_tags(
382388
debug_log!("Skipping tag with empty key after sanitization: '{}'", key);
383389
continue;
384390
}
385-
let final_value = if sanitized_value.is_empty() { "_".to_string() } else { sanitized_value };
391+
let final_value = if sanitized_value.is_empty() {
392+
"_".to_string()
393+
} else {
394+
sanitized_value
395+
};
386396
tags.insert(sanitized_key, final_value);
387397
}
388398
let res = blob_client.set_tags(tags).await;
389399
match res {
390-
Ok(_) => {
391-
Ok(String::from("OK"))
392-
}
393-
Err(e) => {
394-
Err(e.to_string())
395-
}
400+
Ok(_) => Ok(String::from("OK")),
401+
Err(e) => Err(e.to_string()),
396402
}
397403
}
398404

@@ -424,7 +430,13 @@ async fn azure_list_files(directory: String) -> Vec<String> {
424430

425431
/// Implement Driver trait for AzureDriver
426432
impl super::Driver for AzureDriver {
427-
fn write_file(&self, filename: String, data: Vec<u8>, cont_type: String, owner_email: Option<String>) -> String {
433+
fn write_file(
434+
&self,
435+
filename: String,
436+
data: Vec<u8>,
437+
cont_type: String,
438+
owner_email: Option<String>,
439+
) -> String {
428440
let filenameret = filename.clone();
429441
/* Call async write_file_to_blob use tokio::task::block_in_place */
430442
tokio::task::block_in_place(|| {

0 commit comments

Comments
 (0)