Skip to content

Commit

Permalink
stateengine plugin: Introduce option to define se_log_level via an it…
Browse files Browse the repository at this point in the history
…em so it can be dynamically changed during operation. Further introduce startup_log_level for setting log level on initialization. Some further logging improvements
  • Loading branch information
onkelandy committed Aug 19, 2023
1 parent bce5bfc commit 7de7aeb
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 290 deletions.
197 changes: 62 additions & 135 deletions stateengine/StateEngineAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,22 @@ def update_webif_actionstatus(self, state, name, success, issue=None):
pass

# Write action to logger
def write_to_logger(self, log_level=StateEngineDefaults.log_level):
if log_level == -1:
self._log_info("name: {}", self._name)
else:
self._log_debug("name: {}", self._name)
self.__delay.write_to_logger(log_level)
def write_to_logger(self):
self._log_debug("name: {}", self._name)
self.__delay.write_to_logger()
if self.__repeat is not None:
self.__repeat.write_to_logger(log_level)
self.__repeat.write_to_logger()
if self.__instanteval is not None:
self.__instanteval.write_to_logger(log_level)
self.__instanteval.write_to_logger()
if self.conditionset is not None:
self.conditionset.write_to_logger(log_level)
self.conditionset.write_to_logger()
if self.previousconditionset is not None:
self.previousconditionset.write_to_logger(log_level)
self.previousconditionset.write_to_logger()
if self.previousstate_conditionset is not None:
self.previousstate_conditionset.write_to_logger(log_level)
self.previousstate_conditionset.write_to_logger()
if self.__mode is not None:
self.__mode.write_to_logger(log_level)
self.__order.write_to_logger(log_level)
self.__mode.write_to_logger()
self.__order.write_to_logger()

def set_source(self, current_condition, previous_condition, previousstate_condition):
source = []
Expand Down Expand Up @@ -485,25 +482,16 @@ def complete(self, item_state, evals_items=None):
self._caller += '_self'

# Write action to logger
def write_to_logger(self, log_level=StateEngineDefaults.log_level):
SeActionBase.write_to_logger(self, log_level)
def write_to_logger(self):
SeActionBase.write_to_logger(self)
if isinstance(self.__item, str):
if log_level == -1:
self._log_info("item from eval: {0}", self.__item)
else:
self._log_debug("item from eval: {0}", self.__item)
self._log_debug("item from eval: {0}", self.__item)
elif self.__item is not None:
if log_level == -1:
self._log_info("item: {0}", self.__item.property.path)
else:
self._log_debug("item: {0}", self.__item.property.path)
self._log_debug("item: {0}", self.__item.property.path)
if self.__status is not None:
if log_level == -1:
self._log_info("status: {0}", self.__status.property.path)
else:
self._log_debug("status: {0}", self.__status.property.path)
self.__mindelta.write_to_logger(log_level)
self.__value.write_to_logger(log_level)
self._log_debug("status: {0}", self.__status.property.path)
self.__mindelta.write_to_logger()
self.__value.write_to_logger()

# Check if execution is possible
def _can_execute(self, state):
Expand Down Expand Up @@ -612,17 +600,11 @@ def complete(self, item_state, evals_items=None):
self._scheduler_name = "{}-SeByAttrDelayTimer".format(self.__byattr)

# Write action to logger
def write_to_logger(self, log_level=StateEngineDefaults.log_level):
if log_level == -1:
self._log_info("function: {}", self.__function)
else:
self._log_debug("function: {}", self.__function)
SeActionBase.write_to_logger(self, log_level)
def write_to_logger(self):
self._log_debug("function: {}", self.__function)
SeActionBase.write_to_logger(self)
if self.__byattr is not None:
if log_level == -1:
self._log_info("set by attribute: {0}", self.__byattr)
else:
self._log_debug("set by attribute: {0}", self.__byattr)
self._log_debug("set by attribute: {0}", self.__byattr)

# Really execute the action
def real_execute(self, state, actionname: str, namevar: str = "", repeat_text: str = "", value=None, returnvalue=False, current_condition=None, previous_condition=None, previousstate_condition=None):
Expand Down Expand Up @@ -670,22 +652,13 @@ def complete(self, item_state, evals_items=None):
self._scheduler_name = "{}-SeLogicDelayTimer".format(self.__logic)

