Skip to content

Commit 206bd46

Browse files
CopilotbedaHovorka
andcommitted
WIP: Migrate to kotlin-logging - Main, DefaultContext, AbstractPath, SimpleTrack, RailSwitch partial
Co-authored-by: bedaHovorka <5263405+bedaHovorka@users.noreply.github.com>
1 parent 8b54fc0 commit 206bd46

7 files changed

Lines changed: 83 additions & 165 deletions

File tree

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ plugins {
4444
val jdiscoVersion: String by project
4545
val slf4jVersion: String by project
4646
val logbackVersion: String by project
47+
val kotlinLoggingVersion: String by project
4748
val junitPlatformVersion: String by project
4849
val junitJupiterVersion: String by project
4950
val assertjVersion: String by project
@@ -86,6 +87,7 @@ dependencies {
8687
implementation("dk.ruc.keld:jdisco:$jdiscoVersion") // Discrete event simulation library
8788
implementation("org.slf4j:slf4j-api:$slf4jVersion") // Logging facade
8889
implementation("ch.qos.logback:logback-classic:$logbackVersion") // SLF4J implementation (includes logback-core)
90+
implementation("io.github.oshai:kotlin-logging-jvm:$kotlinLoggingVersion") // Kotlin logging library
8991

9092
// Kotlin dependencies (for Phase 1 migration)
9193
implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion") // Kotlin standard library

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ kotlinVersion=2.0.20
1717
jdiscoVersion=1.2.0
1818
slf4jVersion=2.0.17
1919
logbackVersion=1.5.23
20+
kotlinLoggingVersion=7.0.3
2021
junitPlatformVersion=1.11.4
2122
junitJupiterVersion=5.11.4
2223
assertjVersion=3.27.6

src/main/kotlin/cz/vutbr/fit/interlockSim/Main.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import cz.vutbr.fit.interlockSim.sim.ShuntingLoop
2323
import cz.vutbr.fit.interlockSim.sim.SimulationException
2424
import cz.vutbr.fit.interlockSim.util.Util
2525
import cz.vutbr.fit.interlockSim.xml.XMLContextFactory
26-
import org.slf4j.LoggerFactory
26+
import io.github.oshai.kotlinlogging.KotlinLogging
2727
import java.io.File
2828
import java.io.InputStream
2929
import java.io.PrintStream
@@ -104,10 +104,10 @@ class Main private constructor() {
104104
out.println(cause.message + " cannot convert to number")
105105
return
106106
}
107-
logger.error("Example initialization failed", cause)
107+
logger.error(cause) { "Example initialization failed" }
108108
} catch (e: Exception) {
109109
out.println("Example inilialization failed")
110-
logger.error("Example initialization failed", e)
110+
logger.error(e) { "Example initialization failed" }
111111
}
112112
}
113113

