From: Ondrej Lichtner <olichtne(a)redhat.com>
Somwhere during the changes to the logging I created a bug that makes it
that multiple recipes are logged into incorrect directories, getting
further into the folder hierarchy. A related problem that occured is
that the logs specific to a recipe would also contain the logs of all
recipes executed after it.
This patch fixes that by saving the paths and then restoring them when
moving on to the next recipe.
The bug was created by moving code from the LoggingServer to the
NetTestController. The LoggingServer had one static variable as the base
for the paths, however after moving it to the controller the variable is
no longer static and is changed by every slave we are connecting to.
This results in the next recipe being logged in the directory for the
last connected slave of the previous recipe.
Signed-off-by: Ondrej Lichtner <olichtne(a)redhat.com>
---
lnst-ctl | 4 ++++
lnst/Common/Logs.py | 36 +++++++++++++++++++++++++++---------
2 files changed, 31 insertions(+), 9 deletions(-)
diff --git a/lnst-ctl b/lnst-ctl
index 2ce304f..8504ae1 100755
--- a/lnst-ctl
+++ b/lnst-ctl
@@ -140,6 +140,7 @@ def main():
summary = []
res_serializer = NetTestResultSerializer()
+ Logs.save_state()
for recipe_path in args:
if os.path.isdir(recipe_path):
all_files = []
@@ -151,6 +152,7 @@ def main():
for f in all_files:
recipe_file = os.path.join(recipe_path, f)
if re.match(r'^.*\.xml$', recipe_file):
+ Logs.reset_state()
logging.info("Processing recipe file \"%s\"" %
recipe_file)
summary.append(get_recipe_result(action, recipe_file,
cleanup,
@@ -159,11 +161,13 @@ def main():
config))
Logs.set_logging_root_path(clean=False)
else:
+ Logs.reset_state()
summary.append(get_recipe_result(action, recipe_path,
cleanup, res_serializer,
packet_capture,
config))
+ Logs.reset_state()
Logs.set_logging_root_path(clean=False)
print_summary(summary)
diff --git a/lnst/Common/Logs.py b/lnst/Common/Logs.py
index a9910eb..e9c33ea 100644
--- a/lnst/Common/Logs.py
+++ b/lnst/Common/Logs.py
@@ -151,11 +151,12 @@ class Logs:
file_handlers = []
formatter = None
logFolder = None
- logger = None
+ loggers = []
root_path = None
debug = None
date = None
nameExtend = None
+ state = None
@classmethod
def __init__(cls,debug=0, waitForNet=False, logger=logging.getLogger(),
recipe_path=None, to_display=True, date=None,
@@ -175,7 +176,7 @@ class Logs:
cls.logFolder = log_folder
else:
cls.logFolder = os.path.join(os.path.dirname(sys.argv[0]), './Logs')
- cls.logger = logger
+ cls.loggers.append(logger)
cls.debug = debug
if date is None:
cls.date = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
@@ -186,6 +187,23 @@ class Logs:
recipe_path, to_display)
@classmethod
+ def save_state(cls):
+ cls.state = {"logFolder": cls.logFolder, "date": cls.date}
+
+ @classmethod
+ def reset_state(cls):
+ cls.logFolder = cls.state["logFolder"]
+ cls.date = cls.state["date"]
+
+ for logger in cls.loggers:
+ handlers = list(logger.handlers)
+ for handler in handlers:
+ if type(handler) == logging.FileHandler:
+ logger.removeHandler(handler)
+
+ cls.loggers = cls.loggers[:1]
+
+ @classmethod
def clean_root_log_folder(cls, logRootPath):
try:
shutil.rmtree(logRootPath)
@@ -204,8 +222,6 @@ class Logs:
file_info.setFormatter(cls.formatter)
file_info.setLevel(logging.INFO)
- cls.file_handlers.append(file_debug)
- cls.file_handlers.append(file_info)
return (file_debug, file_info)
@@ -217,15 +233,17 @@ class Logs:
if recipe_path is None:
recipe_path = ""
- root_logger = cls.logger
+ root_logger = cls.loggers[-1]
recipe_name = os.path.splitext(os.path.split(recipe_path)[1])[0]
cls.root_path = os.path.join(cls.logFolder, cls.date+cls.nameExtend,
recipe_name)
if (clean):
cls.clean_root_log_folder(cls.root_path)
- for fhandler in cls.file_handlers:
- root_logger.removeHandler(fhandler)
- del cls.file_handlers[:]
+
+ handlers = list(root_logger.handlers)
+ for handler in handlers:
+ if type(handler) == logging.FileHandler:
+ logger.removeHandler(handler)
(file_debug, file_info) = cls._create_file_handler(cls.root_path)
root_logger.addHandler(file_debug)
@@ -249,7 +267,7 @@ class Logs:
@param debug: If True print to terminal debug level of logging messages.
"""
- root_logger = cls.logger
+ root_logger = cls.loggers[-1]
if to_display:
display = logging.StreamHandler()
display.setFormatter(cls.formatter)
--
1.7.11.7