cu29_export/
lib.rs

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
use std::fmt::{Display, Formatter};
use std::io::Read;
use std::path::{Path, PathBuf};

use bincode::config::standard;
use bincode::decode_from_std_read;
use bincode::error::DecodeError;
use clap::{Parser, Subcommand, ValueEnum};
use cu29::prelude::*;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum ExportFormat {
    Json,
    Csv,
}

impl Display for ExportFormat {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ExportFormat::Json => write!(f, "json"),
            ExportFormat::Csv => write!(f, "csv"),
        }
    }
}

/// This is a generator for a main function to build a log extractor.
#[derive(Parser)]
#[command(author, version, about)]
pub struct LogReaderCli {
    /// The base path is the name with no _0 _1 et the end.
    /// for example for toto_0.copper, toto_1.copper ... the base name is toto.copper
    pub unifiedlog_base: PathBuf,

    #[command(subcommand)]
    pub command: Command,
}

#[derive(Subcommand)]
pub enum Command {
    /// Extract logs
    ExtractLog { log_index: PathBuf },
    /// Extract copperlists
    ExtractCopperlist {
        #[arg(short, long, default_value_t = ExportFormat::Json)]
        export_format: ExportFormat,
    },
}

/// This is a generator for a main function to build a log extractor.
/// It depends on the specific type of the CopperList payload that is determined at compile time from the configuration.
pub fn run_cli<P>() -> CuResult<()>
where
    P: CopperListTuple,
{
    let args = LogReaderCli::parse();
    let unifiedlog_base = args.unifiedlog_base;

    let UnifiedLogger::Read(dl) = UnifiedLoggerBuilder::new()
        .file_base_name(&unifiedlog_base)
        .build()
        .expect("Failed to create logger")
    else {
        panic!("Failed to create logger");
    };

    match args.command {
        Command::ExtractLog { log_index } => {
            let reader = UnifiedLoggerIOReader::new(dl, UnifiedLogType::StructuredLogLine);
            textlog_dump(reader, &log_index)?;
        }
        Command::ExtractCopperlist { export_format } => {
            println!("Extracting copperlists with format: {export_format}");
            let mut reader = UnifiedLoggerIOReader::new(dl, UnifiedLogType::CopperList);
            let iter = copperlists_dump::<P>(&mut reader);
            for entry in iter {
                println!("{entry:#?}");
            }
        }
    }

    Ok(())
}

/// Extracts the copper lists from a binary representation.
/// P is the Payload determined by the configuration of the application.
pub fn copperlists_dump<P: CopperListTuple>(
    mut src: impl Read,
) -> impl Iterator<Item = CopperList<P>> {
    std::iter::from_fn(move || {
        let entry = decode_from_std_read::<CopperList<P>, _, _>(&mut src, standard());
        match entry {
            Ok(entry) => Some(entry),
            Err(e) => match e {
                DecodeError::UnexpectedEnd { .. } => None,
                DecodeError::Io { inner, additional } => {
                    if inner.kind() == std::io::ErrorKind::UnexpectedEof {
                        None
                    } else {
                        println!("Error {inner:?} additional:{additional}");
                        None
                    }
                }
                _ => {
                    println!("Error {e:?}");
                    None
                }
            },
        }
    })
}

/// Full dump of the copper structured log from its binary representation.
/// This rebuilds a textual log.
/// src: the source of the log data
/// index: the path to the index file (containing the interned strings constructed at build time)
pub fn textlog_dump(mut src: impl Read, index: &Path) -> CuResult<()> {
    let all_strings = read_interned_strings(index)?;
    loop {
        let entry = decode_from_std_read::<CuLogEntry, _, _>(&mut src, standard());

        match entry {
            Err(DecodeError::UnexpectedEnd { .. }) => return Ok(()),
            Err(DecodeError::Io { inner, additional }) => {
                if inner.kind() == std::io::ErrorKind::UnexpectedEof {
                    return Ok(());
                } else {
                    println!("Error {inner:?} additional:{additional}");
                    return Err(CuError::new_with_cause("Error reading log", inner));
                }
            }
            Err(e) => {
                println!("Error {e:?}");
                return Err(CuError::new_with_cause("Error reading log", e));
            }
            Ok(entry) => {
                if entry.msg_index == 0 {
                    break;
                }

                let result = rebuild_logline(&all_strings, &entry);
                if result.is_err() {
                    println!("Failed to rebuild log line: {result:?}");
                    continue;
                }
                println!("{}: {}", entry.time, result.unwrap());
            }
        };
    }
    Ok(())
}

