Skip to content

Commit

Permalink
Fixed an issue where non-coloured logs weren't printed
Browse files Browse the repository at this point in the history
Added additional testing
Added missing copyright headers
Added build status
  • Loading branch information
djcass44 committed Apr 13, 2019
1 parent c59543f commit ec55a5b
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# LOG2
[![Build Status](https://ci.castive.dev/api/badges/djcass44/log2/status.svg)](https://ci.castive.dev/djcass44/log2)

Log2 is a simple library for showing pretty log statements.

Expand Down
17 changes: 17 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright 2019 Django Cass
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
Expand Down
33 changes: 24 additions & 9 deletions src/main/kotlin/dev/castive/log2/Log.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright 2019 Django Cass
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package dev.castive.log2

import java.text.SimpleDateFormat
Expand Down Expand Up @@ -53,16 +70,14 @@ object Log {

private fun log(name: String, msg: String, priority: Int, colour: String? = null) {
if(priorityLevel > priority) return
if(colour == null) {
"${timeFormat.format(System.currentTimeMillis())} | [${Thread.currentThread().name}] |-${priorities[priority]} in $name - $msg"
return
}
println(getMessage(name, msg, priority, colour))
}

internal fun getMessage(name: String, msg: String, priority: Int, colour: String? = null): String {
if(colour == null) return "${timeFormat.format(System.currentTimeMillis())} | [${Thread.currentThread().name}] |-${priorities[priority]} in $name - $msg"
// Display with pretty colours
println(if(USE_SHORT_COLOURS) {
"${timeFormat.format(System.currentTimeMillis())} | [${Thread.currentThread().name}] |-$colour${priorities[priority]}$ANSI_RESET in $name - $msg"
} else {
"$colour${timeFormat.format(System.currentTimeMillis())} | [${Thread.currentThread().name}] |-${priorities[priority]} in $name - $msg$ANSI_RESET"
})
return if(USE_SHORT_COLOURS) "${timeFormat.format(System.currentTimeMillis())} | [${Thread.currentThread().name}] |-$colour${priorities[priority]}$ANSI_RESET in $name - $msg"
else "$colour${timeFormat.format(System.currentTimeMillis())} | [${Thread.currentThread().name}] |-${priorities[priority]} in $name - $msg$ANSI_RESET"
}

public fun setPriorityLevel(level: Int) {
Expand Down
51 changes: 49 additions & 2 deletions src/test/kotlin/dev/castive/log2/LogTest.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
/*
* Copyright 2019 Django Cass
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package dev.castive.log2

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
import java.text.SimpleDateFormat

class LogTest {
@BeforeEach
internal fun setUp() {
Log.setPriorityLevel(0)
}

@Test
fun manualColours() {
Log.USE_SHORT_COLOURS = true
Expand All @@ -29,6 +53,29 @@ class LogTest {
Log.s(javaClass, "Silent test")
}
@ParameterizedTest
@ValueSource(strings = [
Log.ANSI_GREEN,
Log.ANSI_RED,
Log.ANSI_WHITE_BACKGROUND,
Log.ANSI_YELLOW
])
fun testColourChange(colour: String) {
Log.USE_SHORT_COLOURS = false
val log = Log.getMessage(javaClass.name, "Test message", 0, colour)
println(log)
assert(log.startsWith(colour))
assert(log.endsWith(Log.ANSI_RESET))
}
@Test
fun testNullColour() {
Log.USE_SHORT_COLOURS = false
val log = Log.getMessage(javaClass.name, "Test message", 0, null)
println(log)
// Non-coloured log should start with the year
assertTrue(log.startsWith(SimpleDateFormat("yyyy").format(System.currentTimeMillis())))
assertFalse(log.endsWith(Log.ANSI_RESET))
}
@ParameterizedTest
@ValueSource(strings = [
"INFO",
"VERBOSE",
Expand All @@ -42,6 +89,6 @@ class LogTest {
])
fun testNamedSetter(name: String) {
Log.setPriority(name)
Assertions.assertEquals(name, Log.getPriority())
assertEquals(name, Log.getPriority())
}
}

0 comments on commit ec55a5b

Please sign in to comment.