cu29-unifiedlog 1.2.6

The super quick, linear, unified logging system (text + task data) used by Copper. This crate can be used independently from the Copper project.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;
extern crate core;

#[cfg(feature = "std")]
pub mod memmap;
pub mod noop;

#[cfg(feature = "std")]
mod compat {
    // backward compatibility for the std implementation
    pub use crate::memmap::LogPosition;
    pub use crate::memmap::MmapUnifiedLogger as UnifiedLogger;
    pub use crate::memmap::MmapUnifiedLoggerBuilder as UnifiedLoggerBuilder;
    pub use crate::memmap::MmapUnifiedLoggerRead as UnifiedLoggerRead;
    pub use crate::memmap::MmapUnifiedLoggerWrite as UnifiedLoggerWrite;
    pub use crate::memmap::UnifiedLoggerIOReader;
}

#[cfg(feature = "std")]
pub use compat::*;
pub use noop::{NoopLogger, NoopSectionStorage};

use alloc::string::ToString;
#[cfg(not(feature = "std"))]
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::fmt::{Debug, Display, Formatter, Result as FmtResult};
#[cfg(not(feature = "std"))]
use spin::Mutex;
#[cfg(feature = "std")]
use std::sync::{Arc, Mutex};

use bincode::error::EncodeError;
use bincode::{Decode, Encode};
use cu29_traits::{CuError, CuResult, UnifiedLogType, WriteStream};

/// ID to spot the beginning of a Copper Log
#[allow(dead_code)]
pub const MAIN_MAGIC: [u8; 4] = [0xB4, 0xA5, 0x50, 0xFF]; // BRASS OFF

/// ID to spot a section of Copper Log
pub const SECTION_MAGIC: [u8; 2] = [0xFA, 0x57]; // FAST

/// Version of the unified log **encapsulation only**: file headers, section
/// headers, and the layout used to locate sections in a slab.
///
/// This is **never** a version of the encoded content inside sections. Changes
/// to CopperLists, payload types, keyframes, or their serialization must not bump
/// this value. Decode content with the logreader built for the exact application
/// version that produced it; this header cannot establish content compatibility.
/// The encapsulation remains version 1, unchanged since Copper's original format.
pub const UNIFIED_LOG_FORMAT_VERSION: u8 = 1;

pub const SECTION_HEADER_COMPACT_SIZE: u16 = 512; // Usual minimum size for a disk sector.

/// The main file header of the datalogger.
#[derive(Encode, Decode, Debug)]
pub struct MainHeader {
    pub magic: [u8; 4], // Magic number to identify the file.
    /// Encapsulation version only; see [`UNIFIED_LOG_FORMAT_VERSION`].
    /// Never versions encoded section content or determines decoder compatibility.
    /// Content requires the producing application's matching logreader.
    pub format_version: u8,
    pub first_section_offset: u16, // This is to align with a page at write time.
    pub page_size: u16,
}

impl Display for MainHeader {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        writeln!(
            f,
            "  Magic -> {:2x}{:2x}{:2x}{:2x}",
            self.magic[0], self.magic[1], self.magic[2], self.magic[3]
        )?;
        writeln!(f, "  format_version -> {}", self.format_version)?;
        writeln!(f, "  first_section_offset -> {}", self.first_section_offset)?;
        writeln!(f, "  page_size -> {}", self.page_size)
    }
}

/// Each concurrent sublogger is tracked through a section header.
/// They form a linked list of sections.
/// The entry type is used to identify the type of data in the section.
#[derive(Encode, Decode, Debug)]
pub struct SectionHeader {
    pub magic: [u8; 2],  // Magic number to identify the section.
    pub block_size: u16, // IMPORTANT: we assume this header fits in this block size.
    pub entry_type: UnifiedLogType,
    pub offset_to_next_section: u32, // offset from the first byte of this header to the first byte of the next header (MAGIC to MAGIC).
    pub used: u32,                   // how much of the section is filled.
    pub is_open: bool,               // true while being written, false once closed.
}

impl Display for SectionHeader {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        writeln!(f, "    Magic -> {:2x}{:2x}", self.magic[0], self.magic[1])?;
        writeln!(f, "    type -> {:?}", self.entry_type)?;
        write!(
            f,
            "    use  -> {} / {} (open: {})",
            self.used, self.offset_to_next_section, self.is_open
        )
    }
}

