1
//! Utilities for logging messages from the library.
2

            
3
#[doc(hidden)]
4
#[macro_export]
5
macro_rules! rsvg_log {
6
    (
7
        $session:expr,
8
        $($arg:tt)+
9
    ) => {
10
        if $session.log_enabled() {
11
            println!("{}", format_args!($($arg)+));
12
        }
13
    };
14
}
15

            
16
/// Captures the basic state of a [`cairo::Context`] for logging purposes.
17
///
18
/// A librsvg "transaction" like rendering a
19
/// [`crate::api::SvgHandle`], which takes a Cairo context, depends on the state of the
20
/// context as it was passed in by the caller.  For example, librsvg may decide to
21
/// operate differently depending on the context's target surface type, or its current
22
/// transformation matrix.  This struct captures that sort of information.
23
4
#[derive(Copy, Clone, Debug, PartialEq)]
24
struct CairoContextState {
25
2
    surface_type: cairo::SurfaceType,
26
2
    matrix: cairo::Matrix,
27
}
28

            
29
impl CairoContextState {
30
    #[cfg(test)]
31
2
    fn new(cr: &cairo::Context) -> Self {
32
2
        let surface_type = cr.target().type_();
33
2
        let matrix = cr.matrix();
34

            
35
2
        Self {
36
            surface_type,
37
            matrix,
38
        }
39
2
    }
40
}
41

            
42
#[cfg(test)]
43
mod tests {
44
    use super::*;
45

            
46
    #[test]
47
2
    fn captures_cr_state() {
48
1
        let surface = cairo::ImageSurface::create(cairo::Format::ARgb32, 10, 10).unwrap();
49
1
        let cr = cairo::Context::new(&surface).unwrap();
50
1
        let state = CairoContextState::new(&cr);
51

            
52
1
        assert_eq!(
53
1
            CairoContextState {
54
1
                surface_type: cairo::SurfaceType::Image,
55
1
                matrix: cairo::Matrix::identity(),
56
            },
57
            state,
58
        );
59

            
60
1
        let surface = cairo::RecordingSurface::create(cairo::Content::ColorAlpha, None).unwrap();
61
1
        let cr = cairo::Context::new(&surface).unwrap();
62
1
        cr.scale(2.0, 3.0);
63
1
        let state = CairoContextState::new(&cr);
64

            
65
1
        let mut matrix = cairo::Matrix::identity();
66
1
        matrix.scale(2.0, 3.0);
67

            
68
1
        assert_eq!(
69
1
            CairoContextState {
70
1
                surface_type: cairo::SurfaceType::Recording,
71
1
                matrix,
72
            },
73
            state,
74
        );
75
2
    }
76
}