Skip to content

Commit b4239df

Browse files
authored
chore: dedupe TeeSink typed writes and tidy logging/redaction helpers (#188)
PR: #188
1 parent d99e05f commit b4239df

3 files changed

Lines changed: 35 additions & 57 deletions

File tree

sdk-core/src/main/kotlin/org/dexpace/sdk/core/instrumentation/ClientLogger.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,16 @@ public class ClientLogger private constructor(
141141
* Equivalent to SLF4J `Logger.isEnabledForLevel(Level)`; named `canLog` to read naturally
142142
* at call sites such as `if (logger.canLog(LogLevel.VERBOSE)) { … }`.
143143
*/
144-
public fun canLog(level: LogLevel): Boolean = slf4j.isEnabledForLevel(toSlf4j(level))
144+
public fun canLog(level: LogLevel): Boolean = slf4j.isEnabledForLevel(slf4jLevel(level))
145145

146-
internal fun slf4jLevel(level: LogLevel): Level = toSlf4j(level)
146+
internal fun slf4jLevel(level: LogLevel): Level =
147+
when (level) {
148+
LogLevel.ERROR -> Level.ERROR
149+
LogLevel.WARNING -> Level.WARN
150+
LogLevel.INFO -> Level.INFO
151+
// SLF4J has no VERBOSE; map to DEBUG (the closest convention).
152+
LogLevel.VERBOSE -> Level.DEBUG
153+
}
147154

148155
/**
149156
* One-shot guard for the [warnDroppedEventFieldOnce] diagnostic. The misuse it flags — a
@@ -175,15 +182,6 @@ public class ClientLogger private constructor(
175182
eventNameTag,
176183
)
177184
}
178-
179-
private fun toSlf4j(level: LogLevel): Level =
180-
when (level) {
181-
LogLevel.ERROR -> Level.ERROR
182-
LogLevel.WARNING -> Level.WARN
183-
LogLevel.INFO -> Level.INFO
184-
// SLF4J has no VERBOSE; map to DEBUG (the closest convention).
185-
LogLevel.VERBOSE -> Level.DEBUG
186-
}
187185
}
188186

189187
/**

sdk-core/src/main/kotlin/org/dexpace/sdk/core/instrumentation/UrlRedactor.kt

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -169,29 +169,19 @@ public object UrlRedactor {
169169
) {
170170
if (pair.isEmpty()) return
171171
val eq = pair.indexOf('=')
172-
val encodedName: String
173-
val hasValue: Boolean
174-
val encodedValue: String
175172
if (eq < 0) {
176-
encodedName = pair
177-
hasValue = false
178-
encodedValue = ""
179-
} else {
180-
encodedName = pair.substring(0, eq)
181-
hasValue = true
182-
encodedValue = pair.substring(eq + 1)
173+
// Bare name with no '=' — emit verbatim; there is no value to redact.
174+
out.append(pair)
175+
return
183176
}
184177

185-
val allowed = allowedLower.contains(safeDecode(encodedName).lowercase())
186-
178+
val encodedName = pair.substring(0, eq)
187179
out.append(encodedName)
188-
if (hasValue) {
189-
out.append('=')
190-
if (allowed) {
191-
out.append(encodedValue)
192-
} else {
193-
out.append(REDACTED_ENCODED)
194-
}
180+
out.append('=')
181+
if (allowedLower.contains(safeDecode(encodedName).lowercase())) {
182+
out.append(pair.substring(eq + 1))
183+
} else {
184+
out.append(REDACTED_ENCODED)
195185
}
196186
}
197187

sdk-core/src/main/kotlin/org/dexpace/sdk/core/io/TeeSink.kt

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ internal class TeeSink(
104104
* [requested] and the remaining [tapLimit] budget, clamped to never go negative. The actual
105105
* copy and [mirrored] advancement stay at each call site.
106106
*/
107-
private fun tapAllowance(requested: Long): Long {
108-
val remaining = (tapLimit - mirrored).coerceAtLeast(0L)
109-
return if (requested < remaining) requested else remaining
110-
}
107+
private fun tapAllowance(requested: Long): Long = minOf(requested, (tapLimit - mirrored).coerceAtLeast(0L))
111108

112109
@Throws(IOException::class)
113110
override fun flush() {
@@ -119,23 +116,28 @@ internal class TeeSink(
119116
primary.close()
120117
}
121118

122-
@Throws(IOException::class)
123-
override fun write(source: ByteArray): BufferedSink {
124-
scratch.write(source)
119+
/**
120+
* Stages one typed write into [scratch] then tees it into the tap and drains it into the
121+
* primary in a single pass, returning `this` for chaining. [encode] receives [scratch]
122+
* explicitly as its `it` argument so the staged write always targets the staging [Buffer] —
123+
* never one of this `TeeSink`'s own `write*` overrides, which a `Buffer.()` receiver lambda
124+
* could silently rebind to (and self-recurse) if a `Buffer` overload were ever added.
125+
*/
126+
private inline fun staged(encode: (Buffer) -> Unit): BufferedSink {
127+
encode(scratch)
125128
drainScratch()
126129
return this
127130
}
128131

132+
@Throws(IOException::class)
133+
override fun write(source: ByteArray): BufferedSink = staged { it.write(source) }
134+
129135
@Throws(IOException::class)
130136
override fun write(
131137
source: ByteArray,
132138
offset: Int,
133139
byteCount: Int,
134-
): BufferedSink {
135-
scratch.write(source, offset, byteCount)
136-
drainScratch()
137-
return this
138-
}
140+
): BufferedSink = staged { it.write(source, offset, byteCount) }
139141

140142
@Throws(IOException::class)
141143
override fun writeAll(source: Source): Long {
@@ -157,32 +159,20 @@ internal class TeeSink(
157159
}
158160

159161
@Throws(IOException::class)
160-
override fun writeUtf8(string: String): BufferedSink {
161-
scratch.writeUtf8(string)
162-
drainScratch()
163-
return this
164-
}
162+
override fun writeUtf8(string: String): BufferedSink = staged { it.writeUtf8(string) }
165163

166164
@Throws(IOException::class)
167165
override fun writeUtf8(
168166
string: String,
169167
beginIndex: Int,
170168
endIndex: Int,
171-
): BufferedSink {
172-
scratch.writeUtf8(string, beginIndex, endIndex)
173-
drainScratch()
174-
return this
175-
}
169+
): BufferedSink = staged { it.writeUtf8(string, beginIndex, endIndex) }
176170

177171
@Throws(IOException::class)
178172
override fun writeString(
179173
string: String,
180174
charset: Charset,
181-
): BufferedSink {
182-
scratch.writeString(string, charset)
183-
drainScratch()
184-
return this
185-
}
175+
): BufferedSink = staged { it.writeString(string, charset) }
186176

187177
@Throws(IOException::class)
188178
override fun outputStream(): OutputStream {

0 commit comments

Comments
 (0)