# Write action to logger
def write_to_logger(self, log_level=StateEngineDefaults.log_level):
if log_level == -1:
self._log_info("function: {}", self.__function)
else:
self._log_debug("function: {}", self.__function)
SeActionBase.write_to_logger(self, log_level)
def write_to_logger(self):
self._log_debug("function: {}", self.__function)
SeActionBase.write_to_logger(self)
if self.__logic is not None:
if log_level == -1:
self._log_info("trigger logic: {0}", self.__logic)
else:
self._log_debug("trigger logic: {0}", self.__logic)
self._log_debug("trigger logic: {0}", self.__logic)
if self.__value is not None:
if log_level == -1:
self._log_info("value: {0}", self.__value)
else:
self._log_debug("value: {0}", self.__value)
self._log_debug("value: {0}", self.__value)

# Really execute the action
def real_execute(self, state, actionname: str, namevar: str = "", repeat_text: str = "", value=None, returnvalue=False, current_condition=None, previous_condition=None, previousstate_condition=None):
Expand Down Expand Up @@ -739,17 +712,11 @@ def complete(self, item_state, evals_items=None):
self._scheduler_name = "{}-SeRunDelayTimer".format(StateEngineTools.get_eval_name(self.__eval))

# Write action to logger
def write_to_logger(self, log_level=StateEngineDefaults.log_level):
if log_level == -1:
self._log_info("function: {}", self.__function)
else:
self._log_debug("function: {}", self.__function)
SeActionBase.write_to_logger(self, log_level)
def write_to_logger(self):
self._log_debug("function: {}", self.__function)
SeActionBase.write_to_logger(self)
if self.__eval is not None:
if log_level == -1:
self._log_info("eval: {0}", StateEngineTools.get_eval_name(self.__eval))
else:
self._log_debug("eval: {0}", StateEngineTools.get_eval_name(self.__eval))
self._log_debug("eval: {0}", StateEngineTools.get_eval_name(self.__eval))

# Really execute the action
def real_execute(self, state, actionname: str, namevar: str = "", repeat_text: str = "", value=None, returnvalue=False, current_condition=None, previous_condition=None, previousstate_condition=None):
Expand Down Expand Up @@ -848,35 +815,19 @@ def complete(self, item_state, evals_items=None):
self._scheduler_name = "{}-SeItemDelayTimer".format(self.__item.property.path)

# Write action to logger
def write_to_logger(self, log_level=StateEngineDefaults.log_level):
if log_level == -1:
self._log_info("function: {}", self.__function)
self._log_info("value: {}", self.__value)
else:
self._log_debug("function: {}", self.__function)
self._log_debug("value: {}", self.__value)
SeActionBase.write_to_logger(self, log_level)
def write_to_logger(self):
self._log_debug("function: {}", self.__function)
self._log_debug("value: {}", self.__value)
SeActionBase.write_to_logger(self)
if isinstance(self.__item, str):
if log_level == -1:
self._log_info("item from eval: {0}", self.__item)
else:
self._log_debug("item from eval: {0}", self.__item)
self._log_debug("item from eval: {0}", self.__item)
elif self.__item is not None:
if log_level == -1:
self._log_info("item: {0}", self.__item.property.path)
else:
self._log_debug("item: {0}", self.__item.property.path)
self._log_debug("item: {0}", self.__item.property.path)
if self.__status is not None:
if log_level == -1:
self._log_info("status: {0}", self.__status.property.path)
else:
self._log_debug("status: {0}", self.__status.property.path)
self.__mindelta.write_to_logger(log_level)
self.__value.write_to_logger(log_level)
if log_level == -1:
self._log_info("force update: yes")
else:
self._log_debug("force update: yes")
self._log_debug("status: {0}", self.__status.property.path)
self.__mindelta.write_to_logger()
self.__value.write_to_logger()
self._log_debug("force update: yes")

# Check if execution is possible
def _can_execute(self, state):
Expand Down Expand Up @@ -1019,26 +970,14 @@ def complete(self, item_state, evals_items=None):
self._scheduler_name = "{}_{}-SeSpecialDelayTimer".format(self.__special, item)

