Skip to content

Use Qt's public logging APIs rather than undocumented internals #1300

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 17 additions & 24 deletions crates/cxx-qt-lib/include/core/qtlogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,31 @@
// SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// clang-format on
// SPDX-FileContributor: Joshua Goins <[email protected]>
// SPDX-FileContributor: Joshua Booth <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
#pragma once

#include "rust/cxx.h"
#include <QDebug>
#include <QtCore/qlogging.h>

QMessageLogContext
construct_qmessagelogcontext(const char* fileName,
int lineNumber,
const char* functionName,
const char* categoryName);

int
qmessagelogcontext_line(const QMessageLogContext& context);

const char*
qmessagelogcontext_file(const QMessageLogContext& context);

const char*
qmessagelogcontext_function(const QMessageLogContext& context);

const char*
qmessagelogcontext_category(const QMessageLogContext& context);
#include <QtCore/QString>

// Define namespace otherwise we hit a GCC bug
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
namespace rust {
namespace cxxqtlib1 {
void
q_debug(const char* fileName, int lineNumber, const QString& message);

void
q_info(const char* fileName, int lineNumber, const QString& message);

void
q_warning(const char* fileName, int lineNumber, const QString& message);

void
q_critical(const char* fileName, int lineNumber, const QString& message);

template<>
struct IsRelocatable<QMessageLogContext> : ::std::true_type
{};
void
q_fatal(const char* fileName, int lineNumber, const QString& message);

}
} // namespace rust
4 changes: 1 addition & 3 deletions crates/cxx-qt-lib/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ mod qvector;
pub use qvector::{QVector, QVectorElement};

mod qtlogging;
pub use qtlogging::{
q_format_log_message, q_set_message_pattern, qt_message_output, QMessageLogContext, QtMsgType,
};
pub use qtlogging::{q_critical, q_debug, q_fatal, q_info, q_set_message_pattern, q_warning};

#[cxx::bridge]
mod ffi {
Expand Down
9 changes: 9 additions & 0 deletions crates/cxx-qt-lib/src/core/qstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,15 @@ impl From<QString> for String {
}
}

impl From<fmt::Arguments<'_>> for QString {
fn from(value: fmt::Arguments) -> Self {
match value.as_str() {
Some(s) => Self::from(s),
None => Self::from(&value.to_string()),
}
}
}

impl QString {
/// Returns a copy of this string with the lowest numbered place marker replaced by string a, i.e., %1, %2, ..., %99.
pub fn arg(&self, a: &QString) -> Self {
Expand Down
71 changes: 36 additions & 35 deletions crates/cxx-qt-lib/src/core/qtlogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,57 @@
// SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// clang-format on
// SPDX-FileContributor: Joshua Goins <[email protected]>
// SPDX-FileContributor: Joshua Booth <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
#include "cxx-qt-lib/qtlogging.h"

#include <cxx-qt-lib/assertion_utils.h>

// QMessageLogContext has three "const char*" members for line, category, etc
// https://codebrowser.dev/qt5/qtbase/src/corelib/global/qlogging.h.html#QMessageLogContext
assert_alignment_and_size(QMessageLogContext, {
int version;
int line;
const char* file;
const char* function;
const char* category;
});

static_assert(!::std::is_trivially_copy_assignable<QMessageLogContext>::value);
static_assert(
!::std::is_trivially_copy_constructible<QMessageLogContext>::value);
static_assert(::std::is_trivially_destructible<QMessageLogContext>::value);

QMessageLogContext
construct_qmessagelogcontext(const char* fileName,
int lineNumber,
const char* functionName,
const char* categoryName)
#include <QtCore/qlogging.h>

namespace rust {
namespace cxxqtlib1 {

inline void
log(QtMsgType type,
const char* fileName,
int lineNumber,
const QString& message)
{
qt_message_output(
type,
QMessageLogContext(fileName, lineNumber, nullptr, "default"),
message);
}

void
q_debug(const char* fileName, int lineNumber, const QString& message)
{
return QMessageLogContext(fileName, lineNumber, functionName, categoryName);
log(QtMsgType::QtDebugMsg, fileName, lineNumber, message);
}

int
qmessagelogcontext_line(const QMessageLogContext& context)
void
q_info(const char* fileName, int lineNumber, const QString& message)
{
return context.line;
log(QtMsgType::QtInfoMsg, fileName, lineNumber, message);
}

const char*
qmessagelogcontext_file(const QMessageLogContext& context)
void
q_warning(const char* fileName, int lineNumber, const QString& message)
{
return context.file;
log(QtMsgType::QtWarningMsg, fileName, lineNumber, message);
}

const char*
qmessagelogcontext_function(const QMessageLogContext& context)
void
q_critical(const char* fileName, int lineNumber, const QString& message)
{
return context.function;
log(QtMsgType::QtCriticalMsg, fileName, lineNumber, message);
}

const char*
qmessagelogcontext_category(const QMessageLogContext& context)
void
q_fatal(const char* fileName, int lineNumber, const QString& message)
{
return context.category;
log(QtMsgType::QtFatalMsg, fileName, lineNumber, message);
}

}
}
Loading
Loading