Skip to content

Commit

Permalink
Optimize abbreviation. Add overloaded methods
Browse files Browse the repository at this point in the history
  • Loading branch information
iRevive committed Feb 13, 2020
1 parent 97274b0 commit 438ce7f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
32 changes: 20 additions & 12 deletions core/src/main/scala/io/odin/formatter/Formatter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ object Formatter {

val colorful: Formatter = Formatter.create(ThrowableFormat.Default, PositionFormat.Full, colorful = true)

def create(throwableFormat: ThrowableFormat, colorful: Boolean): Formatter =
create(throwableFormat, PositionFormat.Full, colorful)

/**
* Creates new formatter with provided options
*
Expand Down Expand Up @@ -76,10 +79,10 @@ object Formatter {
/**
* The result differs depending on the format:
*
* [[PositionFormat.Full]] - prints full position
* `PositionFormat.Full` - prints full position
* 'io.odin.formatter.Formatter formatPosition:75' formatted as 'io.odin.formatter.Formatter formatPosition:75'
*
* [[PositionFormat.AbbreviatePackage]] - prints abbreviated package and full enclosing
* `PositionFormat.AbbreviatePackage` - prints abbreviated package and full enclosing
* 'io.odin.formatter.Formatter formatPosition:75' formatted as 'i.o.f.Formatter formatPosition:75'
*/
def formatPosition(position: Position, format: PositionFormat): String = {
Expand All @@ -98,10 +101,10 @@ object Formatter {
* Default Throwable printer is twice as slow. This method was borrowed from scribe library.
*
* The result differs depending on the format:
* [[ThrowableFormat.Depth.Full]] - prints all elements of a stack trace
* [[ThrowableFormat.Depth.Fixed]] - prints N elements of a stack trace
* [[ThrowableFormat.Indent.NoIndent]] - prints a stack trace without indentation
* [[ThrowableFormat.Indent.Fixed]] - prints a stack trace prepending every line with N spaces
* `ThrowableFormat.Depth.Full` - prints all elements of a stack trace
* `ThrowableFormat.Depth.Fixed` - prints N elements of a stack trace
* `ThrowableFormat.Indent.NoIndent` - prints a stack trace without indentation
* `ThrowableFormat.Indent.Fixed` - prints a stack trace prepending every line with N spaces
*/
def formatThrowable(t: Throwable, format: ThrowableFormat): String = {
val indent = format.indent match {
Expand Down Expand Up @@ -157,15 +160,20 @@ object Formatter {

private def abbreviate(enclosure: String): String = {
@tailrec
def loop(input: List[String], builder: StringBuilder): StringBuilder = {
input match {
case Nil => builder
case head :: Nil => builder.append(head)
case head :: tail => loop(tail, builder.append(head.headOption.getOrElse('?')).append('.'))
def loop(input: Array[String], builder: StringBuilder): StringBuilder = {
input.length match {
case 0 => builder
case 1 => builder.append(input.head)
case _ =>
val head = input.head
val value = if (head.isEmpty) '?' else head.head
loop(input.tail, builder.append(value).append(dot))
}
}

loop(enclosure.split('.').toList, new StringBuilder).toString()
loop(enclosure.split('.'), new StringBuilder).toString()
}

private val dot = "."

}
3 changes: 3 additions & 0 deletions json/src/main/scala/io/odin/json/Formatter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ object Formatter {

val json: OFormatter = create(ThrowableFormat.Default, PositionFormat.Full)

def create(throwableFormat: ThrowableFormat): OFormatter =
create(throwableFormat, PositionFormat.Full)

def create(throwableFormat: ThrowableFormat, positionFormat: PositionFormat): OFormatter = {
implicit val encoder: Encoder[LoggerMessage] = loggerMessageEncoder(throwableFormat, positionFormat)
(msg: LoggerMessage) => msg.asJson.noSpaces
Expand Down

0 comments on commit 438ce7f

Please sign in to comment.