Skip to content
This repository was archived by the owner on Jun 8, 2021. It is now read-only.

Commit d51d51a

Browse files
committed
Added rust_log_handler function to do glib-to-rust logging
1 parent 039e769 commit d51d51a

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/bridged_logging.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// See the COPYRIGHT file at the top-level directory of this distribution.
33
// Licensed under the MIT license, see the LICENSE file or <https://opensource.org/licenses/MIT>
44

5-
extern crate log as rs_log;
6-
5+
use crate::log as glib_log;
76
use glib_sys;
87
use translate::*;
98

@@ -47,6 +46,16 @@ pub enum GlibLoggerDomain {
4746
/// In order to use this type, `glib` must be built with the `log` feature
4847
/// enabled.
4948
///
49+
/// Use this if you want to use glib as the main logging output in your application,
50+
/// and want to route all logging happening through the log crate to glib logging.
51+
/// If you want the opposite, see
52+
/// [`rust_log_handler`](fn.rust_log_handler.html).
53+
///
54+
/// NOTE: This should never be used when
55+
/// [`rust_log_handler`](fn.rust_log_handler.html) has
56+
/// been registered as a default glib log handler, otherwise a stack overflow
57+
/// will occur.
58+
///
5059
/// Example:
5160
///
5261
/// ```no_run
@@ -70,6 +79,8 @@ pub struct GlibLogger {
7079

7180
impl GlibLogger {
7281
/// Creates a new instance of [`GlibLogger`](struct.GlibLogger.html).
82+
/// See documentation of [`GlibLogger`](struct.GlibLogger.html) for more
83+
/// information.
7384
///
7485
/// Example:
7586
///
@@ -186,6 +197,33 @@ impl rs_log::Log for GlibLogger {
186197
fn flush(&self) {}
187198
}
188199

200+
/// Provides a glib log handler which routes all logging messages to the
201+
/// [`log crate`](https://crates.io/crates/log).
202+
///
203+
/// In order to use this function, `glib` must be built with the `log` feature
204+
/// enabled.
205+
///
206+
/// Use this function if you want to use the log crate as the main logging
207+
/// output in your application, and want to route all logging happening in
208+
/// glib to the log crate. If you want the opposite, use [`GlibLogger`](struct.GlibLogger.html).
209+
///
210+
/// NOTE: This should never be used when [`GlibLogger`](struct.GlibLogger.html) is
211+
/// registered as a logger, otherwise a stack overflow will occur.
212+
///
213+
/// ```no_run
214+
/// glib::log_set_default_handler(glib::rust_log_handler);
215+
/// ```
216+
pub fn rust_log_handler(domain: &str, level: glib_log::LogLevel, message: &str) {
217+
let level = match level {
218+
glib_log::LogLevel::Error | glib_log::LogLevel::Critical => log::Level::Error,
219+
glib_log::LogLevel::Warning => log::Level::Warn,
220+
glib_log::LogLevel::Message | glib_log::LogLevel::Info => log::Level::Info,
221+
glib_log::LogLevel::Debug => log::Level::Debug,
222+
};
223+
224+
rs_log::log!(target: domain, level, "{}", message);
225+
}
226+
189227
/// A macro which behaves exactly as `log::error!` except that it sets the
190228
/// current log target to the contents of a `G_LOG_DOMAIN` constant (and fails
191229
/// to build if not defined).

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,14 @@ pub use log::{
199199
unset_print_handler, unset_printerr_handler, LogHandlerId, LogLevel, LogLevels,
200200
};
201201

202+
#[cfg(any(feature = "log", feature = "dox"))]
203+
extern crate log as rs_log;
204+
202205
#[cfg(any(feature = "log", feature = "dox"))]
203206
#[macro_use]
204207
mod bridged_logging;
205208
#[cfg(any(feature = "log", feature = "dox"))]
206-
pub use bridged_logging::{GlibLogger, GlibLoggerDomain, GlibLoggerFormat};
209+
pub use bridged_logging::{rust_log_handler, GlibLogger, GlibLoggerDomain, GlibLoggerFormat};
207210

208211
pub mod send_unique;
209212
pub use send_unique::{SendUnique, SendUniqueCell};

0 commit comments

Comments
 (0)