DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,911 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Ayush
Ayush

Posted on • Updated on

Writing a QML Application in Rust (Part 2)

Introduction

In the last part, we went over the basic setup for qtmetaobject-rs. In this post, we will set up the basic logging stuff for qt. While logging is not flashy or the best way to debug, it seriously sucks to work without it.

Initialize Logging

  1. We first need to add log and env_logger as dependencies in Cargo.toml.
  2. Initilaize logger in main.rs.
fn main() {
    qmetaobject::log::init_qt_to_rust();
    env_logger::init();
    info!("Logβ‹…Test");

    // Everythin Else
}
Enter fullscreen mode Exit fullscreen mode
  1. Run project
RUST_LOG=warn,error,info,debug,trace cargo run
Enter fullscreen mode Exit fullscreen mode

Note: Logging from both Rust and QML side (console.log()) should now work.

Autoload variable

While this works, it would be preferable not to set RUST_LOG every time for the project. I decided to use direnv to set the RUST_LOG variable for the directory rather than a global variable. The instructions are given below:

Install direnv

The install instructions for different distros can be found here. Since I am using arch, I decided to install it from AUR.

Hook into Shell

direnv works with many shells. Instructions to each shell can be found here.

Create .envrc file

We need to create .envrc file in the root of the project with the following contents:

export RUST_LOG=error,warn,info,debug,trace
Enter fullscreen mode Exit fullscreen mode

direnv should ask us to allow the .envrc file the first time we cd to the project directory. Then the RUST_LOG variable should automatically be set and unset when entering and leaving the project, respectively.

Conclusion

The following post will be about using KI18n with qmetaobject-rs, which is a KDE localization framework.

Top comments (0)

πŸŒ™ Dark Mode?!

Turn it on in Settings