impl Default for SectionHeader {
    fn default() -> Self {
        Self {
            magic: SECTION_MAGIC,
            block_size: 512,
            entry_type: UnifiedLogType::Empty,
            offset_to_next_section: 0,
            used: 0,
            is_open: true,
        }
    }
}

pub enum AllocatedSection<S: SectionStorage> {
    NoMoreSpace,
    Section(SectionHandle<S>),
}

/// A Storage is an append-only structure that can update a header section.
pub trait SectionStorage: Send + Sync {
    /// This rewinds the storage, serialize the header and jumps to the beginning of the user data storage.
    fn initialize<E: Encode>(&mut self, header: &E) -> Result<usize, EncodeError>;
    /// This updates the header leaving the position to the end of the user data storage.
    fn post_update_header<E: Encode>(&mut self, header: &E) -> Result<usize, EncodeError>;
    /// Appends the entry to the user data storage.
    fn append<E: Encode>(&mut self, entry: &E) -> Result<usize, EncodeError>;
    /// Flushes the section to the underlying storage
    fn flush(&mut self) -> CuResult<usize>;
}

/// A SectionHandle is a handle to a section in the datalogger.
/// It allows tracking the lifecycle of the section.
#[derive(Default)]
pub struct SectionHandle<S: SectionStorage> {
    header: SectionHeader, // keep a copy of the header as metadata
    storage: S,
}

impl<S: SectionStorage> SectionHandle<S> {
    pub fn create(header: SectionHeader, mut storage: S) -> CuResult<Self> {
        // Write the first version of the header.
        let _ = storage.initialize(&header).map_err(|e| e.to_string())?;
        Ok(Self { header, storage })
    }

    pub fn mark_closed(&mut self) {
        self.header.is_open = false;
    }
    pub fn append<E: Encode>(&mut self, entry: E) -> Result<usize, EncodeError> {
        self.storage.append(&entry)
    }

    pub fn get_storage(&self) -> &S {
        &self.storage
    }

    pub fn get_storage_mut(&mut self) -> &mut S {
        &mut self.storage
    }

    pub fn post_update_header(&mut self) -> Result<usize, EncodeError> {
        self.storage.post_update_header(&self.header)
    }
}

/// Basic statistics for the unified logger.
/// Note: the total_allocated_space might grow for the std implementation
pub struct UnifiedLogStatus {
    pub total_used_space: usize,
    pub total_allocated_space: usize,
}

/// Payload stored in the end-of-log section to signal whether the log was cleanly closed.
#[derive(Encode, Decode, Debug, Clone)]
pub struct EndOfLogMarker {
    pub temporary: bool,
}

/// The writing interface to the unified logger.
/// Writing is "almost" linear as various streams can allocate sections and track them until
/// they drop them.
pub trait UnifiedLogWrite<S: SectionStorage>: Send + Sync {
    /// A section is a contiguous chunk of memory that can be used to write data.
    /// It can store various types of data as specified by the entry_type.
    /// The requested_section_size is the size of the section to allocate.
    /// It returns a handle to the section that can be used to write data until
    /// it is flushed with flush_section, it is then considered unmutable.
    fn add_section(
        &mut self,
        entry_type: UnifiedLogType,
        requested_section_size: usize,
    ) -> CuResult<SectionHandle<S>>;

    /// Flush the given section to the underlying storage.
    fn flush_section(&mut self, section: &mut SectionHandle<S>);

    /// Returns the current status of the unified logger.
    fn status(&self) -> UnifiedLogStatus;
}

/// Read back a unified log linearly.
pub trait UnifiedLogRead {
    /// Read through the unified logger until it reaches the UnifiedLogType given in datalogtype.
    /// It will return the byte array of the section if found.
    fn read_next_section_type(&mut self, datalogtype: UnifiedLogType) -> CuResult<Option<Vec<u8>>>;

    /// Read through the next section entry regardless of its type.
    /// It will return the header and the byte array of the section.
    /// Note the last Entry should be of UnifiedLogType::LastEntry if the log is not corrupted.
    fn raw_read_section(&mut self) -> CuResult<(SectionHeader, Vec<u8>)>;
}