// only for not macos platforms
#[cfg(not(target_os = "macos"))]
mod python {
    use bincode::config::standard;
    use bincode::decode_from_std_read;
    use bincode::error::DecodeError;
    use cu29::prelude::*;
    use pyo3::exceptions::PyIOError;
    use pyo3::prelude::*;
    use pyo3::types::{PyDelta, PyDict, PyList};
    use std::io::Read;
    use std::path::Path;

    #[pyclass]
    pub struct PyLogIterator {
        reader: Box<dyn Read + Send + Sync>,
    }

    #[pymethods]
    impl PyLogIterator {
        fn __iter__(slf: PyRefMut<Self>) -> PyRefMut<Self> {
            slf
        }

        fn __next__(mut slf: PyRefMut<Self>) -> Option<PyResult<PyCuLogEntry>> {
            match decode_from_std_read::<CuLogEntry, _, _>(&mut slf.reader, standard()) {
                Ok(entry) => {
                    if entry.msg_index == 0 {
                        None
                    } else {
                        Some(Ok(PyCuLogEntry { inner: entry }))
                    }
                }
                Err(DecodeError::UnexpectedEnd { .. }) => None,
                Err(DecodeError::Io { inner, .. })
                    if inner.kind() == std::io::ErrorKind::UnexpectedEof =>
                {
                    None
                }
                Err(e) => Some(Err(PyIOError::new_err(e.to_string()))),
            }
        }
    }

    /// Creates an iterator of CuLogEntries from a bare binary structured log file (ie. not within a unified log).
    /// This is mainly used for using the structured logging out of the Copper framework.
    /// it returns a tuple with the iterator of log entries and the list of interned strings.
    #[pyfunction]
    pub fn struct_log_iterator_bare(
        bare_struct_src_path: &str,
        index_path: &str,
    ) -> PyResult<(PyLogIterator, Vec<String>)> {
        let file = std::fs::File::open(bare_struct_src_path)
            .map_err(|e| PyIOError::new_err(e.to_string()))?;
        let all_strings = read_interned_strings(Path::new(index_path))
            .map_err(|e| PyIOError::new_err(e.to_string()))?;
        Ok((
            PyLogIterator {
                reader: Box::new(file),
            },
            all_strings,
        ))
    }
    /// Creates an iterator of CuLogEntries from a unified log file.
    /// This function allows you to easily use python to datamind Copper's structured text logs.
    /// it returns a tuple with the iterator of log entries and the list of interned strings.
    #[pyfunction]
    pub fn struct_log_iterator_unified(
        unified_src_path: &str,
        index_path: &str,
    ) -> PyResult<(PyLogIterator, Vec<String>)> {
        let all_strings = read_interned_strings(Path::new(index_path))
            .map_err(|e| PyIOError::new_err(e.to_string()))?;

        let UnifiedLogger::Read(dl) = UnifiedLoggerBuilder::new()
            .file_base_name(Path::new(unified_src_path))
            .build()
            .expect("Failed to create logger")
        else {
            panic!("Failed to create logger");
        };

        let reader = UnifiedLoggerIOReader::new(dl, UnifiedLogType::StructuredLogLine);
        Ok((
            PyLogIterator {
                reader: Box::new(reader),
            },
            all_strings,
        ))
    }

    /// This is a python wrapper for CuLogEntries.
    #[pyclass]
    pub struct PyCuLogEntry {
        pub inner: CuLogEntry,
    }

