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

Commit 460632d

Browse files
committed
Added 'log-like' macros to bridged logging in bridged_logging_domain_macros feature
1 parent 8f25f76 commit 460632d

File tree

4 files changed

+138
-6
lines changed

4 files changed

+138
-6
lines changed

.travis.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ matrix:
77
env: GTK=3.14 FEATURES=
88
- os: linux
99
rust: nightly
10-
env: GTK=3.24 FEATURES="v2_48 log"
10+
env: GTK=3.24 FEATURES="v2_48 log_macros"
1111
- os: linux
1212
rust: beta
1313
env: GTK=3.14 FEATURES=
1414
- os: linux
1515
rust: beta
16-
env: GTK=3.24 FEATURES="v2_48 log"
16+
env: GTK=3.24 FEATURES="v2_48 log_macros"
1717
- os: linux
1818
rust: 1.40.0
1919
env: GTK=3.14 FEATURES=
2020
- os: linux
2121
rust: 1.40.0
22-
env: GTK=3.24 FEATURES="v2_48 log"
22+
env: GTK=3.24 FEATURES="v2_48 log_macros"
2323
- os: linux
2424
rust: stable
2525
env: GTK=3.14 FEATURES=
2626
- os: linux
2727
rust: stable
28-
env: GTK=3.24 FEATURES="v2_48 log"
28+
env: GTK=3.24 FEATURES="v2_48 log_macros"
2929
- os: osx
3030
rust: nightly
3131
env: GTK=3.14 FEATURES=
@@ -55,7 +55,7 @@ matrix:
5555
env: GTK=3.14 FEATURES= ARM=1 OTHER_TARGET="--target armv7-unknown-linux-gnueabihf"
5656
- os: linux
5757
rust: nightly
58-
env: GTK=3.24 FEATURES="v2_48 log" ARM=1 OTHER_TARGET="--target armv7-unknown-linux-gnueabihf"
58+
env: GTK=3.24 FEATURES="v2_48 log_macros" ARM=1 OTHER_TARGET="--target armv7-unknown-linux-gnueabihf"
5959
addons:
6060
apt:
6161
packages:

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ v2_58 = ["v2_56", "glib-sys/v2_58", "gobject-sys/v2_58"]
4949
v2_60 = ["v2_58", "glib-sys/v2_60"]
5050
v2_62 = ["v2_60", "glib-sys/v2_62", "gobject-sys/v2_62"]
5151
v2_64 = ["v2_62", "glib-sys/v2_64"]
52-
dox = ["glib-sys/dox", "gobject-sys/dox", "log"]
52+
log_macros = ["log"]
53+
dox = ["glib-sys/dox", "gobject-sys/dox", "log_macros"]
5354

5455
[package.metadata.docs.rs]
5556
features = ["dox"]