# Write action to logger
def write_to_logger(self, log_level=StateEngineDefaults.log_level):
if log_level == -1:
self._log_info("function: {}", self.__function)
else:
self._log_debug("function: {}", self.__function)
SeActionBase.write_to_logger(self, log_level)
if log_level == -1:
self._log_info("Special Action: {0}", self.__special)
else:
self._log_debug("Special Action: {0}", self.__special)
def write_to_logger(self):
self._log_debug("function: {}", self.__function)
SeActionBase.write_to_logger(self)
self._log_debug("Special Action: {0}", self.__special)
if isinstance(self.__value, list):
if log_level == -1:
self._log_info("value: {0}", self.__value)
else:
self._log_debug("value: {0}", self.__value)
self._log_debug("value: {0}", self.__value)
else:
if log_level == -1:
self._log_info("Retrigger item: {0}", self.__value.property.path)
else:
self._log_debug("Retrigger item: {0}", self.__value.property.path)
self._log_debug("Retrigger item: {0}", self.__value.property.path)

# Really execute the action
def real_execute(self, state, actionname: str, namevar: str = "", repeat_text: str = "", value=None, returnvalue=False, current_condition=None, previous_condition=None, previousstate_condition=None):
Expand Down Expand Up @@ -1144,13 +1083,10 @@ def __init__(self, abitem, name: str):
def __repr__(self):
return "SeAction Add {}".format(self._name)

def write_to_logger(self, log_level=StateEngineDefaults.log_level):
if log_level == 1:
self._log_info("function: {}", self.__function)
else:
self._log_debug("function: {}", self.__function)
SeActionSetItem.write_to_logger(self, log_level)
SeActionBase.write_to_logger(self, log_level)
def write_to_logger(self):
self._log_debug("function: {}", self.__function)
SeActionSetItem.write_to_logger(self)
SeActionBase.write_to_logger(self)

def _execute_set_add_remove(self, state, actionname, namevar, repeat_text, item, value, current_condition=None, previous_condition=None, previousstate_condition=None):
value = value if isinstance(value, list) else [value]
Expand Down Expand Up @@ -1185,13 +1121,10 @@ def __init__(self, abitem, name: str):
def __repr__(self):
return "SeAction RemoveFirst {}".format(self._name)

def write_to_logger(self, log_level=StateEngineDefaults.log_level):
if log_level == -1:
self._log_info("function: {}", self.__function)
else:
self._log_debug("function: {}", self.__function)
SeActionSetItem.write_to_logger(self, log_level)
SeActionBase.write_to_logger(self, log_level)
def write_to_logger(self):
self._log_debug("function: {}", self.__function)
SeActionSetItem.write_to_logger(self)
SeActionBase.write_to_logger(self)

def _execute_set_add_remove(self, state, actionname, namevar, repeat_text, item, value, current_condition=None, previous_condition=None, previousstate_condition=None):
currentvalue = item.property.value
Expand Down Expand Up @@ -1232,13 +1165,10 @@ def __init__(self, abitem, name: str):
def __repr__(self):
return "SeAction RemoveLast {}".format(self._name)

def write_to_logger(self, log_level=StateEngineDefaults.log_level):
if log_level == -1:
self._log_info("function: {}", self.__function)
else:
self._log_debug("function: {}", self.__function)
SeActionSetItem.write_to_logger(self, log_level)
SeActionBase.write_to_logger(self, log_level)
def write_to_logger(self):
self._log_debug("function: {}", self.__function)
SeActionSetItem.write_to_logger(self)
SeActionBase.write_to_logger(self)

def _execute_set_add_remove(self, state, actionname, namevar, repeat_text, item, value, current_condition=None, previous_condition=None, previousstate_condition=None):
currentvalue = item.property.value
Expand Down Expand Up @@ -1281,13 +1211,10 @@ def __init__(self, abitem, name: str):
def __repr__(self):
return "SeAction RemoveAll {}".format(self._name)

def write_to_logger(self, log_level=StateEngineDefaults.log_level):
if log_level == -1:
self._log_info("function: {}", self.__function)
else:
self._log_debug("function: {}", self.__function)
SeActionSetItem.write_to_logger(self, log_level)
SeActionBase.write_to_logger(self, log_level)
def write_to_logger(self):
self._log_debug("function: {}", self.__function)
SeActionSetItem.write_to_logger(self)
SeActionBase.write_to_logger(self)