    #[pymethods]
    impl PyCuLogEntry {
        /// Returns the timestamp of the log entry.
        pub fn ts<'a>(&self, py: Python<'a>) -> Bound<'a, PyDelta> {
            let nanoseconds = self.inner.time.0;

            // Convert nanoseconds to seconds and microseconds
            let days = (nanoseconds / 86_400_000_000_000) as i32;
            let seconds = (nanoseconds / 1_000_000_000) as i32;
            let microseconds = ((nanoseconds % 1_000_000_000) / 1_000) as i32;

            PyDelta::new(py, days, seconds, microseconds, false).unwrap()
        }

        /// Returns the index of the message in the vector of interned strings.
        pub fn msg_index(&self) -> u32 {
            self.inner.msg_index
        }

        /// Returns the index of the parameter names in the vector of interned strings.
        pub fn paramname_indexes(&self) -> Vec<u32> {
            self.inner.paramname_indexes.iter().copied().collect()
        }

        /// Returns the parameters of this log line
        pub fn params(&self) -> Vec<PyObject> {
            self.inner.params.iter().map(value_to_py).collect()
        }
    }

    #[pymodule]
    fn cu29_export(m: &Bound<'_, PyModule>) -> PyResult<()> {
        m.add_class::<PyCuLogEntry>()?;
        m.add_class::<PyLogIterator>()?;
        m.add_function(wrap_pyfunction!(struct_log_iterator_bare, m)?)?;
        m.add_function(wrap_pyfunction!(struct_log_iterator_unified, m)?)?;
        Ok(())
    }

    fn value_to_py(value: &cu29::prelude::Value) -> PyObject {
        match value {
            Value::String(s) => Python::with_gil(|py| s.into_pyobject(py).unwrap().into()),
            Value::U64(u) => Python::with_gil(|py| u.into_pyobject(py).unwrap().into()),
            Value::I64(i) => Python::with_gil(|py| i.into_pyobject(py).unwrap().into()),
            Value::F64(f) => Python::with_gil(|py| f.into_pyobject(py).unwrap().into()),
            Value::Bool(b) => Python::with_gil(|py| b.into_pyobject(py).unwrap().to_owned().into()),
            Value::CuTime(t) => Python::with_gil(|py| t.0.into_pyobject(py).unwrap().into()),
            Value::Bytes(b) => Python::with_gil(|py| b.into_pyobject(py).unwrap().into()),
            Value::Char(c) => Python::with_gil(|py| c.into_pyobject(py).unwrap().into()),
            Value::I8(i) => Python::with_gil(|py| i.into_pyobject(py).unwrap().into()),
            Value::U8(u) => Python::with_gil(|py| u.into_pyobject(py).unwrap().into()),
            Value::I16(i) => Python::with_gil(|py| i.into_pyobject(py).unwrap().into()),
            Value::U16(u) => Python::with_gil(|py| u.into_pyobject(py).unwrap().into()),
            Value::I32(i) => Python::with_gil(|py| i.into_pyobject(py).unwrap().into()),
            Value::U32(u) => Python::with_gil(|py| u.into_pyobject(py).unwrap().into()),
            Value::Map(m) => Python::with_gil(|py| {
                let dict = PyDict::new(py);
                for (k, v) in m.iter() {
                    dict.set_item(value_to_py(k), value_to_py(v)).unwrap();
                }
                dict.into_pyobject(py).unwrap().into()
            }),
            Value::F32(f) => Python::with_gil(|py| f.into_pyobject(py).unwrap().into()),
            Value::Option(o) => Python::with_gil(|py| {
                if o.is_none() {
                    py.None()
                } else {
                    o.clone().map(|v| value_to_py(&v)).unwrap()
                }
            }),
            Value::Unit => Python::with_gil(|py| py.None()),
            Value::Newtype(v) => value_to_py(v),
            Value::Seq(s) => Python::with_gil(|py| {
                let list = PyList::new(py, s.iter().map(value_to_py)).unwrap();
                list.into_pyobject(py).unwrap().into()
            }),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bincode::encode_into_slice;
    use fs_extra::dir::{copy, CopyOptions};
    use std::io::Cursor;
    use std::sync::{Arc, Mutex};
    use tempfile::{tempdir, TempDir};

    fn copy_stringindex_to_temp(tmpdir: &TempDir) -> PathBuf {
        // for some reason using the index in real only locks it and generates a change in the file.
        let temp_path = tmpdir.path();

        let mut copy_options = CopyOptions::new();
        copy_options.copy_inside = true;

        copy("test/cu29_log_index", temp_path, &copy_options).unwrap();
        temp_path.join("cu29_log_index")
    }

    #[test]
    fn test_extract_low_level_cu29_log() {
        let temp_dir = TempDir::new().unwrap();
        let temp_path = copy_stringindex_to_temp(&temp_dir);
        let entry = CuLogEntry::new(3);
        let bytes = bincode::encode_to_vec(&entry, standard()).unwrap();
        let reader = Cursor::new(bytes.as_slice());
        textlog_dump(reader, temp_path.as_path()).unwrap();
    }

    #[test]
    fn end_to_end_datalogger_and_structlog_test() {
        let dir = tempdir().expect("Failed to create temp dir");
        let path = dir
            .path()
            .join("end_to_end_datalogger_and_structlog_test.copper");
        {
            // Write a couple log entries
            let UnifiedLogger::Write(logger) = UnifiedLoggerBuilder::new()
                .write(true)
                .create(true)
                .file_base_name(&path)
                .preallocated_size(100000)
                .build()
                .expect("Failed to create logger")
            else {
                panic!("Failed to create logger")
            };
            let data_logger = Arc::new(Mutex::new(logger));
            let stream = stream_write(data_logger.clone(), UnifiedLogType::StructuredLogLine, 1024);
            let rt = LoggerRuntime::init(RobotClock::default(), stream, None::<NullLog>);

            let mut entry = CuLogEntry::new(4); // this is a "Just a String {}" log line
            entry.add_param(0, Value::String("Parameter for the log line".into()));
            log(&mut entry).expect("Failed to log");
            let mut entry = CuLogEntry::new(2); // this is a "Just a String {}" log line
            entry.add_param(0, Value::String("Parameter for the log line".into()));
            log(&mut entry).expect("Failed to log");

            // everything is dropped here
            drop(rt);
        }
        // Read back the log
        let UnifiedLogger::Read(logger) = UnifiedLoggerBuilder::new()
            .file_base_name(
                &dir.path()
                    .join("end_to_end_datalogger_and_structlog_test.copper"),
            )
            .build()
            .expect("Failed to create logger")
        else {
            panic!("Failed to create logger")
        };
        let reader = UnifiedLoggerIOReader::new(logger, UnifiedLogType::StructuredLogLine);
        let temp_dir = TempDir::new().unwrap();
        textlog_dump(
            reader,
            Path::new(copy_stringindex_to_temp(&temp_dir).as_path()),
        )
        .expect("Failed to dump log");
    }

    // This is normally generated at compile time in CuPayload.
    type MyCuPayload = (u8, i32, f32);

    /// Checks if we can recover the copper lists from a binary representation.
    #[test]
    fn test_copperlists_dump() {
        let mut data = vec![0u8; 10000];
        let mypls: [MyCuPayload; 4] = [(1, 2, 3.0), (2, 3, 4.0), (3, 4, 5.0), (4, 5, 6.0)];

        let mut offset: usize = 0;
        for pl in mypls.iter() {
            let cl = CopperList::<MyCuPayload>::new(1, *pl);
            offset +=
                encode_into_slice(&cl, &mut data.as_mut_slice()[offset..], standard()).unwrap();
        }

        let reader = Cursor::new(data);

        let mut iter = copperlists_dump::<MyCuPayload>(reader);
        assert_eq!(iter.next().unwrap().msgs, (1, 2, 3.0));
        assert_eq!(iter.next().unwrap().msgs, (2, 3, 4.0));
        assert_eq!(iter.next().unwrap().msgs, (3, 4, 5.0));
        assert_eq!(iter.next().unwrap().msgs, (4, 5, 6.0));
    }
}