/// Create a new stream to write to the unifiedlogger.
pub fn stream_write<E: Encode, S: SectionStorage>(
    logger: Arc<Mutex<impl UnifiedLogWrite<S>>>,
    entry_type: UnifiedLogType,
    minimum_allocation_amount: usize,
) -> CuResult<impl WriteStream<E>> {
    LogStream::new(entry_type, logger, minimum_allocation_amount)
}

/// A wrapper around the unifiedlogger that implements the Write trait.
pub struct LogStream<S: SectionStorage, L: UnifiedLogWrite<S>> {
    entry_type: UnifiedLogType,
    parent_logger: Arc<Mutex<L>>,
    current_section: SectionHandle<S>,
    minimum_allocation_amount: usize,
    last_log_bytes: usize,
}

// Storage has already appended: detect invalid accounting without publishing
// a truncated/wrapped header. This cannot roll back a faulty storage writer.
fn record_bytes(header: &mut SectionHeader, bytes: usize) -> CuResult<()> {
    let bytes = u32::try_from(bytes)
        .map_err(|_| CuError::from("Encoded entry size exceeds section counter"))?;
    header.used = header
        .used
        .checked_add(bytes)
        .ok_or_else(|| CuError::from("Encoded section size exceeds section counter"))?;
    Ok(())
}

impl<S: SectionStorage, L: UnifiedLogWrite<S>> LogStream<S, L> {
    /// Creates a concrete stream, including for borrowed canonical encoded entries.
    pub fn new(
        entry_type: UnifiedLogType,
        parent_logger: Arc<Mutex<L>>,
        minimum_allocation_amount: usize,
    ) -> CuResult<Self> {
        #[cfg(feature = "std")]
        let section = parent_logger
            .lock()
            .map_err(|e| {
                CuError::from("Could not lock a section at LogStream creation")
                    .add_cause(e.to_string().as_str())
            })?
            .add_section(entry_type, minimum_allocation_amount)?;

        #[cfg(not(feature = "std"))]
        let section = parent_logger
            .lock()
            .add_section(entry_type, minimum_allocation_amount)?;

        Ok(Self {
            entry_type,
            parent_logger,
            current_section: section,
            minimum_allocation_amount,
            last_log_bytes: 0,
        })
    }
}

impl<S: SectionStorage, L: UnifiedLogWrite<S>> Debug for LogStream<S, L> {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(
            f,
            "MmapStream {{ entry_type: {:?}, current_position: {}, minimum_allocation_amount: {} }}",
            self.entry_type, self.current_section.header.used, self.minimum_allocation_amount
        )
    }
}

impl<E: Encode, S: SectionStorage, L: UnifiedLogWrite<S>> WriteStream<E> for LogStream<S, L> {
    fn log(&mut self, obj: &E) -> CuResult<()> {
        //let dst = self.current_section.get_user_buffer();
        // let result = encode_into_slice(obj, dst, standard());
        let result = self.current_section.append(obj);
        match result {
            Ok(nb_bytes) => {
                record_bytes(&mut self.current_section.header, nb_bytes)?;
                self.last_log_bytes = nb_bytes;
                // Track encoded bytes so monitoring can compute actual bytes written.
                Ok(())
            }
            Err(e) => match e {
                EncodeError::UnexpectedEnd => {
                    #[cfg(feature = "std")]
                    let logger_guard = self.parent_logger.lock();

                    #[cfg(not(feature = "std"))]
                    let mut logger_guard = self.parent_logger.lock();

                    #[cfg(feature = "std")]
                    let mut logger_guard =
                        match logger_guard {
                            Ok(g) => g,
                            Err(_) => return Err(
                                "Logger mutex poisoned while reporting EncodeError::UnexpectedEnd"
                                    .into(),
                            ), // It will retry but at least not completely crash.
                        };

                    logger_guard.flush_section(&mut self.current_section);
                    self.current_section = logger_guard
                        .add_section(self.entry_type, self.minimum_allocation_amount)?;

                    let result = self
                        .current_section
                        .append(obj)
                        .map_err(|e| {
                            CuError::from(
                                "Failed to encode object in a newly minted section. Unrecoverable failure.",
                            )
                            .add_cause(e.to_string().as_str())
                        })?; // If we fail just after creating a section, there is not much we can do.

                    record_bytes(&mut self.current_section.header, result)?;
                    self.last_log_bytes = result;
                    Ok(())
                }
                _ => {
                    let err =
                        <&str as Into<CuError>>::into("Unexpected error while encoding object.")
                            .add_cause(e.to_string().as_str());
                    Err(err)
                }
            },
        }
    }

