Skip to content

Commit abf0c85

Browse files
committed
Replace println with log4j
1 parent 75a6f9c commit abf0c85

File tree

9 files changed

+40
-22
lines changed

9 files changed

+40
-22
lines changed

src/main/kotlin/Main.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Checksum : Callable<Int> {
8282

8383
environmentParameters = processEnvironmentParameters(parameters)
8484
if(environmentParameters.isNotEmpty()){
85-
logger.warn("environment parameters: ${environmentParameters.toString()}")
85+
logger.info("environment parameters: ${environmentParameters.toString()}")
8686
}
8787

8888
//val fileContents = Files.readAllBytes(file.toPath())

src/main/kotlin/PlcSimulation.kt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import kotlinx.coroutines.*
22
import operations.*
3+
import org.slf4j.LoggerFactory
34
import java.lang.Float
45
import java.rmi.NotBoundException
56
import java.util.*
@@ -16,6 +17,11 @@ class PlcSimulation(
1617
val addOperation = AddOperation(configurationParser.getConfiguredDevice().configuration, memory, parameters)
1718
val setOperation = SetOperation(configurationParser.getConfiguredDevice().configuration, memory, parameters)
1819
val subOperation = SubOperation(configurationParser.getConfiguredDevice().configuration, memory, parameters)
20+
21+
companion object {
22+
val logger = LoggerFactory.getLogger("PlcSimulation")
23+
}
24+
1925
init {
2026
var simulationConfiguration = configurationParser.getConfiguredDevice().simulation
2127
var configuration = configurationParser.getConfiguredDevice().configuration
@@ -34,9 +40,9 @@ class PlcSimulation(
3440
}
3541
}
3642
} catch (e: Exception) {
37-
println("Exception - ${e.message}")
43+
logger.error("Exception - ${e.message}")
3844
} finally {
39-
println("Job: Finally block, cleaning up resources")
45+
logger.error("Job: Finally block, cleaning up resources")
4046
}
4147
}
4248

@@ -53,7 +59,7 @@ class PlcSimulation(
5359
}
5460

5561
is Delay -> {
56-
println("Delay value ${element.value}")
62+
logger.info("Delay value ${element.value}")
5763
delay(element.value.toLong())
5864
}
5965

