Skip to content

Commit 5d5bf91

Browse files
committed
improve logging (sorry kids no emojis)
Signed-off-by: Ikey Doherty <[email protected]>
1 parent 05a3e65 commit 5d5bf91

File tree

4 files changed

+55
-55
lines changed

4 files changed

+55
-55
lines changed

crates/partitioning/src/blkpg.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-License-Identifier: MPL-2.0
44

55
use disks::{BasicDisk, DiskInit};
6-
use log::{debug, error, info, warn};
6+
use log::{debug, error, info};
77
use std::{
88
fs::File,
99
io,
@@ -63,8 +63,8 @@ pub(crate) fn add_partition<F>(fd: F, partition_number: i32, start: i64, length:
6363
where
6464
F: AsRawFd,
6565
{
66-
info!(
67-
"➕ Adding partition {} (start: {}, length: {})",
66+
debug!(
67+
"Initiating partition addition - Number: {}, Start: {}, Length: {}",
6868
partition_number, start, length
6969
);
7070
let mut part = BlkpgPartition {
@@ -85,10 +85,10 @@ where
8585
let res = unsafe { libc::ioctl(fd.as_raw_fd(), BLKPG as _, &mut ioctl) };
8686
if res < 0 {
8787
let err = io::Error::last_os_error();
88-
error!("❌ Failed to add partition: {}", err);
88+
error!("Partition creation failed: {}", err);
8989
return Err(err);
9090
}
91-
info!("Successfully added partition {}", partition_number);
91+
info!("Successfully created partition {}", partition_number);
9292
Ok(())
9393
}
9494

@@ -104,7 +104,7 @@ pub(crate) fn delete_partition<F>(fd: F, partition_number: i32) -> io::Result<()
104104
where
105105
F: AsRawFd,
106106
{
107-
warn!("🗑️ Attempting to delete partition {}", partition_number);
107+
info!("Initiating deletion of partition {}", partition_number);
108108
let mut part = BlkpgPartition {
109109
start: 0,
110110
length: 0,
@@ -123,10 +123,10 @@ where
123123
let res = unsafe { libc::ioctl(fd.as_raw_fd(), BLKPG as _, &mut ioctl) };
124124
if res < 0 {
125125
let err = io::Error::last_os_error();
126-
error!("Failed to delete partition {}: {}", partition_number, err);
126+
error!("Failed to delete partition {}: {}", partition_number, err);
127127
return Err(err);
128128
}
129-
info!("Successfully deleted partition {}", partition_number);
129+
info!("Successfully removed partition {}", partition_number);
130130
Ok(())
131131
}
132132

@@ -138,21 +138,17 @@ where
138138
/// # Returns
139139
/// `Result<(), Error>` indicating success or partition operation failure
140140
pub fn sync_gpt_partitions<P: AsRef<Path>>(path: P) -> Result<(), Error> {
141-
info!("🔄 Syncing GPT partitions for {:?}", path.as_ref());
141+
info!("Initiating GPT partition synchronization for {:?}", path.as_ref());
142142
let file = File::open(&path)?;
143143

144144
// Read GPT table
145-
debug!("📖 Reading GPT table...");
145+
debug!("Reading GPT partition table");
146146
let gpt = gpt::GptConfig::new().writable(false).open(&path)?;
147147
let partitions = gpt.partitions();
148148
let block_size = 512;
149-
info!(
150-
"📊 Found {} partitions with block size {}",
151-
partitions.len(),
152-
block_size
153-
);
149+
info!("Located {} partitions (block size: {})", partitions.len(), block_size);
154150

155-
warn!("🗑️ Deleting existing partitions...");
151+
debug!("Beginning partition cleanup process");
156152

157153
// Find the disk for enumeration purposes
158154
let base_name = path
@@ -169,7 +165,7 @@ pub fn sync_gpt_partitions<P: AsRef<Path>>(path: P) -> Result<(), Error> {
169165
}
170166

171167
// Add partitions from GPT
172-
info!("➕ Adding new partitions from GPT...");
168+
debug!("Beginning partition creation from GPT table");
173169
for (i, partition) in partitions.iter() {
174170
add_partition(
175171
file.as_fd(),
@@ -179,6 +175,6 @@ pub fn sync_gpt_partitions<P: AsRef<Path>>(path: P) -> Result<(), Error> {
179175
)?;
180176
}
181177

182-
info!("GPT partition sync completed successfully");
178+
info!("GPT partition synchronization completed successfully");
183179
Ok(())
184180
}

crates/partitioning/src/loopback.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
};
99

1010
use linux_raw_sys::loop_device::{LOOP_CLR_FD, LOOP_CTL_GET_FREE, LOOP_SET_FD, LOOP_SET_STATUS64};
11-
use log::{debug, error, info, warn};
11+
use log::{debug, error, info};
1212
use nix::libc;
1313

1414
/// Represents a loop device that can be used to mount files as block devices
@@ -28,21 +28,21 @@ impl LoopDevice {
2828
pub fn create() -> io::Result<Self> {
2929
use std::fs::OpenOptions;
3030

31-
debug!("🔄 Opening loop control device");
31+
debug!("Opening loop control device");
3232
let ctrl = OpenOptions::new().read(true).write(true).open("/dev/loop-control")?;
3333

3434
// Get next free loop device number
3535
let devno = unsafe { libc::ioctl(ctrl.as_raw_fd(), LOOP_CTL_GET_FREE as _) };
3636
if devno < 0 {
37-
error!("Failed to get free loop device number");
37+
error!("Failed to acquire free loop device number");
3838
return Err(io::Error::last_os_error());
3939
}
4040

4141
let path = format!("/dev/loop{}", devno);
42-
info!("🔧 Creating new loop device at {}", path);
42+
debug!("Creating new loop device at {}", path);
4343
let fd = OpenOptions::new().read(true).write(true).open(&path)?.into();
4444

45-
info!("Successfully created loop device {}", path);
45+
info!("Successfully initialized loop device {}", path);
4646
Ok(LoopDevice { fd, path })
4747
}
4848

@@ -55,27 +55,27 @@ impl LoopDevice {
5555
/// # Returns
5656
/// `io::Result<()>` indicating success or failure
5757
pub fn attach(&self, backing_file: &str) -> io::Result<()> {
58-
debug!("📎 Attaching backing file {} to {}", backing_file, self.path);
58+
debug!("Attempting to attach backing file {} to {}", backing_file, self.path);
5959
let f = fs::OpenOptions::new().read(true).write(true).open(backing_file)?;
6060

6161
let file_fd = f.as_raw_fd();
6262
let our_fd = self.fd.as_raw_fd();
6363
let res = unsafe { libc::ioctl(our_fd, LOOP_SET_FD as _, file_fd) };
6464

6565
if res < 0 {
66-
error!("Failed to attach backing file {}", backing_file);
66+
error!("Failed to attach backing file {} - OS error", backing_file);
6767
return Err(io::Error::last_os_error());
6868
}
6969

7070
// Force loop device to immediately update by setting empty status
7171
let info: linux_raw_sys::loop_device::loop_info64 = unsafe { std::mem::zeroed() };
7272
let res = unsafe { libc::ioctl(our_fd, LOOP_SET_STATUS64 as _, &info) };
7373
if res < 0 {
74-
warn!("⚠️ Failed to update loop device status");
74+
error!("Failed to update loop device status - device may be in inconsistent state");
7575
return Err(io::Error::last_os_error());
7676
}
7777

78-
info!("Successfully attached backing file {}", backing_file);
78+
info!("Successfully attached backing file {} to loop device", backing_file);
7979
Ok(())
8080
}
8181

@@ -84,14 +84,14 @@ impl LoopDevice {
8484
/// # Returns
8585
/// `io::Result<()>` indicating success or failure
8686
pub fn detach(&self) -> io::Result<()> {
87-
debug!("🔓 Detaching backing file from {}", self.path);
87+
debug!("Initiating detachment of backing file from {}", self.path);
8888
let res = unsafe { libc::ioctl(self.fd.as_raw_fd(), LOOP_CLR_FD as _, 0) };
8989
if res < 0 {
90-
error!("Failed to detach backing file from {}", self.path);
90+
error!("Failed to detach backing file from {} - OS error", self.path);
9191
return Err(io::Error::last_os_error());
9292
}
9393

94-
info!("Successfully detached backing file from {}", self.path);
94+
info!("Successfully detached backing file from loop device {}", self.path);
9595
Ok(())
9696
}
9797
}

crates/partitioning/src/sparsefile.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// SPDX-License-Identifier: MPL-2.0
44

5-
use log::info;
5+
use log::{debug, info};
66
use std::{fs, io, path::Path};
77

88
/// Creates a sparse file at the specified path with the given size.
@@ -17,17 +17,21 @@ pub fn create<P>(path: P, size: u64) -> io::Result<()>
1717
where
1818
P: AsRef<Path>,
1919
{
20-
info!("🗂️ Creating sparse file at {:?}", path.as_ref());
20+
debug!("Creating sparse file at {:?}", path.as_ref());
2121

2222
let file = fs::OpenOptions::new()
2323
.write(true)
2424
.create(true)
2525
.truncate(true)
2626
.open(&path)?;
2727

28-
info!("📝 Setting file size to {} bytes", size);
28+
debug!("Setting file size to {} bytes", size);
2929
file.set_len(size)?;
3030

31-
info!("✅ Successfully created sparse file");
31+
info!(
32+
"Successfully created sparse file of {} bytes at {:?}",
33+
size,
34+
path.as_ref()
35+
);
3236
Ok(())
3337
}

disk-test/src/main.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ fn create_protective_mbr<P>(disk_size: u64, path: P) -> Result<(), Box<dyn std::
2727
where
2828
P: AsRef<Path>,
2929
{
30-
debug!("🛡️ Creating protective MBR for disk of size {} bytes", disk_size);
30+
info!("Creating protective MBR for disk of size {} bytes", disk_size);
3131
let mut gpt_device = fs::File::options().write(true).open(&path)?;
3232
let lb_size = (disk_size / 512) as u32;
3333
let lb_size = lb_size.saturating_sub(1); // subtract 1 for the MBR
3434
let mbr = ProtectiveMBR::with_lb_size(lb_size);
3535
mbr.overwrite_lba0(&mut gpt_device)?;
36-
info!("Successfully created protective MBR at {:?}", path.as_ref());
36+
info!("Successfully created protective MBR at {:?}", path.as_ref());
3737
Ok(())
3838
}
3939

@@ -54,7 +54,7 @@ fn create_default_partition_scheme<P>(path: P) -> Result<(), Box<dyn std::error:
5454
where
5555
P: AsRef<Path>,
5656
{
57-
info!("💽 Creating default GPT partition scheme on {:?}", path.as_ref());
57+
info!("Creating default GPT partition scheme on {:?}", path.as_ref());
5858

5959
// Configure and create GPT disk
6060
let gpt_config = GptConfig::new()
@@ -63,24 +63,24 @@ where
6363

6464
let mut gpt_disk = gpt_config.create(&path)?;
6565

66-
debug!("📝 Creating EFI System Partition (256MB)");
66+
info!("Creating EFI System Partition (256MB)");
6767
gpt_disk.add_partition("", 256 * 1024 * 1024, partition_types::EFI, 0, None)?;
6868

69-
debug!("📝 Creating Boot Partition (2GB)");
69+
info!("Creating Boot Partition (2GB)");
7070
gpt_disk.add_partition("", 2 * 1024 * 1024 * 1024, partition_types::FREEDESK_BOOT, 0, None)?;
7171

72-
debug!("📝 Creating Swap Partition (4GB)");
72+
info!("Creating Swap Partition (4GB)");
7373
gpt_disk.add_partition("", 4 * 1024 * 1024 * 1024, partition_types::LINUX_SWAP, 0, None)?;
7474

7575
// Use remaining space for root partition
7676
let sectors = gpt_disk.find_free_sectors();
77-
debug!("📊 Available sectors: {sectors:?}");
77+
debug!("Available sectors: {sectors:?}");
7878
let (_, length) = sectors.iter().find(|(_, l)| *l > 0).unwrap();
79-
debug!("📝 Creating Root Partition ({}MB)", (length * 512) / (1024 * 1024));
79+
info!("Creating Root Partition ({}MB)", (length * 512) / (1024 * 1024));
8080
gpt_disk.add_partition("", *length * 512, partition_types::LINUX_FS, 0, None)?;
8181
let _ = gpt_disk.write()?;
8282

83-
info!("Successfully created partition scheme");
83+
info!("Successfully created partition scheme");
8484
Ok(())
8585
}
8686

@@ -90,31 +90,31 @@ where
9090
/// - Partitioning with GPT
9191
/// - Enumerating block devices
9292
fn main() -> Result<(), Box<dyn std::error::Error>> {
93-
pretty_env_logger::formatted_builder()
94-
.filter_level(log::LevelFilter::Trace)
93+
pretty_env_logger::formatted_timed_builder()
94+
.filter_level(log::LevelFilter::Info)
9595
.init();
96-
info!("🚀 Starting disk partitioning demo");
96+
info!("Starting disk partitioning demo");
9797

9898
// Create 35GB sparse image file and attach to loopback device
9999
let image_size = 35 * 1024 * 1024 * 1024;
100-
info!("📁 Creating {}GB sparse image file", image_size / (1024 * 1024 * 1024));
100+
info!("Creating {}GB sparse image file", image_size / (1024 * 1024 * 1024));
101101
sparsefile::create("hello.world", image_size)?;
102102

103-
debug!("🔄 Setting up loopback device");
103+
info!("Setting up loopback device");
104104
let device = loopback::LoopDevice::create()?;
105105
device.attach("hello.world")?;
106-
info!("💫 Loop device created at: {}", &device.path);
106+
info!("Loop device created at: {}", &device.path);
107107

108108
// Initialize disk with protective MBR and partition scheme
109109
create_protective_mbr(image_size, "hello.world")?;
110110
create_default_partition_scheme("hello.world")?;
111111

112112
// Notify kernel of partition table changes
113-
debug!("🔄 Syncing partition table changes");
113+
debug!("Syncing partition table changes");
114114
blkpg::sync_gpt_partitions(&device.path)?;
115115

116116
// Get list of all loopback devices
117-
info!("🔍 Discovering block devices");
117+
debug!("Discovering block devices");
118118
let loop_devices = BlockDevice::discover()?
119119
.into_iter()
120120
.filter_map(|device| {
@@ -127,12 +127,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
127127
.collect::<Vec<_>>();
128128

129129
// Display information about discovered devices
130-
info!("📋 Device information:");
130+
info!("Device information:");
131131
for loop_device in loop_devices {
132132
if let Some(file) = loop_device.file_path() {
133133
if let Some(disk) = loop_device.disk() {
134134
info!(
135-
"💾 Loopback device: {} (backing file: {})",
135+
"Loopback device: {} (backing file: {})",
136136
loop_device.name(),
137137
file.display()
138138
);
@@ -145,10 +145,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
145145
}
146146

147147
// Clean up resources
148-
debug!("🧹 Cleaning up resources");
148+
info!("Cleaning up resources");
149149
device.detach()?;
150150
//fs::remove_file("hello.world")?;
151151

152-
info!("Demo completed successfully");
152+
info!("Demo completed successfully");
153153
Ok(())
154154
}

0 commit comments

Comments
 (0)