Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

redstrate
Copy link
Contributor

This allows Rust and CXX-Qt applications to send messages to the Qt logger.

@redstrate redstrate self-assigned this Jan 17, 2024
crates/cxx-qt-lib/src/core/qtlogging.rs Outdated Show resolved Hide resolved
examples/cargo_without_cmake/src/main.rs Outdated Show resolved Hide resolved
@redstrate redstrate marked this pull request as draft January 17, 2024 19:10
@Montel
Copy link
Contributor

Montel commented Jan 18, 2024

Hi,
could you add info in CHANGELOG file please ?
Thanks

@redstrate redstrate marked this pull request as ready for review February 11, 2024 19:13
Copy link
Collaborator

@LeonMatthesKDAB LeonMatthesKDAB left a 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.

crates/cxx-qt-lib/src/core/qtlogging.rs Outdated Show resolved Hide resolved
version: i32,
line: i32,
file: &'a *const c_char,
function: &'a *const c_char,
Copy link
Collaborator

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 :)

Copy link
Contributor Author

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?

Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

@redstrate redstrate force-pushed the wip/josh/qtlogging branch 6 times, most recently from 40efe50 to 7dc5dbb Compare August 11, 2024 14:55
@redstrate redstrate force-pushed the wip/josh/qtlogging branch 2 times, most recently from f519be1 to 78b72af Compare August 18, 2024 22:50
@redstrate redstrate force-pushed the wip/josh/qtlogging branch 3 times, most recently from bff8d3a to 88eaa9e Compare December 26, 2024 18:06
Copy link

codecov bot commented Dec 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (e84662a) to head (438b6c4).

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.
📢 Have feedback on the report? Share it here.

@redstrate redstrate force-pushed the wip/josh/qtlogging branch 4 times, most recently from d204d89 to 9e0cd75 Compare December 31, 2024 15:12
Copy link
Collaborator

@LeonMatthesKDAB LeonMatthesKDAB left a 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.

crates/cxx-qt-lib/src/core/qtlogging.rs Outdated Show resolved Hide resolved
crates/cxx-qt-lib/src/core/qtlogging.rs Outdated Show resolved Hide resolved
crates/cxx-qt-lib/src/core/qtlogging.rs Outdated Show resolved Hide resolved
@redstrate redstrate force-pushed the wip/josh/qtlogging branch 2 times, most recently from 61dc340 to 6b03a11 Compare January 10, 2025 18:00
Copy link
Collaborator

@LeonMatthesKDAB LeonMatthesKDAB left a 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.

Comment on lines +35 to +57
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;
}
Copy link
Collaborator

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.

crates/cxx-qt-lib/src/core/qtlogging.rs Outdated Show resolved Hide resolved
/// A fatal message.
QtFatalMsg = 3,
/// A critical message.
QtCriticalMsg = 2,
Copy link
Contributor Author

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.

This allows Rust and CXX-Qt applications to send messages to the Qt
logger.
@redstrate redstrate changed the title Add bindings for QtMsgType, QMessageLogContext and qt_message_output cxx-qt-lib: Add bindings for QMessageLogContext and qt_message_output Jan 15, 2025
This is needed to ensure the Rust-side QMessageLogContext struct has the
expected size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants