Skip to content

Commit d913a49

Browse files
CopilotbedaHovorka
andcommitted
Migrate logging from SLF4J to kotlin-logging (Main, DefaultContext, AbstractPath, SimpleTrack, RailSwitch, RailSemaphore, Train completed)
Co-authored-by: bedaHovorka <5263405+bedaHovorka@users.noreply.github.com>
1 parent 206bd46 commit d913a49

3 files changed

Lines changed: 36 additions & 88 deletions

File tree

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

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

1212
import cz.vutbr.fit.interlockSim.objects.paths.PathElement
1313
import cz.vutbr.fit.interlockSim.sim.PathSeparatorChangeException
14-
import org.slf4j.Logger
15-
import org.slf4j.LoggerFactory
14+
import io.github.oshai.kotlinlogging.KotlinLogging
1615
import java.util.Set
1716

1817
/**
@@ -127,14 +126,12 @@ open class RailSemaphore(
127126
* @param signal
128127
*/
129128
open fun setSignal(signal: Signal) {
130-
if (logger.isDebugEnabled && this.signal != signal) {
131-
logger.debug(
132-
"Semaphore {} signal change: {} -> {} at t={}",
133-
if (getName() != null) getName() else this.hashCode(),
134-
this.signal,
135-
signal,
136-
jDisco.Process.time()
137-
)
129+
logger.debug {
130+
if (this.signal != signal) {
131+
"Semaphore ${if (getName() != null) getName() else this.hashCode()} signal change: ${this.signal} -> $signal at t=${jDisco.Process.time()}"
132+
} else {
133+
""
134+
}
138135
}
139136
this.signal = signal
140137
}
@@ -171,7 +168,7 @@ open class RailSemaphore(
171168
}
172169

173170
companion object {
174-
private val logger: Logger = LoggerFactory.getLogger(RailSemaphore::class.java)
171+
private val logger = KotlinLogging.logger {}
175172

176173
/**
177174
* create semaphore, which don't change signal - like: "predzvest", impasse end "naraznik", "rychlostnik"

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ package cz.vutbr.fit.interlockSim.objects.cells
1212
import cz.vutbr.fit.interlockSim.sim.PathSeparatorChangeException
1313
import cz.vutbr.fit.interlockSim.util.EnumUnorientedGraph
1414
import io.github.oshai.kotlinlogging.KotlinLogging
15-
import io.github.oshai.kotlinlogging.KotlinLoggingFactory
1615
import java.util.EnumMap
1716
import java.util.EnumSet
1817
import java.util.Map.Entry
@@ -167,14 +166,8 @@ class RailSwitch : NodeCell {
167166
assert(conf != null)
168167
val oldConf = conf
169168
conf = if (conf == Conf.MAIN) Conf.BRANCH else Conf.MAIN
170-
if (logger.isInfoEnabled) {
171-
logger.info(
172-
"{} Switch {} position change: {} -> {}",
173-
jDisco.Process.time(),
174-
this.hashCode(),
175-
oldConf,
176-
conf
177-
)
169+
logger.info {
170+
"${jDisco.Process.time()} Switch ${this.hashCode()} position change: $oldConf -> $conf"
178171
}
179172
}
180173

@@ -223,16 +216,8 @@ class RailSwitch : NodeCell {
223216
allowedSpeed: Double
224217
) {
225218
val newConf = getPathConfWithException(from, to)
226-
if (logger.isInfoEnabled) {
227-
logger.info(
228-
"{} Switch {} path setup: from={} to={}, conf={}, allowedSpeed={}",
229-
jDisco.Process.time(),
230-
this.hashCode(),
231-
from,
232-
to,
233-
newConf,
234-
allowedSpeed
235-
)
219+
logger.info {
220+
"${jDisco.Process.time()} Switch ${this.hashCode()} path setup: from=$from to=$to, conf=$newConf, allowedSpeed=$allowedSpeed"
236221
}
237222
setConf(newConf)
238223
}

src/main/kotlin/cz/vutbr/fit/interlockSim/sim/Train.kt

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import jDisco.Continuous
2424
import jDisco.Process
2525
import jDisco.Reporter
2626
import jDisco.Variable
27-
import org.slf4j.Logger
28-
import org.slf4j.LoggerFactory
27+
import io.github.oshai.kotlinlogging.KotlinLogging
2928

3029
/**
3130
* Train Process
@@ -35,7 +34,7 @@ class Train :
3534
Process,
3635
TrackOccupant {
3736
companion object {
38-
private val logger: Logger = LoggerFactory.getLogger(Train::class.java)
37+
private val logger = KotlinLogging.logger {}
3938
private var count: Int = 0
4039
}
4140

@@ -176,36 +175,31 @@ class Train :
176175
// isSeparatorInDirection accepts nullable Track parameters
177176
assert(context.isSeparatorInDirection(separator as OrientedPathSeparator, next, current)) { semaphore }
178177
assert(semaphore.getSignal() != null)
179-
if (logger.isInfoEnabled) {
180-
logger.info(
181-
"{} SENSOR: Train {} detected at semaphore {}, signal={}, velocity={} m/s",
182-
jDisco.Process.time(),
183-
number,
184-
if (semaphore.getName() != null) semaphore.getName() else semaphore.hashCode(),
185-
semaphore.getSignal(),
186-
getVelocity()
187-
)
178+
logger.info {
179+
"${jDisco.Process.time()} SENSOR: Train $number detected at semaphore " +
180+
"${if (semaphore.getName() != null) semaphore.getName() else semaphore.hashCode()}, " +
181+
"signal=${semaphore.getSignal()}, velocity=${getVelocity()} m/s"
188182
}
189183
val path: Path? = context.pathToNextSemaphore(separator, next!!)
190184

191185
// EXTENSION stanice
192186

193187
if (semaphore.getSignal() == RailSemaphore.Signal.STOP) {
194188
assert(getVelocity() >= 0)
195-
logger.debug("Train {} approaching semaphore with STOP signal, halting", number)
189+
logger.debug { "Train $number approaching semaphore with STOP signal, halting" }
196190
fireStop()
197191
context.report(semaphore.getSignal().toString(), this@Train, ReportType.TRAIN_EVENTS)
198192

199193
// freePath(separator, next); //vlak si sam pri zastaveni u semaforu postavi cestu k dalsimu sem.
200194
waitUntil(allowingSignal(semaphore))
201-
logger.debug("Train {} received allowing signal from semaphore, resuming movement", number)
195+
logger.debug { "Train $number received allowing signal from semaphore, resuming movement" }
202196
context.report("OK " + semaphore.getSignal(), this@Train, ReportType.TRAIN_EVENTS)
203197
fireStart(semaphore, context.pathToNextSemaphore(separator, next!!)) // znovu najit
204198
} else if (semaphore.getSignal().isAllowing() && velocity.state <= maxAbsError) {
205-
logger.debug("Train {} starting movement with allowing signal", number)
199+
logger.debug { "Train $number starting movement with allowing signal" }
206200
fireStart(semaphore, path)
207201
} else {
208-
logger.debug("Train {} accelerating toward next semaphore", number)
202+
logger.debug { "Train $number accelerating toward next semaphore" }
209203
accelerateToSignal(semaphore, path)
210204
}
211205
hold(1.0)
@@ -306,15 +300,8 @@ class Train :
306300
current: TrackSection?,
307301
next: TrackSection?
308302
) {
309-
if (logger.isDebugEnabled) {
310-
logger.debug(
311-
"{} POSITION: Train {} front at separator {}, entering block {}, leaving block {}",
312-
jDisco.Process.time(),
313-
number,
314-
where,
315-
next,
316-
current
317-
)
303+
logger.debug {
304+
"${jDisco.Process.time()} POSITION: Train $number front at separator $where, entering block $next, leaving block $current"
318305
}
319306

320307
if (where is RailSemaphore &&
@@ -345,14 +332,8 @@ class Train :
345332
current: TrackSection?,
346333
next: TrackSection?
347334
) {
348-
if (logger.isDebugEnabled) {
349-
logger.debug(
350-
"{} POSITION: Train {} tail at separator {}, clearing block {}",
351-
jDisco.Process.time(),
352-
number,
353-
where,
354-
current
355-
)
335+
logger.debug {
336+
"${jDisco.Process.time()} POSITION: Train $number tail at separator $where, clearing block $current"
356337
}
357338
if (where == timetable.getIn()) {
358339
fromHome = true
@@ -428,17 +409,13 @@ class Train :
428409
override fun iteration() {
429410
assert(currentCondition != null)
430411
accelerate = true
431-
if (logger.isTraceEnabled) {
432-
logger.trace("Train {} motor iteration: target speed {}, current velocity {}", number, targetSpeed, getVelocity())
433-
}
412+
logger.trace { "Train $number motor iteration: target speed $targetSpeed, getVelocity(), current velocity ${getVelocity()}" }
434413
start()
435414
waitUntil(currentCondition)
436415

437416
if (accelerate && currentCondition!!.getStopTest() == AccelerationStopTest.TO_HALF_SPEED) {
438417
targetSpeed = 0.0
439-
if (logger.isTraceEnabled) {
440-
logger.trace("Train {} motor: deceleration phase to half speed, target {}", number, targetSpeed)
441-
}
418+
logger.trace { "Train $number motor: deceleration phase to half speed, target $targetSpeed" }
442419
waitUntil(AccelerationStopCondition(AccelerationStopTest.DECELERATION_ENDED))
443420
}
444421

@@ -463,9 +440,7 @@ class Train :
463440
* @param speed
464441
*/
465442
fun accelerateTo(speed: Double) {
466-
if (logger.isDebugEnabled) {
467-
logger.debug("Train {} motor: accelerate to speed {}, current velocity {}", number, speed, getVelocity())
468-
}
443+
logger.debug { "Train $number motor: accelerate to speed $speed, current velocity ${getVelocity()}" }
469444
context.report("in on warning", this@Train, ReportType._DEBUG)
470445
privateAccelerateTo(
471446
speed,
@@ -484,13 +459,8 @@ class Train :
484459
* @param normalSpeed
485460
*/
486461
fun onWarning(normalSpeed: Double) {
487-
if (logger.isDebugEnabled) {
488-
logger.debug(
489-
"Train {} motor: warning mode, target speed {}, current velocity {}",
490-
number,
491-
normalSpeed,
492-
getVelocity()
493-
)
462+
logger.debug {
463+
"Train $number motor: warning mode, target speed $normalSpeed, current velocity ${getVelocity()}"
494464
}
495465
context.report("in on warning $normalSpeed", this@Train, ReportType._DEBUG)
496466

@@ -562,13 +532,9 @@ class Train :
562532
this.length = timetable.getLength()
563533
number = ++count
564534
trainPrefix = "Train #$number"
565-
logger.debug(
566-
"Train {} created: from {} to {}, length {}",
567-
number,
568-
timetable.getIn().getName(),
569-
timetable.getOut().getName(),
570-
length
571-
)
535+
logger.debug {
536+
"Train $number created: from ${timetable.getIn().getName()} to ${timetable.getOut().getName()}, length $length"
537+
}
572538
}
573539

574540
override fun distanceToSemaphore(): Double =
@@ -578,7 +544,7 @@ class Train :
578544
// zarazeni do fronty vstupniho bodu (simulace systemu sousedni stanice)
579545
val inout: InOut = timetable.getIn()
580546
val worker: InOutWorker = context.getWorkerFor(inout)
581-
logger.debug("Train {} approved for movement from {} to {}", number, inout.getName(), timetable.getOut().getName())
547+
logger.debug { "Train $number approved for movement from ${inout.getName()} to ${timetable.getOut().getName()}" }
582548
worker.enterTrain(this)
583549
context.report("approved ${inout.getName()}->${timetable.getOut().getName()}", this, ReportType.TRAIN_EVENTS)
584550

@@ -601,7 +567,7 @@ class Train :
601567
stop()
602568
motor.terminate()
603569
// ukoncovaci..
604-
logger.debug("Train {} completed journey: distance traveled {}", number, front.getTotalDistance())
570+
logger.debug { "Train $number completed journey: distance traveled ${front.getTotalDistance()}" }
605571
context.report("ends", this, ReportType.TRAIN_EVENTS)
606572
}
607573

0 commit comments

Comments
 (0)