def _execute_set_add_remove(self, state, actionname, namevar, repeat_text, item, value, current_condition=None, previous_condition=None, previousstate_condition=None):
currentvalue = item.property.value
Expand Down
4 changes: 2 additions & 2 deletions stateengine/StateEngineActions.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def get(self):
return finalactions

# log all actions
def write_to_logger(self, log_level=StateEngineDefaults.log_level):
def write_to_logger(self):
actions = []
for name in self.__actions:
actions.append((self.__actions[name].get_order(), self.__actions[name]))
Expand All @@ -441,6 +441,6 @@ def write_to_logger(self, log_level=StateEngineDefaults.log_level):
self._log_info("Action '{0}':", action.name)
self._log_increase_indent()
self._abitem._initactionname = action.name
action.write_to_logger(log_level)
action.write_to_logger()
self._abitem._initactionname = None
self._log_decrease_indent()
18 changes: 9 additions & 9 deletions stateengine/StateEngineCondition.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def check(self, state):
return True

# Write condition to logger
def write_to_logger(self, log_level=StateEngineDefaults.log_level):
def write_to_logger(self):
if self.__error is not None:
self._log_warning("error: {0}", self.__error)
if self.__item is not None:
Expand All @@ -320,22 +320,22 @@ def write_to_logger(self, log_level=StateEngineDefaults.log_level):
self._log_info("eval: {0}", StateEngineTools.get_eval_name(e))
else:
self._log_info("eval: {0}", StateEngineTools.get_eval_name(self.__eval))
self.__value.write_to_logger(log_level)
self.__min.write_to_logger(log_level)
self.__max.write_to_logger(log_level)
self.__value.write_to_logger()
self.__min.write_to_logger()
self.__max.write_to_logger()
if self.__negate is not None:
self._log_info("negate: {0}", self.__negate)
self.__agemin.write_to_logger(log_level)
self.__agemax.write_to_logger(log_level)
self.__agemin.write_to_logger()
self.__agemax.write_to_logger()
if self.__agenegate is not None:
self._log_info("age negate: {0}", self.__agenegate)
self.__changedby.write_to_logger(log_level)
self.__changedby.write_to_logger()
if self.__changedbynegate is not None and not self.__changedby.is_empty():
self._log_info("changedby negate: {0}", self.__changedbynegate)
self.__updatedby.write_to_logger(log_level)
self.__updatedby.write_to_logger()
if self.__updatedbynegate is not None and not self.__updatedby.is_empty():
self._log_info("updatedby negate: {0}", self.__updatedbynegate)
self.__triggeredby.write_to_logger(log_level)
self.__triggeredby.write_to_logger()
if self.__updatedbynegate is not None and not self.__triggeredby.is_empty():
self._log_info("triggeredby negate: {0}", self.__triggeredbynegate)

Expand Down
4 changes: 2 additions & 2 deletions stateengine/StateEngineConditionSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ def complete(self, item_state):
del self.conditions[name]

# Write the whole condition set to the logger
def write_to_logger(self, log_level=StateEngineDefaults.log_level):
def write_to_logger(self):
for name in self.__conditions:
self._log_info("Condition '{0}':", name)
self._log_increase_indent()
self.__conditions[name].write_to_logger(log_level)
self.__conditions[name].write_to_logger()
self._log_decrease_indent()

def __currentconditionset_set(self, conditionsetid, name):
Expand Down
4 changes: 2 additions & 2 deletions stateengine/StateEngineConditionSets.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ def complete(self, item_state):
self.__condition_sets[name].complete(item_state)

# Write all condition sets to logger
def write_to_logger(self, log_level=StateEngineDefaults.log_level):
def write_to_logger(self):
for name in self.__condition_sets:
self._log_info("Condition Set '{0}':", name)
self._log_increase_indent()
self.__condition_sets[name].write_to_logger(log_level)
self.__condition_sets[name].write_to_logger()
self._log_decrease_indent()

# check if one of the conditions sets in the list is matching.
Expand Down
4 changes: 0 additions & 4 deletions stateengine/StateEngineDefaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

suspend_time = 3600

log_level = 0

suntracking_offset = 0

lamella_open_value = 0
Expand All @@ -40,8 +38,6 @@

logger = None

se_logger = logging.getLogger('stateengine')

log_maxage = 0


Expand Down
Loading

0 comments on commit 7de7aeb

Please sign in to comment.