@@ -172,7 +172,7 @@ class Main private constructor() {
172172
*/
173173
const val PROGRAM_FULL_NAME = "$PROGRAM_NAME $PROGRAM_VERSION"
174174

175-
private val logger = LoggerFactory.getLogger(Main::class.java)
175+
private val logger = KotlinLogging.logger {}
176176

177177
private val instance = Main()
178178

src/main/kotlin/cz/vutbr/fit/interlockSim/context/DefaultContext.kt

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ import cz.vutbr.fit.interlockSim.util.Util
3737
import jDisco.DiscoException
3838
import jDisco.Process
3939
import jDisco.Random
40-
import org.slf4j.Logger
41-
import org.slf4j.LoggerFactory
40+
import io.github.oshai.kotlinlogging.KotlinLogging
4241
import java.beans.PropertyChangeSupport
4342
import java.util.ArrayList
4443
import java.util.Collection
@@ -122,14 +121,13 @@ abstract class DefaultContext :
122121
/**
123122
* Logger for general class operations.
124123
*/
125-
private val logger: Logger = LoggerFactory.getLogger(DefaultContext::class.java)
124+
private val logger = KotlinLogging.logger {}
126125

127126
/**
128127
* Separate logger for simulation events to allow independent level control.
129128
* Configured in logback.xml as "cz.vutbr.fit.interlockSim.simulation".
130129
*/
131-
private val simulationLogger: Logger =
132-
LoggerFactory.getLogger("cz.vutbr.fit.interlockSim.simulation")
130+
private val simulationLogger = KotlinLogging.logger("cz.vutbr.fit.interlockSim.simulation")
133131

134132
/**
135133
* Constant - square root of 2
@@ -140,7 +138,7 @@ abstract class DefaultContext :
140138

141139
protected constructor(cols: Int, rows: Int) {
142140
this.railwayNetGrid = DefaultRailWayNetGrid(cols, rows)
143-
logger.debug("Initialized railway network grid: {}x{} cells", cols, rows)
141+
logger.debug { "Initialized railway network grid: ${cols}x${rows} cells" }
144142
}
145143

146144
override fun getRailWayNetGrid(): DefaultRailWayNetGrid = railwayNetGrid
@@ -461,13 +459,9 @@ abstract class DefaultContext :
461459
val lineParts = findTrackLineParts(key1, key2, trackBlock)
462460

463461
if (lineParts == null || lineParts.isEmpty()) {
464-
logger.debug(
465-
"Join failed between ({},{}) and ({},{})",
466-
key1.x,
467-
key1.y,
468-
key2.x,
469-
key2.y
470-
)
462+
logger.debug {
463+
"Join failed between (${key1.x},${key1.y}) and (${key2.x},${key2.y})"
464+
}
471465
changeSupport.firePropertyChange(
472466
ContextChangeListener.JOIN_FAILED,
473467
null,
@@ -483,14 +477,9 @@ abstract class DefaultContext :
483477
}
484478

485479
val mapSize = (lineParts as? MutableMap<Point, TrackBlockPart>)?.size ?: 0
486-
logger.debug(
487-
"Created track join ({},{})->({},{}) with {} intermediate cells",
488-
key1.x,
489-
key1.y,
490-
key2.x,
491-
key2.y,
492-
mapSize
493-
)
480+
logger.debug {
481+
"Created track join (${key1.x},${key1.y})->(${key2.x},${key2.y}) with $mapSize intermediate cells"
482+
}
494483
changeSupport.firePropertyChange(
495484
ContextChangeListener.JOIN_CREATED,
496485
null,
@@ -543,9 +532,7 @@ abstract class DefaultContext :
543532
}
544533
if (nodeCell is InOut) getInOuts().add(nodeCell as InOut)
545534
changeSupport.firePropertyChange(ContextChangeListener.CELL_ADDED, null, key)
546-
if (logger.isTraceEnabled()) {
547-
logger.trace("Added {} at ({},{})", nodeCell.javaClass.simpleName, key.x, key.y)
548-
}
535+
logger.trace { "Added ${nodeCell.javaClass.simpleName} at (${key.x},${key.y})" }
549536
}
550537

551538
/**
@@ -742,12 +729,8 @@ abstract class DefaultContext :
742729

743730
@Suppress("UNCHECKED_CAST")
744731
val result = nextTrackBlock?.getNextTrackSection(nodeCell, null as TrackSection?)
745-
if (logger.isTraceEnabled()) {
746-
logger.trace(
747-
"getNextTrackSection: navigating network from {}, result: {}",
748-
separator,
749-
if (result != null) "found" else "not found"
750-
)
732+
logger.trace {
733+
"getNextTrackSection: navigating network from $separator, result: ${if (result != null) "found" else "not found"}"
751734
}
752735
return result
753736
}
@@ -758,22 +741,19 @@ abstract class DefaultContext :
758741
@Throws(EmptyContextException::class, SimulationException::class)
759742
override fun run() {
760743
if (getGraph().isEmpty() || getGrid().isEmpty() || inouts.isEmpty()) {
761-
logger.warn(
762-
"Cannot start simulation: graph={}, grid={}, inouts={}",
763-
if (getGraph().isEmpty()) "empty" else "ok",
764-
if (getGrid().isEmpty()) "empty" else "ok",
765-
if (inouts.isEmpty()) "empty" else "ok"
766-
)
744+
logger.warn {
745+
"Cannot start simulation: graph=${if (getGraph().isEmpty()) "empty" else "ok"}, " +
746+
"grid=${if (getGrid().isEmpty()) "empty" else "ok"}, " +
747+
"inouts=${if (inouts.isEmpty()) "empty" else "ok"}"
748+
}
767749
throw EmptyContextException()
768750
}
769751
if (mainProcess == null) mainProcess = Generator(this)
770752

771-
logger.info(
772-
"Starting simulation: {} InOut points, {} track blocks, main process={}",
773-
inouts.size,
774-
getGraph().size(),
775-
mainProcess!!.javaClass.simpleName
776-
)
753+
logger.info {
754+
"Starting simulation: ${inouts.size} InOut points, ${getGraph().size()} track blocks, " +
755+
"main process=${mainProcess!!.javaClass.simpleName}"
756+
}
777757

778758
for (i in inouts) {
779759
workers[i] = InOutWorker(this, i)
@@ -782,7 +762,7 @@ abstract class DefaultContext :
782762
try {
783763
Process.activate(mainProcess)
784764
} catch (e: DiscoException) {
785-
logger.error("Failed to activate main simulation process", e)
765+
logger.error(e) { "Failed to activate main simulation process" }
786766
throw SimulationException(e)
787767
}
788768
}
@@ -854,9 +834,7 @@ abstract class DefaultContext :
854834
if (sep == null || nxt == null) {
855835
throw IllegalArgumentException("wrong arguments for aPath finding")
856836
}
857-
if (logger.isDebugEnabled()) {
858-
logger.debug("pathToNextSemaphore: searching path from {} via track section", sep)
859-
}
837+
logger.debug { "pathToNextSemaphore: searching path from $sep via track section" }
860838
var separator = sep
861839
var previous: TrackSection? = null
862840
var next: TrackSection? = nxt
@@ -874,14 +852,12 @@ abstract class DefaultContext :
874852
if (separator is OrientedPathSeparator) {
875853
if (isSeparatorInDirection(separator, next, previous)) {
876854
path.add(separator)
877-
if (logger.isTraceEnabled()) {
878-
logger.trace("pathToNextSemaphore: found complete path with length {}", path.length())
879-
}
855+
logger.trace { "pathToNextSemaphore: found complete path with length ${path.length()}" }
880856
return path
881857
}
882858
}
883859
} while (next != null)
884-
logger.debug("pathToNextSemaphore: no path found from {}", sep)
860+
logger.debug { "pathToNextSemaphore: no path found from $sep" }
885861
return null
886862
}
887863

@@ -918,7 +894,6 @@ abstract class DefaultContext :
918894
type: ReportType
919895
) {
920896
if (!isReporting(type)) return
921-
if (!simulationLogger.isInfoEnabled()) return
922897

923898
val buf = if (report is StringBuilder) report else StringBuilder(report)
924899
try {
@@ -927,11 +902,11 @@ abstract class DefaultContext :
927902
buf.insert(0, obj)
928903
}
929904
} catch (e: Exception) {
930-
logger.error("Error generating simulation report for type {}", type, e)
905+
logger.error(e) { "Error generating simulation report for type $type" }
931906
}
932907
buf.insert(0, ' ')
933908
buf.insert(0, jDisco.Process.time())
934-
simulationLogger.info("{}", buf)
909+
simulationLogger.info { buf }
935910
}
936911

937912
/**

src/main/kotlin/cz/vutbr/fit/interlockSim/objects/cells/RailSwitch.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ package cz.vutbr.fit.interlockSim.objects.cells
1111

1212
import cz.vutbr.fit.interlockSim.sim.PathSeparatorChangeException
1313
import cz.vutbr.fit.interlockSim.util.EnumUnorientedGraph
14-
import org.slf4j.Logger
15-
import org.slf4j.LoggerFactory
14+
import io.github.oshai.kotlinlogging.KotlinLogging
15+
import io.github.oshai.kotlinlogging.KotlinLoggingFactory
1616
import java.util.EnumMap
1717
import java.util.EnumSet
1818
import java.util.Map.Entry
@@ -259,7 +259,7 @@ class RailSwitch : NodeCell {
259259
}
260260

261261
companion object {
262-
private val logger: Logger = LoggerFactory.getLogger(RailSwitch::class.java)
262+
private val logger = KotlinLogging.logger {}
263263

264264
/**
265265
* allowed speed

0 commit comments

Comments
 (0)