    fn last_log_bytes(&self) -> Option<usize> {
        Some(self.last_log_bytes)
    }
}

impl<S: SectionStorage, L: UnifiedLogWrite<S>> Drop for LogStream<S, L> {
    fn drop(&mut self) {
        #[cfg(feature = "std")]
        match self.parent_logger.lock() {
            Ok(mut logger_guard) => {
                logger_guard.flush_section(&mut self.current_section);
            }
            Err(_) => {
                // Only surface the warning when a real poisoning occurred.
                if !std::thread::panicking() {
                    eprintln!("⚠️ MmapStream::drop: logger mutex poisoned");
                }
            }
        }

        #[cfg(not(feature = "std"))]
        {
            let mut logger_guard = self.parent_logger.lock();
            logger_guard.flush_section(&mut self.current_section);
        }
    }
}

#[cfg(test)]
mod accounting_tests {
    use super::*;

    // A deliberately faulty storage backend reports sizes without encoding data.
    struct ReportedSizeStorage {
        bytes: usize,
        rotate: bool,
    }
    impl SectionStorage for ReportedSizeStorage {
        fn initialize<E: Encode>(&mut self, _: &E) -> Result<usize, EncodeError> {
            Ok(0)
        }
        fn post_update_header<E: Encode>(&mut self, _: &E) -> Result<usize, EncodeError> {
            Ok(0)
        }
        fn append<E: Encode>(&mut self, _: &E) -> Result<usize, EncodeError> {
            if self.rotate {
                Err(EncodeError::UnexpectedEnd)
            } else {
                Ok(self.bytes)
            }
        }
        fn flush(&mut self) -> CuResult<usize> {
            Ok(0)
        }
    }
    struct ReportedSizeLogger {
        bytes: usize,
        rotate: bool,
    }
    impl UnifiedLogWrite<ReportedSizeStorage> for ReportedSizeLogger {
        fn add_section(
            &mut self,
            _: UnifiedLogType,
            _: usize,
        ) -> CuResult<SectionHandle<ReportedSizeStorage>> {
            SectionHandle::create(
                SectionHeader::default(),
                ReportedSizeStorage {
                    bytes: self.bytes,
                    rotate: core::mem::take(&mut self.rotate),
                },
            )
        }
        fn flush_section(&mut self, _: &mut SectionHandle<ReportedSizeStorage>) {}
        fn status(&self) -> UnifiedLogStatus {
            UnifiedLogStatus {
                total_used_space: 0,
                total_allocated_space: 0,
            }
        }
    }

    #[test]
    fn test_stream_rejects_counter_overflow_without_changing_header() {
        let logger = Arc::new(Mutex::new(ReportedSizeLogger {
            bytes: u32::MAX as usize,
            rotate: false,
        }));
        let mut stream = LogStream::new(UnifiedLogType::CopperList, logger, 4096).unwrap();
        stream.log(&0_u8).unwrap();
        assert!(stream.log(&0_u8).is_err());
        assert_eq!(stream.current_section.header.used, u32::MAX);
    }

    #[test]
    #[cfg(target_pointer_width = "64")]
    fn test_stream_rejects_truncation_on_append_and_rotation() {
        for rotate in [false, true] {
            let logger = Arc::new(Mutex::new(ReportedSizeLogger {
                bytes: u32::MAX as usize + 1,
                rotate,
            }));
            let mut stream = LogStream::new(UnifiedLogType::CopperList, logger, 4096).unwrap();
            assert!(stream.log(&0_u8).is_err());
            assert_eq!(stream.current_section.header.used, 0);
            assert_eq!(WriteStream::<u8>::last_log_bytes(&stream), Some(0));
        }
    }
}