Skip to content

Commit 8cf67f9

Browse files
committed
Move logging-related code to mod log
1 parent b2fa78d commit 8cf67f9

File tree

2 files changed

+76
-68
lines changed

2 files changed

+76
-68
lines changed

qmetaobject/src/lib.rs

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -814,72 +814,6 @@ fn add_to_hash(hash: *mut c_void, key: i32, value: QByteArray) {
814814
/// Refer to the documentation of Qt::UserRole
815815
pub const USER_ROLE: i32 = 0x0100;
816816

817-
cpp_class!(
818-
/// Wrapper for Qt's QMessageLogContext
819-
pub unsafe struct QMessageLogContext as "QMessageLogContext"
820-
);
821-
impl QMessageLogContext {
822-
// Return QMessageLogContext::line
823-
pub fn line(&self) -> i32 {
824-
cpp!(unsafe [self as "QMessageLogContext*"] -> i32 as "int" { return self->line; })
825-
}
826-
// Return QMessageLogContext::file
827-
pub fn file(&self) -> &str {
828-
unsafe {
829-
let x = cpp!([self as "QMessageLogContext*"] -> *const c_char as "const char*" {
830-
return self->file;
831-
});
832-
if x.is_null() {
833-
return "";
834-
}
835-
std::ffi::CStr::from_ptr(x).to_str().unwrap()
836-
}
837-
}
838-
// Return QMessageLogContext::function
839-
pub fn function(&self) -> &str {
840-
unsafe {
841-
let x = cpp!([self as "QMessageLogContext*"] -> *const c_char as "const char*" {
842-
return self->function;
843-
});
844-
if x.is_null() {
845-
return "";
846-
}
847-
std::ffi::CStr::from_ptr(x).to_str().unwrap()
848-
}
849-
}
850-
// Return QMessageLogContext::category
851-
pub fn category(&self) -> &str {
852-
unsafe {
853-
let x = cpp!([self as "QMessageLogContext*"] -> *const c_char as "const char*" {
854-
return self->category;
855-
});
856-
if x.is_null() {
857-
return "";
858-
}
859-
std::ffi::CStr::from_ptr(x).to_str().unwrap()
860-
}
861-
}
862-
}
863-
864-
/// Wrap Qt's QtMsgType enum
865-
#[repr(C)]
866-
#[derive(Copy, Clone, Debug)]
867-
pub enum QtMsgType {
868-
QtDebugMsg,
869-
QtWarningMsg,
870-
QtCriticalMsg,
871-
QtFatalMsg,
872-
QtInfoMsg,
873-
// there is also one level defined in C++ code:
874-
// QtSystemMsg = QtCriticalMsg
875-
}
876-
877-
/// Wrap qt's qInstallMessageHandler.
878-
/// Useful in order to forward the log to a rust logging framework
879-
pub fn install_message_handler(logger: extern "C" fn(QtMsgType, &QMessageLogContext, &QString)) {
880-
cpp!(unsafe [logger as "QtMessageHandler"] { qInstallMessageHandler(logger); })
881-
}
882-
883817
/// Embed files and made them available to the Qt resource system.
884818
///
885819
/// The macro accepts an identifier with optional preceding visibility modifier,
@@ -1025,6 +959,7 @@ pub use itemmodel::*;
1025959
pub mod listmodel;
1026960
pub use listmodel::*;
1027961
pub mod log;
962+
pub use crate::log::*;
1028963
pub mod qtdeclarative;
1029964
pub use qtdeclarative::*;
1030965
pub mod qmetatype;

qmetaobject/src/log.rs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,82 @@
11
//! Logging facilities and forwarding
22
3+
use std::os::raw::c_char;
4+
35
use log::{Level, logger, Record};
46

5-
use crate::{install_message_handler, QMessageLogContext, QString, QtMsgType};
6-
use crate::QtMsgType::*;
7+
use crate::QString;
8+
9+
use self::QtMsgType::*;
10+
11+
cpp! {{
12+
#include <qmetaobject_rust.hpp>
13+
}}
14+
15+
cpp_class!(
16+
/// Wrapper for Qt's QMessageLogContext
17+
pub unsafe struct QMessageLogContext as "QMessageLogContext"
18+
);
19+
impl QMessageLogContext {
20+
// Return QMessageLogContext::line
21+
pub fn line(&self) -> i32 {
22+
cpp!(unsafe [self as "QMessageLogContext*"] -> i32 as "int" { return self->line; })
23+
}
24+
// Return QMessageLogContext::file
25+
pub fn file(&self) -> &str {
26+
unsafe {
27+
let x = cpp!([self as "QMessageLogContext*"] -> *const c_char as "const char*" {
28+
return self->file;
29+
});
30+
if x.is_null() {
31+
return "";
32+
}
33+
std::ffi::CStr::from_ptr(x).to_str().unwrap()
34+
}
35+
}
36+
// Return QMessageLogContext::function
37+
pub fn function(&self) -> &str {
38+
unsafe {
39+
let x = cpp!([self as "QMessageLogContext*"] -> *const c_char as "const char*" {
40+
return self->function;
41+
});
42+
if x.is_null() {
43+
return "";
44+
}
45+
std::ffi::CStr::from_ptr(x).to_str().unwrap()
46+
}
47+
}
48+
// Return QMessageLogContext::category
49+
pub fn category(&self) -> &str {
50+
unsafe {
51+
let x = cpp!([self as "QMessageLogContext*"] -> *const c_char as "const char*" {
52+
return self->category;
53+
});
54+
if x.is_null() {
55+
return "";
56+
}
57+
std::ffi::CStr::from_ptr(x).to_str().unwrap()
58+
}
59+
}
60+
}
61+
62+
/// Wrap Qt's QtMsgType enum
63+
#[repr(C)]
64+
#[derive(Copy, Clone, Debug)]
65+
pub enum QtMsgType {
66+
QtDebugMsg,
67+
QtWarningMsg,
68+
QtCriticalMsg,
69+
QtFatalMsg,
70+
QtInfoMsg,
71+
// there is also one level defined in C++ code:
72+
// QtSystemMsg = QtCriticalMsg
73+
}
74+
75+
/// Wrap qt's qInstallMessageHandler.
76+
/// Useful in order to forward the log to a rust logging framework
77+
pub fn install_message_handler(logger: extern "C" fn(QtMsgType, &QMessageLogContext, &QString)) {
78+
cpp!(unsafe [logger as "QtMessageHandler"] { qInstallMessageHandler(logger); })
79+
}
780

881
/// Mapping from Qt logging levels to Rust logging facade's levels.
982
///

0 commit comments

Comments
 (0)