Skip to content

Commit

Permalink
Merge pull request #701 from smeup/bugfix/LS25000432/fix-logging-cont…
Browse files Browse the repository at this point in the history
…ext-scopes

Bugfix/ls25000432/fix logging context scopes
  • Loading branch information
lanarimarco authored Feb 4, 2025
2 parents 8c9755c + a665374 commit 4451181
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.smeup.rpgparser.interpreter

import com.smeup.rpgparser.execution.MainExecutionContext
import com.smeup.rpgparser.logging.LogChannel
import com.smeup.rpgparser.logging.ProgramUsageType
import com.smeup.rpgparser.parsing.ast.*
import com.smeup.rpgparser.parsing.parsetreetoast.LogicalCondition
Expand Down Expand Up @@ -536,6 +537,12 @@ class ExpressionEvaluation(
val callback = MainExecutionContext.getConfiguration().jarikoCallback
val trace = JarikoTrace(JarikoTraceKind.FunctionCall, functionToCall)
callback.traceBlock(trace) {
val source: LogSourceProvider = {
LogSourceData(MainExecutionContext.getExecutionProgramName(), expression.startLine())
}
val entry = LogEntry(source, LogChannel.RESOLUTION.getPropertyName())
val logRenderer = LazyLogEntry(entry) { sep -> "FUNCTION$sep$functionToCall" }
MainExecutionContext.log(logRenderer)
val function = systemInterface.findFunction(interpreterStatus.symbolTable, functionToCall)
?: throw RuntimeException("Function $functionToCall cannot be found (${expression.position.line()})")
val functionWrapper = FunctionWrapper(function = function, functionName = functionToCall, expression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.smeup.rpgparser.interpreter

import com.smeup.rpgparser.execution.MainExecutionContext
import com.smeup.rpgparser.parsing.ast.CompilationUnit
import com.smeup.rpgparser.parsing.ast.DataWrapUpChoice
import com.smeup.rpgparser.parsing.parsetreetoast.error
import com.smeup.rpgparser.parsing.parsetreetoast.resolveAndValidate
import com.smeup.rpgparser.parsing.parsetreetoast.todo
Expand Down Expand Up @@ -208,13 +209,31 @@ private class FunctionInterpreter(systemInterface: SystemInterface, private val

private val staticMemorySliceId = MemorySliceId(
activationGroup = "*STATIC",
programName = "FunctionInterpreter.$procedureName.static")
programName = functionName
)

private val functionName: String get() = "FunctionInterpreter.$procedureName.static"

override fun getMemorySliceId(): MemorySliceId? {
val memorySliceId = super.getMemorySliceId()
return memorySliceId?.copy(programName = "${memorySliceId.programName}.$procedureName")
}

override fun getInterpretationContext(): InterpretationContext {
return object : InterpretationContext {
private var iDataWrapUpChoice: DataWrapUpChoice? = null
override val currentProgramName: String
get() = functionName

override fun shouldReinitialize() = false
override var dataWrapUpChoice: DataWrapUpChoice?
get() = iDataWrapUpChoice
set(value) {
iDataWrapUpChoice = value
}
}
}

/**
* This way I can avoid to persist the memory slice of the function
* */
Expand Down Expand Up @@ -261,4 +280,4 @@ private class FunctionInterpreter(systemInterface: SystemInterface, private val
private fun isFirstExecution(): Boolean {
return MainExecutionContext.getAttributes()[staticMemorySliceId.getAttributeKey()] == null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,16 @@ open class InternalInterpreter(
return indicators
}

private var interpretationContext: InterpretationContext = DummyInterpretationContext
/**
* NOTE: This should never be accessed as is. Is [getInterpretationContext] to get its value.
*/
private var _interpretationContext: InterpretationContext = DummyInterpretationContext
override fun getInterpretationContext(): InterpretationContext {
return interpretationContext
return _interpretationContext
}

fun setInterpretationContext(interpretationContext: InterpretationContext) {
this.interpretationContext = interpretationContext
this._interpretationContext = interpretationContext
}

private val klists = HashMap<String, List<String>>()
Expand Down Expand Up @@ -167,7 +170,7 @@ open class InternalInterpreter(
"${value.render()} cannot be assigned to ${data.name} of type ${data.type}"
}

val programName = interpretationContext.currentProgramName
val programName = getInterpretationContext().currentProgramName

renderLogInternal {
val logSource = { LogSourceData(programName, data.startLine()) }
Expand Down Expand Up @@ -248,7 +251,7 @@ open class InternalInterpreter(
) {
val callback = configuration.jarikoCallback
val initTrace = JarikoTrace(JarikoTraceKind.SymbolTable, "INIT")
val programName = interpretationContext.currentProgramName
val programName = getInterpretationContext().currentProgramName
val logSourceProducer = { LogSourceData(programName = programName, line = compilationUnit.startLine()) }

callback.traceBlock(initTrace) {
Expand Down Expand Up @@ -521,7 +524,7 @@ open class InternalInterpreter(

if (elapsed.inWholeMilliseconds > compilationUnit.minTimeOut!!) {
throw InterpreterTimeoutException(
interpretationContext.currentProgramName,
getInterpretationContext().currentProgramName,
elapsed.inWholeMilliseconds,
compilationUnit.minTimeOut!!
)
Expand Down Expand Up @@ -599,7 +602,7 @@ open class InternalInterpreter(
}

private fun executeWithMute(statement: Statement) {
val programName = interpretationContext.currentProgramName
val programName = getInterpretationContext().currentProgramName
renderLogInternal {
val logSource = { LogSourceData(programName, statement.position.line()) }
LazyLogEntry.produceLine(logSource)
Expand Down Expand Up @@ -694,7 +697,7 @@ open class InternalInterpreter(
compilationUnit: CompilationUnit,
line: String
) {
val programName = interpretationContext.currentProgramName
val programName = getInterpretationContext().currentProgramName
muteAnnotations.forEach {
it.resolveAndValidate(compilationUnit)
when (it) {
Expand Down Expand Up @@ -723,7 +726,7 @@ open class InternalInterpreter(
systemInterface.addExecutedAnnotation(
it.position!!.start.line,
MuteComparisonAnnotationExecuted(
this.interpretationContext.currentProgramName,
this.getInterpretationContext().currentProgramName,
exp,
it.val1,
it.val2,
Expand All @@ -743,7 +746,7 @@ open class InternalInterpreter(
systemInterface.addExecutedAnnotation(
it.position!!.start.line,
MuteTimeoutAnnotationExecuted(
this.interpretationContext.currentProgramName,
this.getInterpretationContext().currentProgramName,
it.timeout,
line
)
Expand All @@ -759,7 +762,7 @@ open class InternalInterpreter(
systemInterface.addExecutedAnnotation(
it.position!!.start.line,
MuteFailAnnotationExecuted(
this.interpretationContext.currentProgramName,
this.getInterpretationContext().currentProgramName,
message,
line
)
Expand Down Expand Up @@ -857,7 +860,7 @@ open class InternalInterpreter(
}

private fun errorDescription(statement: Statement, throwable: Throwable) =
"Program ${interpretationContext.currentProgramName} - ${statement.simpleDescription()} ${throwable.message}"
"Program ${getInterpretationContext().currentProgramName} - ${statement.simpleDescription()} ${throwable.message}"

override fun fillDataFrom(dbFile: EnrichedDBFile, record: Record) {
if (!record.isEmpty()) {
Expand Down Expand Up @@ -906,7 +909,7 @@ open class InternalInterpreter(
val value = this[dataDefinition]
if (value is NumberValue) {
val newValue = value.increment(amount)
val programName = this.interpretationContext.currentProgramName
val programName = this.getInterpretationContext().currentProgramName
renderLogInternal {
val logSource = { LogSourceData(programName, dataDefinition.startLine()) }
LazyLogEntry.produceData(logSource, dataDefinition, newValue, value)
Expand Down Expand Up @@ -935,7 +938,7 @@ open class InternalInterpreter(
else -> expression.evalWith(expressionEvaluation)
}

val programName = this.interpretationContext.currentProgramName
val programName = this.getInterpretationContext().currentProgramName
val sourceProvider = { LogSourceData(programName, expression.startLine()) }
renderLogInternal { LazyLogEntry.produceExpression(sourceProvider, expression, value) }

Expand Down Expand Up @@ -1042,7 +1045,7 @@ open class InternalInterpreter(

renderLogInternal {
val logSource =
{ LogSourceData(interpretationContext.currentProgramName, target.array.startLine()) }
{ LogSourceData(getInterpretationContext().currentProgramName, target.array.startLine()) }
LazyLogEntry.produceAssignmentOfElement(logSource, target.array, index, value)
}

Expand Down Expand Up @@ -1226,15 +1229,15 @@ open class InternalInterpreter(
val associatedActivationGroup = MainExecutionContext.getProgramStack().peek()?.activationGroup
val activationGroup = associatedActivationGroup?.assignedName
return configuration.jarikoCallback.getActivationGroup.invoke(
interpretationContext.currentProgramName, associatedActivationGroup
getInterpretationContext().currentProgramName, associatedActivationGroup
)?.assignedName ?: activationGroup
}
}
}

open fun getMemorySliceId(): MemorySliceId? {
return getActivationGroupAssignedName()?.let {
MemorySliceId(activationGroup = it, interpretationContext.currentProgramName)
MemorySliceId(activationGroup = it, getInterpretationContext().currentProgramName)
}
}

Expand Down Expand Up @@ -1267,7 +1270,7 @@ open class InternalInterpreter(
val exitRT = isRTOn && (isLROn == null || !isLROn)

return configuration.jarikoCallback.exitInRT.invoke(
interpretationContext.currentProgramName
getInterpretationContext().currentProgramName
) ?: exitRT
}

Expand Down Expand Up @@ -1306,7 +1309,7 @@ open class InternalInterpreter(
* Execute a statement keeping track of its state for observability purposes
*/
private inline fun execute(statement: Statement) {
val programName = this.interpretationContext.currentProgramName
val programName = this.getInterpretationContext().currentProgramName
val sourceProducer = if (logsEnabled()) {
{ LogSourceData(programName, statement.position.line()) }
} else null
Expand Down
Loading

0 comments on commit 4451181

Please sign in to comment.