Skip to content
Open

Log #61

Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b0fce0c
init lv2-log crate
YruamaLairba Mar 25, 2020
79e0864
implement feature part of log
YruamaLairba Mar 26, 2020
101d936
impl Uribound and LogURIDcollection for type of log
YruamaLairba Mar 26, 2020
875153c
usable but unsafe draft
YruamaLairba Mar 26, 2020
3d3a31e
avoid '%' be interpreted, Format code
YruamaLairba Mar 29, 2020
862fa63
check for null string terminator for safety
YruamaLairba Mar 29, 2020
92691a4
create and use LogError enum
YruamaLairba Mar 29, 2020
eca5acd
Copy as supertrait for entry urid
YruamaLairba Apr 1, 2020
40ea7b7
add some doc
YruamaLairba Apr 1, 2020
36bf4fe
restrict log feature to the instantiation threading class
YruamaLairba Apr 2, 2020
fe6d8d7
WIP
YruamaLairba Apr 6, 2020
575a626
Merge branch 'develop' into log
YruamaLairba Aug 10, 2021
0eff4b0
fix the code after merge
YruamaLairba Aug 10, 2021
5eb55ab
apply prokopyl suggestion to refactor code
YruamaLairba Aug 10, 2021
d278053
forbid log feature only in the audio threading class
YruamaLairba Aug 10, 2021
a5c2d6f
change print to print_cstr
YruamaLairba Aug 17, 2021
5226867
remove println, it's not needed and almost useless
YruamaLairba Aug 17, 2021
97c4175
fix EntryType marker trait
YruamaLairba Aug 17, 2021
f6fd9ee
use a custom type to not have function pointer inside Option
YruamaLairba Aug 19, 2021
30656af
replace LogError by PrintError
YruamaLairba Aug 19, 2021
6e0ee4a
shorten the name and redocument URID marker
YruamaLairba Aug 19, 2021
826aeb0
ooops, log is not rt-safe but is valid in any threading class
YruamaLairba Aug 19, 2021
f0a47aa
oops, forgot to remove some _class suffix
YruamaLairba Aug 19, 2021
99e12cc
use a pointer replacement.
YruamaLairba Aug 20, 2021
ed9613b
complete and rework the log crate doc
YruamaLairba Aug 21, 2021
5134b89
fix example test (missing dev dependencie)
YruamaLairba Aug 21, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ members = [
"atom",
"core",
"core/derive",
"log",
"midi",
"state",
"sys",
Expand All @@ -104,4 +105,4 @@ lv2-units = { path = "units" }
urid = { path = "urid" }
urid-derive = { path = "urid/derive" }
lv2-urid = { path = "urid/lv2-urid" }
lv2-worker = { path = "worker" }
lv2-worker = { path = "worker" }
12 changes: 12 additions & 0 deletions log/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "lv2-log"
version = "0.1.0"
authors = ["Yruama_Lairba <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
lv2-sys = "1.0.0"
lv2-core = "1.0.0"
urid = "0.1.0"
119 changes: 119 additions & 0 deletions log/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use lv2_core::feature::*;
//use std::ffi::CString;
use std::fmt;
use std::os::raw::*; //get all common c_type
use urid::*;

pub struct EntryClass;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having the entry types in a submodule (entries for instance) would be a cleaner, although it's not a lot of code, it'll show up as quite a few types in the rustdoc and might be a bit overwhelming for new users that might want to focus elsewhere (mainly the Log feature, or the entry type trait).

unsafe impl UriBound for EntryClass {
const URI: &'static [u8] = lv2_sys::LV2_LOG__Entry;
}

pub struct ErrorClass;
unsafe impl UriBound for ErrorClass {
const URI: &'static [u8] = lv2_sys::LV2_LOG__Error;
}

pub struct NoteClass;
unsafe impl UriBound for NoteClass {
const URI: &'static [u8] = lv2_sys::LV2_LOG__Note;
}

pub struct TraceClass;
unsafe impl UriBound for TraceClass {
const URI: &'static [u8] = lv2_sys::LV2_LOG__Trace;
}

pub struct WarningClass;
unsafe impl UriBound for WarningClass {
const URI: &'static [u8] = lv2_sys::LV2_LOG__Warning;
}

/// Marker for URID representing the nature of a log message
// Note : it's may be better to have a URID trait to define a common interface
pub unsafe trait EntryType {
fn get(self) -> u32;
}

unsafe impl EntryType for URID<ErrorClass> {
fn get(self) -> u32 {
URID::<ErrorClass>::get(self)
}
}
unsafe impl EntryType for URID<NoteClass> {
fn get(self) -> u32 {
URID::<NoteClass>::get(self)
}
}
unsafe impl EntryType for URID<TraceClass> {
fn get(self) -> u32 {
URID::<TraceClass>::get(self)
}
}
unsafe impl EntryType for URID<WarningClass> {
fn get(self) -> u32 {
URID::<WarningClass>::get(self)
}
}

/// The Log feature
#[repr(transparent)]
pub struct Log<'a> {
internal: &'a lv2_sys::LV2_Log_Log,
}

unsafe impl<'a> UriBound for Log<'a> {
const URI: &'static [u8] = lv2_sys::LV2_LOG__log;
}

unsafe impl<'a> Feature for Log<'a> {
// Note: this feature can be used in any threading class and doesn't seems to have thready
// unsafty
unsafe fn from_feature_ptr(feature: *const c_void, _class: ThreadingClass) -> Option<Self> {
(feature as *const lv2_sys::LV2_Log_Log)
.as_ref()
.map(|internal| Self { internal })
}
}

impl<'a> Log<'a> {
pub fn print(&self, entry_type: impl EntryType, message: &[u8]) -> Result<(), ()> {
let printf = if let Some(printf) = self.internal.printf {
printf
} else {
return Err(());
};
let res = unsafe {
(printf)(
self.internal.handle,
entry_type.get(),
b"%s\0" as *const _ as *const c_char,
message as *const _ as *const c_char,
)
};
if res > 0 {
Ok(())
} else {
Err(())
}
}
}

/// A URID cache containing all time properties.
#[derive(URIDCollection, Debug)]
pub struct LogURIDCollection {
pub entry_class: URID<EntryClass>,
pub error_class: URID<ErrorClass>,
pub note_class: URID<NoteClass>,
pub trace_class: URID<TraceClass>,
pub warning_class: URID<WarningClass>,
//pub log: URID<Log<'a>>,
}

#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}