diff --git a/README.md b/README.md index 5d1ba01..97d0c5e 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/build.gradle.kts b/build.gradle.kts index 37036d7..815c1bd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 { diff --git a/src/main/kotlin/dev/castive/log2/Log.kt b/src/main/kotlin/dev/castive/log2/Log.kt index 15071ad..d691303 100644 --- a/src/main/kotlin/dev/castive/log2/Log.kt +++ b/src/main/kotlin/dev/castive/log2/Log.kt @@ -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 @@ -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) { diff --git a/src/test/kotlin/dev/castive/log2/LogTest.kt b/src/test/kotlin/dev/castive/log2/LogTest.kt index b315486..acc1f32 100644 --- a/src/test/kotlin/dev/castive/log2/LogTest.kt +++ b/src/test/kotlin/dev/castive/log2/LogTest.kt @@ -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 @@ -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", @@ -42,6 +89,6 @@ class LogTest { ]) fun testNamedSetter(name: String) { Log.setPriority(name) - Assertions.assertEquals(name, Log.getPriority()) + assertEquals(name, Log.getPriority()) } } \ No newline at end of file