2
2
// See the COPYRIGHT file at the top-level directory of this distribution.
3
3
// Licensed under the MIT license, see the LICENSE file or <https://opensource.org/licenses/MIT>
4
4
5
- extern crate log as rs_log;
6
-
5
+ use crate :: log as glib_log;
7
6
use glib_sys;
8
7
use translate:: * ;
9
8
@@ -47,6 +46,16 @@ pub enum GlibLoggerDomain {
47
46
/// In order to use this type, `glib` must be built with the `log` feature
48
47
/// enabled.
49
48
///
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
+ ///
50
59
/// Example:
51
60
///
52
61
/// ```no_run
@@ -70,6 +79,8 @@ pub struct GlibLogger {
70
79
71
80
impl GlibLogger {
72
81
/// Creates a new instance of [`GlibLogger`](struct.GlibLogger.html).
82
+ /// See documentation of [`GlibLogger`](struct.GlibLogger.html) for more
83
+ /// information.
73
84
///
74
85
/// Example:
75
86
///
@@ -186,6 +197,33 @@ impl rs_log::Log for GlibLogger {
186
197
fn flush ( & self ) { }
187
198
}
188
199
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
+
189
227
/// A macro which behaves exactly as `log::error!` except that it sets the
190
228
/// current log target to the contents of a `G_LOG_DOMAIN` constant (and fails
191
229
/// to build if not defined).
0 commit comments