Skip to content

Commit 58db48e

Browse files
committed
Revert to tinylog 1.3
1 parent f9d9d4b commit 58db48e

13 files changed

Lines changed: 87 additions & 65 deletions

src/main/java/animatedledstrip/cmdline/CommandLine.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class CommandLine {
4444
}
4545

4646
input@ while (!endCmdLine) {
47-
print("> ")
4847
val str = readLine() ?: continue
4948
when (str.toUpperCase()) {
5049
"" -> continue@input

src/main/java/animatedledstrip/server/AnimatedLEDStripServer.kt

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ import animatedledstrip.leds.AnimatedLEDStrip
3232
import animatedledstrip.leds.emulated.EmulatedAnimatedLEDStrip
3333
import animatedledstrip.utils.delayBlocking
3434
import org.apache.commons.cli.DefaultParser
35-
import org.tinylog.Logger
36-
import org.tinylog.configuration.Configuration
35+
import org.pmw.tinylog.Configurator
36+
import org.pmw.tinylog.Level
37+
import org.pmw.tinylog.Logger
3738
import java.io.File
3839
import java.io.FileInputStream
3940
import java.io.FileNotFoundException
@@ -66,16 +67,19 @@ class AnimatedLEDStripServer<T : AnimatedLEDStrip>(
6667

6768
val loggingLevel =
6869
when {
69-
cmdline.hasOption("t") -> "trace"
70-
cmdline.hasOption("d") -> "debug"
71-
cmdline.hasOption("q") -> "off"
72-
else -> "info"
70+
cmdline.hasOption("t") -> Level.TRACE
71+
cmdline.hasOption("d") -> Level.DEBUG
72+
cmdline.hasOption("q") -> Level.OFF
73+
else -> Level.INFO
7374
}
7475

75-
Configuration.set("level", loggingLevel)
76-
Configuration.set("format", loggingPattern)
76+
Configurator.defaultConfig().formatPattern(loggingPattern).level(loggingLevel).addWriter(SocketWriter())
77+
.activate()
78+
79+
7780
}
78-
private val properties = Properties().apply {
81+
82+
private val properties: Properties? = Properties().apply {
7983
try {
8084
load(FileInputStream(propertyFileName))
8185
} catch (e: FileNotFoundException) {
@@ -87,22 +91,22 @@ class AnimatedLEDStripServer<T : AnimatedLEDStrip>(
8791

8892
private val emulated: Boolean = cmdline.hasOption("e") || cmdline.hasOption("E")
8993

90-
private val numLEDs: Int = properties.getProperty("numLEDs", "240").toInt()
94+
private val numLEDs: Int = properties?.getProperty("numLEDs", "240")?.toInt() ?: 240
9195

92-
private val pin: Int = properties.getProperty("pin", "12").toInt()
96+
private val pin: Int = properties?.getProperty("pin", "12")?.toInt() ?: 12
9397

9498
private val imageDebuggingEnabled: Boolean = cmdline.hasOption("i")
9599

96100
private val ports = mutableListOf<Int>().apply {
97-
properties.getProperty("ports")?.split(' ')?.forEach {
101+
properties?.getProperty("ports")?.split(' ')?.forEach {
98102
requireNotNull(it.toIntOrNull())
99103
this.add(it.toInt())
100104
}
101105
if (!emulated) this += 1118 // local port
102106
}
103107

104108
private val rendersBeforeSave =
105-
properties.getProperty("renders")?.toIntOrNull() ?: cmdline.getOptionValue("r")?.toIntOrNull() ?: 1000
109+
properties?.getProperty("renders")?.toIntOrNull() ?: cmdline.getOptionValue("r")?.toIntOrNull() ?: 1000
106110

107111
private val leds = when (emulated) {
108112
false -> ledClass.primaryConstructor!!.call(
@@ -119,7 +123,10 @@ class AnimatedLEDStripServer<T : AnimatedLEDStrip>(
119123
)
120124
}
121125

122-
internal val animationHandler = AnimationHandler(leds)
126+
private val persistAnimations =
127+
properties?.getProperty("persist", "false")?.toBoolean() ?: cmdline.hasOption("P")
128+
129+
internal val animationHandler = AnimationHandler(leds, persistAnimations = persistAnimations)
123130

124131
var testAnimation: AnimationData =
125132
AnimationData().animation(Animation.COLOR).color(CCBlue)
@@ -155,6 +162,18 @@ class AnimatedLEDStripServer<T : AnimatedLEDStrip>(
155162
Logger.info { "Shutting down server" }
156163
stop()
157164
}
165+
"DEBUG" -> {
166+
setLoggingLevel(Level.DEBUG)
167+
Logger.debug("Set logging level to debug")
168+
}
169+
"TRACE" -> {
170+
setLoggingLevel(Level.TRACE)
171+
Logger.trace("Set logging level to trace")
172+
}
173+
"INFO" -> {
174+
setLoggingLevel(Level.INFO)
175+
Logger.info("Set logging level to info")
176+
}
158177
"CLEAR" -> {
159178
animationHandler.addAnimation(AnimationData().animation(Animation.COLOR))
160179
}
@@ -179,4 +198,8 @@ class AnimatedLEDStripServer<T : AnimatedLEDStrip>(
179198
}
180199
}
181200

201+
private fun setLoggingLevel(level: Level) {
202+
Configurator.currentConfig().level(level).activate()
203+
}
204+
182205
}

src/main/java/animatedledstrip/server/AnimationHandler.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ import animatedledstrip.leds.AnimatedLEDStrip
3030
import kotlinx.coroutines.GlobalScope
3131
import kotlinx.coroutines.launch
3232
import kotlinx.coroutines.newFixedThreadPoolContext
33-
import org.tinylog.Logger
33+
import org.pmw.tinylog.Logger
3434
import java.io.File
3535
import java.io.FileInputStream
36+
import java.io.InvalidClassException
3637
import java.io.ObjectInputStream
3738
import java.lang.Math.random
3839

@@ -41,7 +42,11 @@ import java.lang.Math.random
4142
* An object that creates ContinuousRunAnimation instances for animations and
4243
* keeps track of currently running animations.
4344
*/
44-
internal class AnimationHandler(private val leds: AnimatedLEDStrip, threadCount: Int = 100) {
45+
internal class AnimationHandler(
46+
private val leds: AnimatedLEDStrip,
47+
threadCount: Int = 100,
48+
internal val persistAnimations: Boolean = false
49+
) {
4550

4651
@Suppress("EXPERIMENTAL_API_USAGE")
4752
val animationThreadPool = newFixedThreadPoolContext(threadCount, "AnimationThreads")
@@ -65,6 +70,9 @@ internal class AnimationHandler(private val leds: AnimatedLEDStrip, threadCount:
6570
close()
6671
}
6772
} catch (e: ClassCastException) {
73+
it.delete()
74+
} catch (e: InvalidClassException) {
75+
it.delete()
6876
}
6977
}
7078
}
@@ -97,7 +105,6 @@ internal class AnimationHandler(private val leds: AnimatedLEDStrip, threadCount:
97105
/* Animations that can be run repeatedly */
98106
false -> {
99107
if (params.continuous) {
100-
Logger.trace { "Calling Continuous Animation" }
101108
val id = (random() * 100000000).toInt().toString()
102109
continuousAnimations[id] =
103110
ContinuousRunAnimation(id, params, leds, this)
@@ -112,17 +119,13 @@ internal class AnimationHandler(private val leds: AnimatedLEDStrip, threadCount:
112119

113120
private fun singleRunAnimation(params: AnimationData) {
114121
GlobalScope.launch(animationThreadPool) {
115-
Logger.trace { "Calling Single Run Animation" }
116122
leds.run(params)
117-
Logger.trace { "Single Run Animation on ${Thread.currentThread().name} complete" }
118123
}
119124
}
120125

121126
fun endAnimation(params: AnimationData?) {
122-
Logger.debug { "Ending an animation" }
123127
continuousAnimations[params?.id ?: "NONE"]?.endAnimation() // End animation
124128
?: run { Logger.warn { "Animation ${params?.id} not running" }; return }
125-
// continuousAnimations.remove(params?.id) // Remove it from the continuousAnimations map
126129
}
127130

128131
fun endAnimation(animation: ContinuousRunAnimation?) {

src/main/java/animatedledstrip/server/ContinuousRunAnimation.kt

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import animatedledstrip.animationutils.Animation
2727
import animatedledstrip.animationutils.AnimationData
2828
import animatedledstrip.leds.AnimatedLEDStrip
2929
import kotlinx.coroutines.*
30-
import org.tinylog.Logger
30+
import org.pmw.tinylog.Logger
3131
import java.io.File
3232
import java.io.FileOutputStream
3333
import java.io.ObjectOutputStream
@@ -58,25 +58,13 @@ internal class ContinuousRunAnimation(
5858
private val fileName = "$id.anim"
5959

6060

61-
init {
62-
sendStartAnimation() // Send animation to GUI
63-
}
64-
65-
6661
/**
6762
* Run the animation in a new thread
6863
*/
6964
fun runAnimation() {
7065
job = GlobalScope.launch(handler.animationThreadPool) {
71-
launch {
72-
withContext(Dispatchers.IO) {
73-
ObjectOutputStream(FileOutputStream(".animations/$fileName")).apply {
74-
writeObject(params)
75-
close()
76-
}
77-
}
78-
}
79-
Logger.trace { "params: $params" }
66+
if (handler.persistAnimations) launch { saveAnimationToDisk() }
67+
sendStartAnimation()
8068
while (continueAnimation) leds.run(params)
8169
sendEndAnimation()
8270
handler.continuousAnimations.remove(id)
@@ -91,7 +79,7 @@ internal class ContinuousRunAnimation(
9179
Logger.debug { "Animation $id ending" }
9280
if (continueAnimation) continueAnimation = false
9381
else job?.cancel()
94-
if (File(".animations/$id").exists())
82+
if (File(".animations/$fileName").exists())
9583
Files.delete(Paths.get(".animations/$fileName"))
9684
}
9785

@@ -100,7 +88,6 @@ internal class ContinuousRunAnimation(
10088
* Send message to client(s) that animation has started
10189
*/
10290
fun sendStartAnimation(connection: SocketConnections.Connection? = null) {
103-
Logger.trace { "Sending animation start to client(s)" }
10491
SocketConnections.sendAnimation(params, id, connection)
10592
}
10693

@@ -110,8 +97,16 @@ internal class ContinuousRunAnimation(
11097
* @param connection
11198
*/
11299
fun sendEndAnimation(connection: SocketConnections.Connection? = null) {
113-
Logger.trace { "Sending animation end to client(s)" }
114100
SocketConnections.sendAnimation(params.copy(animation = Animation.ENDANIMATION), id, connection)
115101
}
116102

103+
private suspend fun saveAnimationToDisk() {
104+
withContext(Dispatchers.IO) {
105+
ObjectOutputStream(FileOutputStream(".animations/$fileName")).apply {
106+
writeObject(params)
107+
close()
108+
}
109+
}
110+
}
111+
117112
}

src/main/java/animatedledstrip/server/GlobalVars.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ val options = Options().apply {
1414
addOption("o", true, "Specify output file name for image debugging")
1515
addOption("r", true, "Specify number of renders between saves")
1616
addOption("i", "Enable image debugging")
17+
addOption("P", "Persist animations across restarts")
1718
addOption("T", "Run test")
1819
addOption("C", "Connect to a running server with a command line")
1920
}

src/main/java/animatedledstrip/server/SocketConnections.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import animatedledstrip.animationutils.Animation
2727
import animatedledstrip.animationutils.AnimationData
2828
import animatedledstrip.animationutils.id
2929
import kotlinx.coroutines.*
30-
import org.tinylog.Logger
30+
import org.pmw.tinylog.Logger
3131
import java.io.EOFException
3232
import java.io.ObjectInputStream
3333
import java.io.ObjectOutputStream

src/main/java/animatedledstrip/server/SocketWriter.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
package animatedledstrip.server
22

3-
import org.tinylog.core.LogEntry
4-
import org.tinylog.core.LogEntryValue
5-
import org.tinylog.writers.Writer
3+
import org.pmw.tinylog.Configuration
4+
import org.pmw.tinylog.LogEntry
5+
import org.pmw.tinylog.writers.LogEntryValue
6+
import org.pmw.tinylog.writers.PropertiesSupport
7+
import org.pmw.tinylog.writers.Writer
68

79
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
8-
class SocketWriter(properties: java.util.Map<String, String>) : Writer {
10+
@PropertiesSupport(name = "socket", properties = [])
11+
class SocketWriter : Writer {
912

10-
val delimiter = properties.getOrDefault("delimiter", ":")
13+
val delimiter = ":"
14+
15+
override fun init(p0: Configuration?) {
16+
}
1117

1218
override fun getRequiredLogEntryValues(): MutableSet<LogEntryValue> {
1319
return mutableSetOf(LogEntryValue.LEVEL, LogEntryValue.MESSAGE)
1420
}
1521

1622
override fun write(log: LogEntry?) {
1723
SocketConnections.localConnection?.sendString(
18-
"${log?.level.toString()}$delimiter".padEnd(8, ' ') +
19-
"${log?.message}"
20-
)
24+
"${log?.level.toString()}$delimiter".padEnd(8, ' ') +
25+
"${log?.message}"
26+
)
2127
}
2228

2329
override fun flush() {

src/main/resources/META-INF/services/org.tinylog.writers.Writer

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
writer = console
2-
writer.format = {{level}:|min-size=8} {message}
3-
writer.level = info
1+
tinylog.writer1 = console
2+
tinylog.writer1.format = {{level}:|min-size=8} {message}
3+
tinylog.writer1.level = info
44

5-
writer2 = socket
6-
writer2.format = {{level}:|min-size=8} {message}
7-
writer2.level = info
5+
tinylog.writer2 = socket
6+
tinylog.writer2.format = {{level}:|min-size=8} {message}
7+
tinylog.writer2.level = info

src/test/java/AnimatedLEDStripServerTest.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ import animatedledstrip.server.SocketConnections
2929
import kotlinx.coroutines.*
3030
import org.junit.Ignore
3131
import org.junit.Test
32-
import org.tinylog.configuration.Configuration
3332
import java.io.ByteArrayInputStream
3433

3534
class AnimatedLEDStripServerTest {
3635

3736
init {
3837
SocketConnections.hostIP = "0.0.0.0"
39-
Configuration.set("level", "off")
4038
}
4139

4240
val leds = EmulatedAnimatedLEDStrip(50)

0 commit comments

Comments
 (0)