-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cxx-qt-lib: Add bindings for QMessageLogContext and qt_message_output #814
base: main
Are you sure you want to change the base?
Conversation
Hi, |
5b47aee
to
c8364f2
Compare
c8364f2
to
f9950b7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good first pass, unfortunately the lifetimes need sorting out.
version: i32, | ||
line: i32, | ||
file: &'a *const c_char, | ||
function: &'a *const c_char, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the correct type!
&*const c_char
is a reference to a pointer, so similar to a const c_char**
.
So reading this from Rust after being assigned in C++ is undefined behavior, as C++ thinks it's a const char*
.
This needs to just be a *const c_char
.
To introduce the required lifetime, you can add a single PhantomData<&'a c_char>
member to the struct.
I'm a bit uncertain how that interacts with #[repr(C)]
. In #[repr(Rust)]
the PhantomData
won't change the size of the struct, but in #[repr(C)]
it might add a single byte, which unfortunately wouldn't allow us to make this type trivial...
Please use static assertions to check what happens either way :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added, it seems like PhantomData does not change the memory layout of the struct - it's still 32 bytes. However, how do I add a static_assert for this? It looks like all of the ones in the .cpp files are checking the size of the Qt type, not the Rust type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&'a *const c_char
is still not the correct type for the constructor argument!
I think it just needs to be &'a CStr
(see: https://doc.rust-lang.org/std/ffi/struct.CStr.html ), which is the C-compatible equivalent to &str
.
For the static assertions, we could use: https://crates.io/crates/static_assertions
We have tried to avoid unnecessary dependencies, but I think this crate should be small and stable enough to not bother anyone. @ahayzen-kdab agree?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it just needs to be &'a CStr (see: https://doc.rust-lang.org/std/ffi/struct.CStr.html ), which is the C-compatible equivalent to &str.
Oops yes, I should've known better. I use CStr all the time 😅 Will wait for andrew's opinion wrt/ static_assertions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added a static assertion now, let me know if we should have some others
40efe50
to
7dc5dbb
Compare
f519be1
to
78b72af
Compare
bff8d3a
to
88eaa9e
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #814 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 71 71
Lines 11967 11967
=========================================
Hits 11967 11967 ☔ View full report in Codecov by Sentry. |
d204d89
to
9e0cd75
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lifetimes are tricky to get right here and still need sorting out.
I also unresolved my two previous comments as both still need more work unfortunately.
61dc340
to
6b03a11
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty happy with this PR now.
Having static_assertions would be a plus. Waiting for opinion from @ahayzen-kdab there.
int | ||
qmessagelogcontext_line(const QMessageLogContext& context) | ||
{ | ||
return context.line; | ||
} | ||
|
||
const char* | ||
qmessagelogcontext_file(const QMessageLogContext& context) | ||
{ | ||
return context.file; | ||
} | ||
|
||
const char* | ||
qmessagelogcontext_function(const QMessageLogContext& context) | ||
{ | ||
return context.function; | ||
} | ||
|
||
const char* | ||
qmessagelogcontext_category(const QMessageLogContext& context) | ||
{ | ||
return context.category; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It just occured to me that we technically don't need these...
As the Rust struct has the right memory layout with named members we can just do context.category
, etc. in Rust.
We technically don't even need the constructor, as we can just create the struct entirely in Rust and it should still construct a compatible struct.
However, as we only assert the size and alignment and not the ordering of the members, I'm totally fine with keeping these functions just to be sure.
6b03a11
to
1e8003a
Compare
/// A fatal message. | ||
QtFatalMsg = 3, | ||
/// A critical message. | ||
QtCriticalMsg = 2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question while waiting: do we rename enum values to something more sensible? in this case the names are weird to avoid collisions, since in qtlogging.h these were just an enum
. hence why there' prefixed with Qt
and suffixed with Msg
.
1e8003a
to
438b6c4
Compare
This allows Rust and CXX-Qt applications to send messages to the Qt logger.
438b6c4
to
7326ea0
Compare
This is needed to ensure the Rust-side QMessageLogContext struct has the expected size.
7326ea0
to
68454a7
Compare
This allows Rust and CXX-Qt applications to send messages to the Qt logger.