Skip to content

Commit c37e3ec

Browse files
authored
Changed log level for messages in debugger ports detection + style improvements (metalbear-co#2991)
* Changed logs, style improvements * Changelog * Fixed doc
1 parent bdc4a04 commit c37e3ec

File tree

2 files changed

+54
-48
lines changed

2 files changed

+54
-48
lines changed

changelog.d/2986.changed.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Changed log level for debugger ports detection.

mirrord/layer/src/debugger_ports.rs

+53-48
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::{
66
};
77

88
use hyper::Uri;
9-
use tracing::{error, warn};
109

1110
/// Environment variable used to tell the layer that it should dynamically detect the local
1211
/// port used by the given debugger. Value passed through this variable should parse into
@@ -26,7 +25,7 @@ pub const MIRRORD_DETECT_DEBUGGER_PORT_ENV: &str = "MIRRORD_DETECT_DEBUGGER_PORT
2625
pub const MIRRORD_IGNORE_DEBUGGER_PORTS_ENV: &str = "MIRRORD_IGNORE_DEBUGGER_PORTS";
2726

2827
/// The default port used by node's --inspect debugger from the
29-
/// [node doceumentation](https://nodejs.org/en/learn/getting-started/debugging#enable-inspector)
28+
/// [node documentation](https://nodejs.org/en/learn/getting-started/debugging#enable-inspector)
3029
pub const NODE_INSPECTOR_DEFAULT_PORT: u16 = 9229;
3130

3231
/// Type of debugger which is used to run the user's processes.
@@ -269,9 +268,8 @@ impl DebuggerType {
269268
}.iter().filter_map(|addr| match addr.ip() {
270269
IpAddr::V4(Ipv4Addr::LOCALHOST) | IpAddr::V6(Ipv6Addr::LOCALHOST) => Some(addr.port()),
271270
other => {
272-
warn!(
273-
"Debugger uses a remote socket address {:?}! This case is not yet handled properly.",
274-
other,
271+
tracing::debug!(
272+
"Debugger uses a remote socket address {other}! This case is not yet handled properly.",
275273
);
276274
None
277275
}
@@ -300,58 +298,64 @@ pub enum DebuggerPorts {
300298
impl FromStr for DebuggerPorts {
301299
type Err = std::convert::Infallible;
302300

301+
/// Parses [`DebuggerPorts`] from a string.
302+
/// The string should look like one of:
303+
/// 1. `<port>`
304+
/// 2. `<port1>-<port2>`
305+
/// 3. Comma-separated sequence of previous two variants
303306
fn from_str(s: &str) -> Result<Self, Self::Err> {
304-
// string looks like 'port1,port2,port3-portx,porty-portz'
305-
let mut vec = vec![];
306-
s.split(',')
307-
.for_each(|s| {
308-
let chunks = s
307+
let vec = s
308+
.split(',')
309+
.filter_map(|entry| {
310+
let chunks = entry
309311
.split('-')
310312
.map(u16::from_str)
311313
.collect::<Result<Vec<_>, _>>()
312-
.inspect_err(|e| error!(
313-
"Failed to decode debugger port range from {} env variable: {}",
314-
MIRRORD_IGNORE_DEBUGGER_PORTS_ENV,
315-
e
316-
))
317-
.ok().unwrap_or_default();
318-
match *chunks.as_slice() {
319-
[p] => vec.push(Self::Single(p)),
320-
[p1, p2] if p1 <= p2 => vec.push(Self::FixedRange(p1..=p2)),
314+
.ok();
315+
316+
match chunks.as_deref() {
317+
Some(&[p]) => Some(Self::Single(p)),
318+
Some(&[p1, p2]) if p1 <= p2 => Some(Self::FixedRange(p1..=p2)),
321319
_ => {
322-
error!(
323-
"Failed to decode debugger ports from {} env variable: expected a port or range of ports",
320+
tracing::debug!(
321+
full_variable = s,
322+
entry,
323+
"Failed to decode debugger ports entry from {} env variable",
324324
MIRRORD_IGNORE_DEBUGGER_PORTS_ENV,
325325
);
326-
},
327-
};
328-
});
329-
if !vec.is_empty() {
330-
Ok(Self::Combination(vec))
331-
} else {
332-
Ok(Self::None)
333-
}
326+
None
327+
}
328+
}
329+
})
330+
.collect::<Vec<_>>();
331+
332+
let result = match vec.len() {
333+
0 => Self::None,
334+
1 => vec.into_iter().next().unwrap(),
335+
_ => Self::Combination(vec),
336+
};
337+
338+
Ok(result)
334339
}
335340
}
336341

337342
impl fmt::Display for DebuggerPorts {
343+
/// Writes [`DebuggerPorts`] into the given [`fmt::Formatter`],
344+
/// using format recognized by [`FromStr`] implementation.
338345
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
339-
write!(
340-
f,
341-
"{}",
342-
match self {
343-
DebuggerPorts::Single(port) => port.to_string(),
344-
DebuggerPorts::FixedRange(range_inclusive) =>
345-
format!("{}-{}", range_inclusive.start(), range_inclusive.end()),
346-
DebuggerPorts::Combination(vec) => {
347-
vec.iter()
348-
.map(ToString::to_string)
349-
.collect::<Vec<_>>()
350-
.join(",")
351-
}
352-
DebuggerPorts::None => String::default(),
346+
match self {
347+
Self::Single(port) => port.fmt(f),
348+
Self::FixedRange(range) => write!(f, "{}-{}", range.start(), range.end()),
349+
Self::Combination(vec) => {
350+
let value = vec
351+
.iter()
352+
.map(ToString::to_string)
353+
.collect::<Vec<_>>()
354+
.join(",");
355+
f.write_str(&value)
353356
}
354-
)
357+
Self::None => Ok(()),
358+
}
355359
}
356360
}
357361

@@ -366,10 +370,11 @@ impl DebuggerPorts {
366370
.ok()
367371
.and_then(|s| {
368372
DebuggerType::from_str(&s)
369-
.inspect_err(|e| {
370-
error!(
371-
"Failed to decode debugger type from {} env variable: {}",
372-
MIRRORD_DETECT_DEBUGGER_PORT_ENV, e
373+
.inspect_err(|error| {
374+
tracing::debug!(
375+
error,
376+
"Failed to decode debugger type from {} env variable",
377+
MIRRORD_DETECT_DEBUGGER_PORT_ENV,
373378
)
374379
})
375380
.ok()

0 commit comments

Comments
 (0)