File: structured_log.rs

package info (click to toggle)
rust-glib 0.20.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,308 kB
  • sloc: makefile: 18
file content (153 lines) | stat: -rw-r--r-- 4,604 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#![allow(clippy::unnecessary_to_owned)]
#![allow(clippy::unnecessary_cast)]

#[test]
#[ignore = "currently broken"]
fn structured_log() {
    use std::sync::{Arc, Mutex};

    use glib::{gstr, prelude::*, GString, LogField, LogLevel};

    let log = Arc::new(Mutex::new(Vec::new()));
    {
        let log = log.clone();
        // can only be called once per test file
        glib::log_set_writer_func(move |level, fields| {
            let fields = fields
                .iter()
                .map(|f| {
                    let value = if let Some(data) = f.user_data() {
                        assert!(f.value_str().is_none());
                        format!("USERDATA: {data}")
                    } else {
                        f.value_str().unwrap().to_owned()
                    };
                    (f.key().to_owned(), value)
                })
                .collect::<Vec<_>>();
            log.lock().unwrap().push((level, fields));
            glib::LogWriterOutput::Handled
        });
    }
    glib::log_structured!(
        "test",
        LogLevel::Message,
        {
            "MY_META" => "abc";
            "MESSAGE" => "normal with meta";
            "MY_META2" => "def";
        }
    );

    glib::log_structured!(
        None,
        LogLevel::Message,
        {
            "MY_META" => "abc";
            "MESSAGE" => "formatted with meta: {} {}", 123, 456.0;
            "MY_META2" => "def{}", "ghi";
            "EMPTY" => b"";
            GString::from("MY_META3") => b"bstring".to_owned();
        }
    );
    glib::log_structured_array(
        LogLevel::Warning,
        &[
            LogField::new(
                gstr!("MESSAGE_ID"),
                "1e45a69523d3460680e2721d3072408f".as_bytes(),
            ),
            LogField::new(gstr!("PRIORITY"), "4".as_bytes()),
            LogField::new(gstr!("MESSAGE"), "from array".as_bytes()),
            LogField::new_user_data(gstr!("SOMEDATA"), 12345),
        ],
    );
    let dict = glib::VariantDict::new(None);
    dict.insert_value(
        "MESSAGE_ID",
        &"9e093d0fac2f4d50838a649796ab154b".to_variant(),
    );
    dict.insert_value("RELEASE", &"true".to_variant());
    dict.insert_value("MY_BYTES", &"123".as_bytes().to_variant());
    dict.insert_value("MESSAGE", &"from variant".to_variant());
    glib::log_variant(Some("test"), LogLevel::Debug, &dict.end());

    let log = std::mem::take(&mut *log.lock().unwrap());
    let log = log
        .iter()
        .map(|(l, v)| {
            (
                *l,
                v.iter()
                    .map(|(k, v)| (k.as_str(), v.as_str()))
                    .collect::<Vec<_>>(),
            )
        })
        .collect::<Vec<_>>();

    let path = if cfg!(windows) {
        "glib\\tests\\structured_log.rs"
    } else {
        "glib/tests/structured_log.rs"
    };

    assert_eq!(
        log[0],
        (
            LogLevel::Message,
            vec![
                ("PRIORITY", "5" as &str),
                ("CODE_FILE", path as &str),
                ("CODE_LINE", "31" as &str),
                ("CODE_FUNC", "structured_log::structured_log" as &str),
                ("MY_META", "abc"),
                ("MESSAGE", "normal with meta"),
                ("MY_META2", "def"),
                ("GLIB_DOMAIN", "test"),
            ]
        ),
    );
    assert_eq!(
        log[1],
        (
            LogLevel::Message,
            vec![
                ("PRIORITY", "5" as &str),
                ("CODE_FILE", path as &str),
                ("CODE_LINE", "41" as &str),
                ("CODE_FUNC", "structured_log::structured_log" as &str),
                ("MY_META", "abc"),
                ("MESSAGE", "formatted with meta: 123 456"),
                ("MY_META2", "defghi"),
                ("EMPTY", ""),
                ("MY_META3", "bstring"),
            ]
        )
    );
    assert_eq!(
        log[2],
        (
            LogLevel::Warning,
            vec![
                ("MESSAGE_ID", "1e45a69523d3460680e2721d3072408f" as &str),
                ("PRIORITY", "4"),
                ("MESSAGE", "from array"),
                ("SOMEDATA", "USERDATA: 12345"),
            ]
        )
    );
    assert_eq!(
        log[3],
        (
            LogLevel::Debug,
            vec![
                ("PRIORITY", "7" as &str),
                ("GLIB_DOMAIN", "test"),
                ("MESSAGE_ID", "9e093d0fac2f4d50838a649796ab154b"),
                ("MY_BYTES", "123"),
                ("RELEASE", "true"),
                ("MESSAGE", "from variant"),
            ]
        )
    );
}