src/bridged_logging.rs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,133 @@ impl rs_log::Log for GlibLogger {
185185

186186
fn flush(&self) {}
187187
}
188+
189+
/// A macro which behaves exactly as `log::error!` except that it sets the
190+
/// current log target to the contents of a `G_LOG_DOMAIN` constant (and fails
191+
/// to build if not defined).
192+
///
193+
/// In order to use this macro, `glib` must be built with the `log_macros`
194+
/// feature enabled and the [`GlibLogger`](struct.GlibLogger.html) must have been
195+
/// initialized using [`GlibLoggerDomain::CrateTarget`](enum.GlibLoggerDomain.html).
196+
///
197+
/// ```no_run
198+
/// #[macro_use] extern crate glib;
199+
///
200+
/// static G_LOG_DOMAIN: &str = "my-domain";
201+
///
202+
/// error!("This will be logged under 'my-domain'");
203+
/// ```
204+
#[macro_export]
205+
#[cfg(any(feature = "dox", feature = "log_macros"))]
206+
macro_rules! error {
207+
(target: $target:expr, $($arg:tt)+) => (
208+
log::log!(target: $target, log::Level::Error, $($arg)+);
209+
);
210+
($($arg:tt)+) => (
211+
log::log!(target: G_LOG_DOMAIN, log::Level::Error, $($arg)+);
212+
)
213+
}
214+
215+
/// A macro which behaves exactly as `log::warn!` except that it sets the
216+
/// current log target to the contents of a `G_LOG_DOMAIN` constant (and fails
217+
/// to build if not defined).
218+
///
219+
/// In order to use this macro, `glib` must be built with the `log_macros`
220+
/// feature enabled and the [`GlibLogger`](struct.GlibLogger.html) must have been
221+
/// initialized using [`GlibLoggerDomain::CrateTarget`](enum.GlibLoggerDomain.html).
222+
///
223+
/// ```no_run
224+
/// #[macro_use] extern crate glib;
225+
///
226+
/// static G_LOG_DOMAIN: &str = "my-domain";
227+
///
228+
/// warn!("This will be logged under 'my-domain'");
229+
/// ```
230+
#[macro_export]
231+
#[cfg(any(feature = "dox", feature = "log_macros"))]
232+
macro_rules! warn {
233+
(target: $target:expr, $($arg:tt)+) => (
234+
log::log!(target: $target, log::Level::Warn, $($arg)+);
235+
);
236+
($($arg:tt)+) => (
237+
log::log!(target: G_LOG_DOMAIN, log::Level::Warn, $($arg)+);
238+
)
239+
}
240+
241+
/// A macro which behaves exactly as `log::info!` except that it sets the
242+
/// current log target to the contents of a `G_LOG_DOMAIN` constant (and fails
243+
/// to build if not defined).
244+
///
245+
/// In order to use this macro, `glib` must be built with the `log_macros`
246+
/// feature enabled and the [`GlibLogger`](struct.GlibLogger.html) must have been
247+
/// initialized using [`GlibLoggerDomain::CrateTarget`](enum.GlibLoggerDomain.html).
248+
///
249+
/// ```no_run
250+
/// #[macro_use] extern crate glib;
251+
///
252+
/// static G_LOG_DOMAIN: &str = "my-domain";
253+
///
254+
/// info!("This will be logged under 'my-domain'");
255+
/// ```
256+
#[macro_export]
257+
#[cfg(any(feature = "dox", feature = "log_macros"))]
258+
macro_rules! info {
259+
(target: $target:expr, $($arg:tt)+) => (
260+
log::log!(target: $target, log::Level::Info, $($arg)+);
261+
);
262+
($($arg:tt)+) => (
263+
log::log!(target: G_LOG_DOMAIN, log::Level::Info, $($arg)+);
264+
)
265+
}
266+
267+
/// A macro which behaves exactly as `log::debug!` except that it sets the
268+
/// current log target to the contents of a `G_LOG_DOMAIN` constant (and fails
269+
/// to build if not defined).
270+
///
271+
/// In order to use this macro, `glib` must be built with the `log_macros`
272+
/// feature enabled and the [`GlibLogger`](struct.GlibLogger.html) must have been
273+
/// initialized using [`GlibLoggerDomain::CrateTarget`](enum.GlibLoggerDomain.html).
274+
///
275+
/// ```no_run
276+
/// #[macro_use] extern crate glib;
277+
///
278+
/// static G_LOG_DOMAIN: &str = "my-domain";
279+
///
280+
/// debug!("This will be logged under 'my-domain'");
281+
/// ```
282+
#[macro_export]
283+
#[cfg(any(feature = "dox", feature = "log_macros"))]
284+
macro_rules! debug {
285+
(target: $target:expr, $($arg:tt)+) => (
286+
log::log!(target: $target, log::Level::Debug, $($arg)+);
287+
);
288+
($($arg:tt)+) => (
289+
log::log!(target: G_LOG_DOMAIN, log::Level::Debug, $($arg)+);
290+
)
291+
}
292+
293+
/// A macro which behaves exactly as `log::trace!` except that it sets the
294+
/// current log target to the contents of a `G_LOG_DOMAIN` constant (and fails
295+
/// to build if not defined).
296+
///
297+
/// In order to use this macro, `glib` must be built with the `log_macros`
298+
/// feature enabled and the [`GlibLogger`](struct.GlibLogger.html) must have been
299+
/// initialized using [`GlibLoggerDomain::CrateTarget`](enum.GlibLoggerDomain.html).
300+
///
301+
/// ```no_run
302+
/// #[macro_use] extern crate glib;
303+
///
304+
/// static G_LOG_DOMAIN: &str = "my-domain";
305+
///
306+
/// trace!("This will be logged under 'my-domain'");
307+
/// ```
308+
#[macro_export]
309+
#[cfg(any(feature = "dox", feature = "log_macros"))]
310+
macro_rules! trace {
311+
(target: $target:expr, $($arg:tt)+) => (
312+
log::log!(target: $target, log::Level::Trace, $($arg)+);
313+
);
314+
($($arg:tt)+) => (
315+
log::log!(target: G_LOG_DOMAIN, log::Level::Trace, $($arg)+);
316+
)
317+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ pub use log::{
200200
};
201201

202202
#[cfg(any(feature = "log", feature = "dox"))]
203+
#[macro_use]
203204
mod bridged_logging;
204205
#[cfg(any(feature = "log", feature = "dox"))]
205206
pub use bridged_logging::{GlibLogger, GlibLoggerDomain, GlibLoggerFormat};

0 commit comments

Comments
 (0)