@@ -85,7 +91,7 @@ class PlcSimulation(
8591

8692

8793
suspend fun ifEqual(element: IfEqual, configuration: Configuration, memory: PlcMemory) {
88-
println("IfEqual symbol ${element.symbol} value ${element.value}")
94+
logger.info("IfEqual symbol ${element.symbol} value ${element.value}")
8995
var value = processValue(element.value)
9096
var variable = configuration.registers.getVarConfiguration(element.symbol)
9197
if (variable == null) {
@@ -113,11 +119,11 @@ class PlcSimulation(
113119
processOperationElement(subElement, configuration, memory)
114120
}
115121
}else {
116-
println("ERROR: Symbol ${element.symbol} has invalid datatype during IfEqual execution")
122+
logger.error("Symbol ${element.symbol} has invalid datatype during IfEqual execution")
117123
}
118124

119125
}else {
120-
println("ERROR: Symbol ${element.symbol} not found during IfEqual execution")
126+
logger.error("Symbol ${element.symbol} not found during IfEqual execution")
121127
throw CancellationException("Error - IfEqual")
122128
}
123129
} else {
@@ -132,7 +138,7 @@ class PlcSimulation(
132138
if (variable.datatype == "FLOAT32") {
133139
var currentValue = memory.readHoldingRegister(variable.address.toInt(), 2)
134140
if(currentValue.isEmpty()){
135-
println("ERROR: Add Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
141+
logger.error("Add Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
136142
throw CancellationException("Error - Add")
137143
}
138144
val intValue = (( currentValue[1].toInt() shl 16) or (currentValue[0].toInt() and 0xFFFF))
@@ -153,7 +159,7 @@ class PlcSimulation(
153159
} else {
154160
var currentValue = memory.readHoldingRegister(variable.address.toInt(), 1)
155161
if(currentValue.isEmpty()){
156-
println("ERROR: IfEqual Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
162+
logger.error("IfEqual Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
157163
throw CancellationException("Error - IfEqual")
158164
}
159165
//compare
@@ -172,7 +178,7 @@ class PlcSimulation(
172178
AddressType.INPUT_REGISTER -> {
173179
val currentValue = memory.readInputRegister(variable.address.toInt(), 1)
174180
if(currentValue.isEmpty()){
175-
println("ERROR: IfEqual Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
181+
logger.error("IfEqual Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
176182
throw CancellationException("Error - IfEqual")
177183
}
178184
//compare
@@ -188,11 +194,11 @@ class PlcSimulation(
188194
}
189195
AddressType.COIL -> {
190196
if ( (element.value != "0" && element.value != "1")) {
191-
println("ERROR: Invalid value format on IfEqual. Symbol ${element.symbol} is of BOOL type and supports only values 0 or 1 not ${element.value} for comparison")
197+
logger.error("Invalid value format on IfEqual. Symbol ${element.symbol} is of BOOL type and supports only values 0 or 1 not ${element.value} for comparison")
192198
}
193199
var currentValue = memory.readCoilStatus(variable.address.toInt(), 2)
194200
if(currentValue.isEmpty()){
195-
println("ERROR: IfEqual Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
201+
logger.error("IfEqual Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
196202
throw CancellationException("Error - IfEqual")
197203
}
198204
//compare

src/main/kotlin/operations/AddOperation.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,27 @@ import Configuration
44
import PlcMemory
55
import Add
66
import EnvironmentVariables
7+
import org.slf4j.LoggerFactory
78
import java.util.concurrent.CancellationException
89
import toBooleanFromBinary
910

1011
class AddOperation(private val configuration: Configuration,private val memory: PlcMemory, environmentVariables: EnvironmentVariables
1112
) : BaseOperation(environmentVariables, configuration) {
13+
14+
companion object {
15+
val logger = LoggerFactory.getLogger("AddOperation")
16+
}
17+
1218
fun addOperation(element: Add){
13-
println("Add symbol ${element.symbol} value ${element.value}")
19+
logger.info("Add symbol ${element.symbol} value ${element.value}")
1420
var value = processValue(element.value)
1521
var variable = configuration.registers.getVarConfiguration(element.symbol)
1622
if (variable == null) {
17-
println("ERROR: Symbol ${element.symbol} not found during Set execution")
23+
logger.error("Symbol ${element.symbol} not found during Set execution")
1824
throw CancellationException("Error - Add")
1925
} else {
2026
if(variable.addressType == AddressType.COIL || variable.addressType == AddressType.DISCRETE_INPUT){
21-
println("ERROR: Symbol ${element.symbol} is of type COIL or DISCRETE_INPUT which is not support by Add operation")
27+
logger.error("Symbol ${element.symbol} is of type COIL or DISCRETE_INPUT which is not support by Add operation")
2228
throw CancellationException("Error - Add")
2329
}
2430

@@ -32,7 +38,7 @@ class AddOperation(private val configuration: Configuration,private val memory:
3238
if (variable.datatype == "FLOAT32") {
3339
var currentValue = memory.readHoldingRegister(variable.address.toInt(), 2)
3440
if(currentValue.isEmpty()){
35-
println("ERROR: Add Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
41+
logger.error("Add Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
3642
throw CancellationException("Error - Add")
3743
}
3844
val intValue = (( currentValue[1].toInt() shl 16) or (currentValue[0].toInt() and 0xFFFF))
@@ -43,7 +49,7 @@ class AddOperation(private val configuration: Configuration,private val memory:
4349
} else {
4450
var currentValue = memory.readHoldingRegister(variable.address.toInt(), 1)
4551
if(currentValue.isEmpty()){
46-
println("ERROR: Add Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
52+
logger.error("Add Operation - Unable to get value of ${element.symbol} address ${variable.address} ")
4753
throw CancellationException("Error - Add")
4854
}
4955
var intValue = value.toInt()

src/main/kotlin/operations/CsvOperation.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Csv
55
import PlcMemory
66
import org.apache.commons.csv.CSVFormat
77
import org.apache.commons.csv.CSVParser
8+
import org.slf4j.LoggerFactory
89
import java.io.FileReader
910
import java.nio.file.Paths
1011
import java.util.concurrent.CancellationException
@@ -13,6 +14,9 @@ data class CsvOperationInfo(var position: Int, var csvColumn: List<String>)
1314
class CsvOperation {
1415
private var csvVariables: MutableMap<String, CsvOperationInfo> = mutableMapOf<String, CsvOperationInfo>()
1516

17+
companion object {
18+
val logger = LoggerFactory.getLogger("CsvOperation")
19+
}
1620
@Throws(InternalError::class)
1721
private fun parseCsv(csvFileName: String, column: Int) : List<String> {
1822
val columnValues = mutableListOf<String>()
@@ -50,9 +54,10 @@ class CsvOperation {
5054
}
5155
fun process(element: Csv, configuration: Configuration, memory: PlcMemory) {
5256
var nextValue = getNextValue(element)
57+
AddOperation.logger.info("Csv symbol ${element.symbol} value $nextValue")
5358
var variable = configuration.registers.getVarConfiguration(element.symbol)
5459
if (variable == null) {
55-
println("ERROR: Symbol ${element.symbol} not found during CSV execution")
60+
logger.error("Symbol ${element.symbol} not found during CSV execution")
5661
throw CancellationException("Error - CSV")
5762
} else {
5863
when (variable.addressType) {

src/main/kotlin/operations/LinearOperation.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class LinearOperation {
4141

4242
fun process(element: Linear, configuration: Configuration, memory: PlcMemory) {
4343
var nextValue = getNextValue(element)
44+
logger.info("Linear symbol ${element.symbol} value $nextValue")
4445
var variable = configuration.registers.getVarConfiguration(element.symbol)
4546
if (variable == null) {
4647
logger.error("Symbol ${element.symbol} not found during Linear execution")

src/main/kotlin/operations/RandomOperation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import java.util.concurrent.CancellationException
1010

1111
fun randomOperation(element: Random, configuration: Configuration, memory: PlcMemory){
1212
val logger = LoggerFactory.getLogger("randomOperation")
13-
logger.debug("Random symbol ${element.symbol} value min ${element.valueMin} value max ${element.valueMax}")
13+
logger.info("Random symbol ${element.symbol} value min ${element.valueMin} value max ${element.valueMax}")
1414
var variable = configuration.registers.getVarConfiguration(element.symbol)
1515
if (variable == null) {
1616
logger.error("Symbol ${element.symbol} not found during Set execution")

src/main/kotlin/operations/SetOperation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SetOperation(private val configuration: Configuration,private val memory:
1515
val logger = LoggerFactory.getLogger("SetOperation")
1616
}
1717
fun setOperation(element: Set) {
18-
logger.debug("Set symbol ${element.symbol} value ${element.value}")
18+
logger.info("Set symbol ${element.symbol} value ${element.value}")
1919
var value = processValue(element.value)
2020
var variable = configuration.registers.getVarConfiguration(element.symbol)
2121
if (variable == null) {

src/main/kotlin/operations/SubOperation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class SubOperation(private val configuration: Configuration,private val memory:
1818
val logger = LoggerFactory.getLogger("SubOperation")
1919
}
2020
fun subOperation(element: Sub) {
21-
logger.debug("Sub symbol ${element.symbol} value ${element.value}")
21+
logger.info("Sub symbol ${element.symbol} value ${element.value}")
2222
var value = processValue(element.value)
2323
var variable = configuration.registers.getVarConfiguration(element.symbol)
2424
if (variable == null) {

src/main/resources/log4j2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<!-- Console appender configuration -->
77
<Console name="console" target="SYSTEM_OUT">
88
<PatternLayout
9-
pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
9+
pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n" />
1010
</Console>
1111
</Appenders>
1212
<Loggers>

0 commit comments

Comments
 (0)