1 // Copyright 2024, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Rust structured logging API integration tests.
16 use structured_log::structured_log;
17 use structured_log::StructuredLogSection::SubsectionEnd;
18 use structured_log::StructuredLogSection::SubsectionStart;
19 use structured_log::LOG_ID_EVENTS;
20 
21 const TAG_KEY_SAMPLE: u32 = 0x12345;
22 
23 #[test]
log_i32()24 fn log_i32() {
25     let res = structured_log!(TAG_KEY_SAMPLE, 5i32);
26     assert_eq!(res, Ok(()));
27 }
28 
29 #[test]
log_i64()30 fn log_i64() {
31     let res = structured_log!(log_id: LOG_ID_EVENTS, TAG_KEY_SAMPLE, 5i64);
32     assert_eq!(res, Ok(()));
33 }
34 
35 #[test]
log_f32()36 fn log_f32() {
37     let res = structured_log!(TAG_KEY_SAMPLE, 5f32);
38     assert_eq!(res, Ok(()));
39 }
40 
41 #[test]
log_str()42 fn log_str() {
43     let res = structured_log!(TAG_KEY_SAMPLE, "test string");
44     assert_eq!(res, Ok(()));
45 }
46 
47 #[test]
log_two_entries()48 fn log_two_entries() {
49     let res = structured_log!(TAG_KEY_SAMPLE, "test message", 10f32);
50     assert_eq!(res, Ok(()));
51 }
52 
53 #[test]
log_multiple_entries()54 fn log_multiple_entries() {
55     let res = structured_log!(TAG_KEY_SAMPLE, "test message", 10f32, 4i32, 4i32, 4i32, 4i32);
56     assert_eq!(res, Ok(()));
57 }
58 
59 #[test]
log_subsection()60 fn log_subsection() {
61     let res = structured_log!(TAG_KEY_SAMPLE, SubsectionStart, "test message", SubsectionEnd);
62     assert_eq!(res, Ok(()));
63 }
64 
65 #[test]
log_subsection_start_end_swapped()66 fn log_subsection_start_end_swapped() {
67     let res = structured_log!(TAG_KEY_SAMPLE, SubsectionEnd, "test message", SubsectionStart);
68     assert_eq!(res, Err("unable to log value"));
69 }
70 
71 #[test]
log_subsection_missing_start()72 fn log_subsection_missing_start() {
73     let res = structured_log!(TAG_KEY_SAMPLE, "test message", SubsectionEnd);
74     assert_eq!(res, Err("unable to log value"));
75 }
76 
77 #[test]
log_subsection_missing_end()78 fn log_subsection_missing_end() {
79     let res = structured_log!(TAG_KEY_SAMPLE, SubsectionStart, "test message");
80     assert_eq!(res, Err("unable to write log message"));
81 }
82 
83 #[test]
log_two_subsection()84 fn log_two_subsection() {
85     let res = structured_log!(
86         TAG_KEY_SAMPLE,
87         SubsectionStart,
88         5i32,
89         SubsectionEnd,
90         SubsectionStart,
91         "test message",
92         SubsectionEnd
93     );
94     assert_eq!(res, Ok(()));
95 }
96 
97 #[test]
log_simple_entry_and_subsection()98 fn log_simple_entry_and_subsection() {
99     let res = structured_log!(
100         TAG_KEY_SAMPLE,
101         "test message",
102         17i64,
103         SubsectionStart,
104         10f32,
105         SubsectionEnd
106     );
107     assert_eq!(res, Ok(()));
108 }
109 
110 #[test]
mixed_subsection()111 fn mixed_subsection() {
112     let res = structured_log!(
113         TAG_KEY_SAMPLE,
114         SubsectionStart,
115         10f32,
116         SubsectionStart,
117         2i32,
118         3i32,
119         SubsectionEnd,
120         SubsectionStart,
121         20i32,
122         30i32,
123         SubsectionEnd,
124         SubsectionEnd
125     );
126     assert_eq!(res, Ok(()));
127 }
128