gtk-rs / glib

DEPRECATED, use https://github.com/gtk-rs/gtk-rs-core repository instead!
http://gtk-rs.org/
MIT License
93 stars 62 forks source link

glib::log_set_handler not available #723

Closed BenjaminRi closed 4 years ago

BenjaminRi commented 4 years ago

I would like to use glib::log_set_handler as defined here:

https://github.com/gtk-rs/glib/blob/039e7698972fc2b0f77701152d3ed97a795a53f3/src/log.rs#L117

However, this handler is bound to the features feature = "v2_46", feature = "dox". What is the "dox" feature - documentation only? Why is log_set_handler only available in v2_46? I would like to do the exact same thing as mentioned in the second example here:

g_log_set_handler ("Gtk", G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL
                   | G_LOG_FLAG_RECURSION, my_log_handler, NULL);

But glib in Rust does not provide me with any method to set a handler for a particular log_domain. I can only do log_set_default_handler which does not do the same and fails to catch messages from the "Gtk" log domain.

GuillaumeGomez commented 4 years ago

The important part of the cfg is the any. So either you need dox or v2_46 feature activated (or both).

Also, the v2_46 feature matches the minimum version of the GLib library with this function.

BenjaminRi commented 4 years ago

What part of the system is responsible for setting the GLib library version? Do I do that manually in my project in Cargo.toml (how?) or does the build system figure that out by itself? My library is past 2.46 but the function is inaccessible:

error[E0425]: cannot find function `log_set_handler` in crate `glib`
    --> src\main.rs:1442:8
     |
1442 |     glib::log_set_handler(None, glib::LogLevels::all(), true, true, printerr2);
     |           ^^^^^^^^^^^^^^^ help: a function with a similar name exists: `log_default_handler`
GuillaumeGomez commented 4 years ago

You have to specify it yourself depending on your needs, nothing automatic there. As for how to specify it, I recommend you to read this part of the cargo book.

BenjaminRi commented 4 years ago

Thank you, it compiled. I didn't know I had to specify this myself. For anyone else having this issue, this is the line in my Cargo.toml that resolved it:

glib = { version= "0.10.3", features = ["v2_46"] }

By setting this feature, you signal that your application can only be used with a glib version that is 2.46 or higher.

GuillaumeGomez commented 4 years ago

I forgot but you can see the full tutorial there: https://gtk-rs.org/docs-src/tutorial/version

There are a few other tutorials that might be useful to you, so don't hesitate to take a look. ;)