tuwunel_core/error/
err.rs1#[macro_export]
41#[collapse_debuginfo(yes)]
42macro_rules! Err {
43 ($($args:tt)*) => {
44 Err($crate::err!($($args)*))
45 };
46}
47
48#[macro_export]
54#[collapse_debuginfo(yes)]
55macro_rules! err {
56 (HttpJson($statuscode:ident, $($args:tt)+)) => {
57 $crate::error::Error::HttpJson(
58 $crate::http::StatusCode::$statuscode,
59 ::axum::Json(::serde_json::json!($($args)+))
60 )
61 };
62
63 (Request(Forbidden($level:ident!($($args:tt)+)))) => {{
64 let mut buf = String::new();
65 $crate::error::Error::Request(
66 $crate::ruma::api::error::ErrorKind::forbidden(),
67 $crate::err_log!(buf, $level, $($args)+),
68 $crate::http::StatusCode::BAD_REQUEST
69 )
70 }};
71
72 (Request(Forbidden($($args:tt)+))) => {
73 $crate::error::Error::Request(
74 $crate::ruma::api::error::ErrorKind::forbidden(),
75 $crate::format_maybe!($($args)+),
76 $crate::http::StatusCode::BAD_REQUEST
77 )
78 };
79
80 (Request($variant:ident($level:ident!($($args:tt)+)))) => {{
81 let mut buf = String::new();
82 $crate::error::Error::Request(
83 $crate::ruma::api::error::ErrorKind::$variant,
84 $crate::err_log!(buf, $level, $($args)+),
85 $crate::http::StatusCode::BAD_REQUEST
86 )
87 }};
88
89 (Request($variant:ident($($args:tt)+))) => {
90 $crate::error::Error::Request(
91 $crate::ruma::api::error::ErrorKind::$variant,
92 $crate::format_maybe!($($args)+),
93 $crate::http::StatusCode::BAD_REQUEST
94 )
95 };
96
97 (RequestStatus($status:ident, $variant:ident($($args:tt)+))) => {
98 $crate::error::Error::RequestStatus(
99 $crate::ruma::api::error::ErrorKind::$variant,
100 $crate::format_maybe!($($args)+),
101 $crate::http::StatusCode::$status
102 )
103 };
104
105 (Config($item:literal, $($args:tt)+)) => {{
106 let mut buf = String::new();
107 $crate::error::Error::Config($item, $crate::err_log!(buf, error, config = %$item, $($args)+))
108 }};
109
110 ($variant:ident($level:ident!($($args:tt)+))) => {{
111 let mut buf = String::new();
112 $crate::error::Error::$variant($crate::err_log!(buf, $level, $($args)+))
113 }};
114
115 ($variant:ident($($args:ident),+)) => {
116 $crate::error::Error::$variant($($args),+)
117 };
118
119 ($variant:ident($($args:tt)+)) => {
120 $crate::error::Error::$variant($crate::format_maybe!($($args)+))
121 };
122
123 ($level:ident!($($args:tt)+)) => {{
124 let mut buf = String::new();
125 $crate::error::Error::Err($crate::err_log!(buf, $level, $($args)+))
126 }};
127
128 ($($args:tt)+) => {
129 $crate::error::Error::Err($crate::format_maybe!($($args)+))
130 };
131}
132
133#[macro_export]
139#[collapse_debuginfo(yes)]
140macro_rules! err_log {
141 ($out:ident, $level:ident, $($fields:tt)+) => {{
142 use $crate::tracing::{
143 callsite, callsite2, metadata, valueset, Callsite,
144 Level,
145 };
146
147 const LEVEL: Level = $crate::err_lev!($level);
148 static __CALLSITE: callsite::DefaultCallsite = callsite2! {
149 name: std::concat! {
150 "event ",
151 std::file!(),
152 ":",
153 std::line!(),
154 },
155 kind: metadata::Kind::EVENT,
156 target: std::module_path!(),
157 level: LEVEL,
158 fields: $($fields)+,
159 };
160
161 ($crate::error::visit)(
162 &mut $out,
163 LEVEL,
164 &__CALLSITE,
165 &mut valueset!(__CALLSITE.metadata().fields(), $($fields)+)
166 );
167
168 ($out).into()
169 }}
170}
171
172#[macro_export]
177#[collapse_debuginfo(yes)]
178macro_rules! err_lev {
179 (debug_warn) => {
180 if $crate::debug::logging() {
181 $crate::tracing::Level::WARN
182 } else {
183 $crate::tracing::Level::DEBUG
184 }
185 };
186
187 (debug_error) => {
188 if $crate::debug::logging() {
189 $crate::tracing::Level::ERROR
190 } else {
191 $crate::tracing::Level::DEBUG
192 }
193 };
194
195 (warn) => {
196 $crate::tracing::Level::WARN
197 };
198
199 (error) => {
200 $crate::tracing::Level::ERROR
201 };
202}
203
204use std::{fmt, fmt::Write};
205
206use tracing::{
207 __macro_support, __tracing_log, Callsite, Event, Level,
208 callsite::DefaultCallsite,
209 field::{Field, ValueSet, Visit},
210 level_enabled,
211};
212
213struct Visitor<'a>(&'a mut String);
214
215impl Visit for Visitor<'_> {
216 #[inline]
217 fn record_debug(&mut self, field: &Field, val: &dyn fmt::Debug) {
218 match field.name() {
219 | "message" => write!(self.0, "{val:?}").expect("stream error"),
220 | "config" => {},
222 | name => write!(self.0, " {name}={val:?}").expect("stream error"),
223 }
224 }
225}
226
227pub fn visit(
233 out: &mut String,
234 level: Level,
235 __callsite: &'static DefaultCallsite,
236 vs: &mut ValueSet<'_>,
237) {
238 let meta = __callsite.metadata();
239 let enabled = level_enabled!(level) && {
240 let interest = __callsite.interest();
241 !interest.is_never() && __macro_support::__is_enabled(meta, interest)
242 };
243
244 if enabled {
245 Event::dispatch(meta, vs);
246 }
247
248 __tracing_log!(level, __callsite, vs);
249 vs.record(&mut Visitor(out));
250}