Skip to content

Commit

Permalink
Add comment to change in Flogger code and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
buenaflor committed Aug 4, 2023
1 parent 9d81d79 commit 7b06ce2
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ package com.buenaflor.kflogger.parser

/**
* Default implementation of the printf message parser. This parser supports all the place-holders
* available in `String#format` but can be extended, if desired, for additional behavior
* For consistency it is recommended, but not required, that custom printf parsers always extend
* from this class.
*
* available in `String#format` but can be extended, if desired, for additional behavior For
* consistency it is recommended, but not required, that custom printf parsers always extend from
* this class.
*
* This class is immutable and thread safe (and any subclasses must also be so).
*/
public expect class KDefaultPrintfMessageParser private constructor() : KPrintfMessageParser {
@Throws(KParseException::class)
override fun parsePrintfTerm(
builder: KMessageBuilder<*>?,
index: Int,
message: String?,
termStart: Int,
specStart: Int,
formatStart: Int
): Int
@Throws(KParseException::class)
override fun parsePrintfTerm(
builder: KMessageBuilder<*>?,
index: Int,
message: String?,
termStart: Int,
specStart: Int,
formatStart: Int
): Int

public companion object {
public fun getInstance(): KPrintfMessageParser
}
public companion object {
public fun getInstance(): KPrintfMessageParser
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public expect abstract class KMessageParser() {
* Implementations of this method are required to invoke the [MessageBuilder.addParameterImpl]
* method of the supplied builder once for each parameter place-holder in the message.
*/
@Throws(KParseException::class)
protected abstract fun <T> parseImpl(builder: KMessageBuilder<T>?)
@Throws(KParseException::class) protected abstract fun <T> parseImpl(builder: KMessageBuilder<T>?)

/**
* Appends the unescaped literal representation of the given message string (assumed to be escaped
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.buenaflor.kflogger.parser

public expect class KParseException : RuntimeException
public expect class KParseException : RuntimeException
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,80 @@ package com.buenaflor.kflogger.parser

/**
* A specialized [MessageParser] for processing log messages in printf style, as used by
* [String.format]. This is an abstract parser which knows how to
* process and extract placeholder terms at a high level, but does not impose its own semantics
* for place-holder types.
* [String.format]. This is an abstract parser which knows how to process and extract placeholder
* terms at a high level, but does not impose its own semantics for place-holder types.
*
*
* Typically you should not subclass this class, but instead subclass
* [DefaultPrintfMessageParser], which provides compatibility with [String.format].
* Typically you should not subclass this class, but instead subclass [DefaultPrintfMessageParser],
* which provides compatibility with [String.format].
*/
public expect abstract class KPrintfMessageParser() : KMessageParser {
/**
* Parses a single printf-like term from a log message into a message template builder.
*
*
* A simple example of an implicit parameter (the argument index is not specified):
* <pre>
* message: "Hello %s World"
* termStart: 6 ───┚╿╿
* specStart: 7 ────┤│
* formatStart: 7 ──╯│
* return: 8 ────────╯
</pre> *
* If this case there is no format specification, so `specStart == formatStart`.
*
*
* A complex example with an explicit index:
* <pre>
* message: "Hello %2$10d World"
* termStart: 6 ───┚ ╿ ╿╿
* specStart: 9 ──────╯ ││
* formatStart: 11 ─────╯│
* return: 12 ───────────╯
</pre> *
* Note that in this example the given index will be 1 (rather than 2) because printf specifies
* indices using a 1-based scheme, but internally they are 0-based.
*
* @param builder the message template builder.
* @param index the zero-based argument index for the parameter.
* @param message the complete log message string.
* @param termStart the index of the initial '%' character that starts the term.
* @param specStart the index of the first format specification character (after any optional
* index specification).
* @param formatStart the index of the (first) format character in the term.
* @return the index after the last character of the term.
*/
@Throws(KParseException::class)
protected abstract fun parsePrintfTerm(
builder: KMessageBuilder<*>?,
index: Int,
message: String?,
termStart: Int,
specStart: Int,
formatStart: Int
): Int
/**
* Parses a single printf-like term from a log message into a message template builder.
*
* A simple example of an implicit parameter (the argument index is not specified):
* <pre>
* message: "Hello %s World"
* termStart: 6 ───┚╿╿
* specStart: 7 ────┤│
* formatStart: 7 ──╯│
* return: 8 ────────╯
* </pre> *
* If this case there is no format specification, so `specStart == formatStart`.
*
* A complex example with an explicit index:
* <pre>
* message: "Hello %2$10d World"
* termStart: 6 ───┚ ╿ ╿╿
* specStart: 9 ──────╯ ││
* formatStart: 11 ─────╯│
* return: 12 ───────────╯
* </pre> *
* Note that in this example the given index will be 1 (rather than 2) because printf specifies
* indices using a 1-based scheme, but internally they are 0-based.
*
* @param builder the message template builder.
* @param index the zero-based argument index for the parameter.
* @param message the complete log message string.
* @param termStart the index of the initial '%' character that starts the term.
* @param specStart the index of the first format specification character (after any optional
* index specification).
* @param formatStart the index of the (first) format character in the term.
* @return the index after the last character of the term.
*/
@Throws(KParseException::class)
protected abstract fun parsePrintfTerm(
builder: KMessageBuilder<*>?,
index: Int,
message: String?,
termStart: Int,
specStart: Int,
formatStart: Int
): Int

public final override fun unescape(out: StringBuilder?, message: String?, start: Int, end: Int)
public final override fun unescape(out: StringBuilder?, message: String?, start: Int, end: Int)

@Throws(KParseException::class)
protected final override fun <T> parseImpl(builder: KMessageBuilder<T>?)
@Throws(KParseException::class)
protected final override fun <T> parseImpl(builder: KMessageBuilder<T>?)

public companion object {
/**
* Returns the system newline separator avoiding any issues with security exceptions or
* "suspicious" values. The only allowed return values are "\n" (default), "\r" or "\r\n".
*/
internal fun getSafeSystemNewline(): String
public companion object {
/**
* Returns the system newline separator avoiding any issues with security exceptions or
* "suspicious" values. The only allowed return values are "\n" (default), "\r" or "\r\n".
*/
internal fun getSafeSystemNewline(): String

/**
* Returns the index of the first unescaped '%' character in message starting at pos (or -1 if not
* found).
*/
// VisibleForTesting
@Throws(KParseException::class)
internal fun nextPrintfTerm(message: String, pos: Int): Int
/**
* Returns the index of the first unescaped '%' character in message starting at pos (or -1 if
* not found).
*/
// VisibleForTesting
@Throws(KParseException::class) internal fun nextPrintfTerm(message: String, pos: Int): Int

/**
* Unescapes the characters in the sub-string `s.substring(start, end)` according to
* printf style formatting rules.
*/
// VisibleForTesting
internal fun unescapePrintf(out: StringBuilder, message: String, start: Int, end: Int)
}
/**
* Unescapes the characters in the sub-string `s.substring(start, end)` according to printf
* style formatting rules.
*/
// VisibleForTesting
internal fun unescapePrintf(out: StringBuilder, message: String, start: Int, end: Int)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.buenaflor.kflogger.backend

import androidx.kruth.assertThat
import com.buenaflor.kflogger.util.IgnoreIos
import kotlin.test.Test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import com.buenaflor.kflogger.util.IgnoreIos
import kotlin.test.Test

class KDefaultPrintfMessageParserTest {
@Test
@IgnoreIos
fun testNotCrashing() {
// This test is to ensure that the code compiles and does not crash.
val messageParser = KDefaultPrintfMessageParser.getInstance()
messageParser.unescape(StringBuilder(), "", 0, 0)
}
}
@Test
@IgnoreIos
fun testNotCrashing() {
// This test is to ensure that the code compiles and does not crash.
val messageParser = KDefaultPrintfMessageParser.getInstance()
messageParser.unescape(StringBuilder(), "", 0, 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,36 @@ import com.buenaflor.kflogger.util.IgnoreIos
import kotlin.test.Test

class KPrintfMessageParserTest {
private val messageParser = CompileOnlyPrintfMessageParser()
private val templateContext = KTemplateContext(messageParser, "message")
private val messageBuilder = CompileOnlyMessageBuilder<String>(templateContext)
private val messageParser = CompileOnlyPrintfMessageParser()
private val templateContext = KTemplateContext(messageParser, "message")
private val messageBuilder = CompileOnlyMessageBuilder<String>(templateContext)

private class CompileOnlyPrintfMessageParser : KPrintfMessageParser() {
override fun parsePrintfTerm(
builder: KMessageBuilder<*>?,
index: Int,
message: String?,
termStart: Int,
specStart: Int,
formatStart: Int
): Int {
return 1
}
private class CompileOnlyPrintfMessageParser : KPrintfMessageParser() {
override fun parsePrintfTerm(
builder: KMessageBuilder<*>?,
index: Int,
message: String?,
termStart: Int,
specStart: Int,
formatStart: Int
): Int {
return 1
}
}

private class CompileOnlyMessageBuilder<T>(val templateContext: KTemplateContext) :
KMessageBuilder<String>(templateContext) {
override fun addParameterImpl(termStart: Int, termEnd: Int, param: KParameter) {}
private class CompileOnlyMessageBuilder<T>(val templateContext: KTemplateContext) :
KMessageBuilder<String>(templateContext) {
override fun addParameterImpl(termStart: Int, termEnd: Int, param: KParameter) {}

override fun buildImpl(): String {
return "${templateContext.message} + 1"
}
override fun buildImpl(): String {
return "${templateContext.message} + 1"
}
}

@Test
@IgnoreIos
fun testNotCrashing() {
// This test is to ensure that the code compiles and does not crash.
messageParser.unescape(StringBuilder(), "", 0, 0)

}
}
@Test
@IgnoreIos
fun testNotCrashing() {
// This test is to ensure that the code compiles and does not crash.
messageParser.unescape(StringBuilder(), "", 0, 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ package com.buenaflor.kflogger.parser

/**
* Default implementation of the printf message parser. This parser supports all the place-holders
* available in `String#format` but can be extended, if desired, for additional behavior
* For consistency it is recommended, but not required, that custom printf parsers always extend
* from this class.
*
* available in `String#format` but can be extended, if desired, for additional behavior For
* consistency it is recommended, but not required, that custom printf parsers always extend from
* this class.
*
* This class is immutable and thread safe (and any subclasses must also be so).
*/
public actual class KDefaultPrintfMessageParser private actual constructor() : KPrintfMessageParser() {
actual override fun parsePrintfTerm(
builder: KMessageBuilder<*>?,
index: Int,
message: String?,
termStart: Int,
specStart: Int,
formatStart: Int
): Int {
TODO("Not yet implemented")
}
public actual class KDefaultPrintfMessageParser private actual constructor() :
KPrintfMessageParser() {
actual override fun parsePrintfTerm(
builder: KMessageBuilder<*>?,
index: Int,
message: String?,
termStart: Int,
specStart: Int,
formatStart: Int
): Int {
TODO("Not yet implemented")
}

public actual companion object {
public actual fun getInstance(): KPrintfMessageParser {
TODO()
}
public actual companion object {
public actual fun getInstance(): KPrintfMessageParser {
TODO()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.buenaflor.kflogger.parser

public actual class KParseException : RuntimeException()
public actual class KParseException : RuntimeException()
Loading

0 comments on commit 7b06ce2

Please sign in to comment.