Skip to content

Commit

Permalink
Merge pull request #3782 from pevogam/logfile-unification
Browse files Browse the repository at this point in the history
Unify all logfile handling into utils_logfile
  • Loading branch information
luckyh authored Dec 11, 2023
2 parents 7050669 + 04a89c0 commit 24b13b1
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 104 deletions.
3 changes: 2 additions & 1 deletion avocado_vt/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from virttest import funcatexit
from virttest import utils_env
from virttest import utils_params
from virttest import utils_logfile
from virttest import utils_misc
from virttest import version
from virttest._wrappers import load_source
Expand Down Expand Up @@ -111,7 +112,7 @@ def __init__(self, **kwargs):
self.__vt_params = vt_params
self.debugdir = self.logdir
self.resultsdir = self.logdir
utils_misc.set_log_file_dir(self.logdir)
utils_logfile.set_log_file_dir(self.logdir)
self.__status = None
self.__exc_info = None

Expand Down
3 changes: 3 additions & 0 deletions virttest/env_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from virttest import migration
from virttest import utils_kernel_module
from virttest import arch
from virttest import utils_logfile
from virttest.utils_conn import SSHConnection
from virttest.utils_version import VersionInterval
from virttest.staging import service
Expand Down Expand Up @@ -630,6 +631,8 @@ def postprocess_vm(test, params, env, name):
except Exception:
pass

utils_logfile.close_log_file()

if params.get("vm_extra_dump_paths") is not None:
vm_extra_dumps = os.path.join(test.outputdir, "vm_extra_dumps")
if not os.path.exists(vm_extra_dumps):
Expand Down
2 changes: 1 addition & 1 deletion virttest/ip_sniffing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import six

from virttest.utils_misc import log_line
from virttest.utils_logfile import log_line
from virttest.utils_version import VersionInterval

LOG = logging.getLogger('avocado.' + __name__)
Expand Down
12 changes: 7 additions & 5 deletions virttest/libvirt_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from avocado.core import exceptions

from virttest import error_context
from virttest import utils_logfile
from virttest import utils_misc
from virttest import cpu
from virttest import virt_vm
Expand Down Expand Up @@ -1455,9 +1456,10 @@ def _create_serial_console(self):
cmd += (" console %s %s" % (self.name, self.serial_ports[0]))
except IndexError:
raise virt_vm.VMConfigMissingError(self.name, "serial")
output_func = utils_misc.log_line # Because qemu-kvm uses this
output_func = utils_logfile.log_line # Because qemu-kvm uses this
port = self.serial_ports[0]
# Because qemu-kvm hard-codes this
output_filename = self.get_serial_console_filename(self.serial_ports[0])
output_filename = self.get_serial_console_filename(port)
output_params = (output_filename,)
prompt = self.params.get("shell_prompt", "[\#\$]")
LOG.debug("Command used to create serial console: %s", cmd)
Expand All @@ -1467,10 +1469,10 @@ def _create_serial_console(self):
prompt=prompt)
if not self.serial_console.is_alive():
LOG.error("Failed to create serial_console")
# Cause serial_console.close() to close open log file
self.serial_console.set_log_file(output_filename)
self.serial_console_log = os.path.join(utils_misc.get_log_file_dir(),
self.serial_console_log = os.path.join(utils_logfile.get_log_file_dir(),
output_filename)
# Cause serial_console.close() to close open log file
self.serial_console.close_hooks += [utils_logfile.close_own_log_file(self.serial_console_log)]

def set_root_serial_console(self, device, remove=False):
"""
Expand Down
3 changes: 2 additions & 1 deletion virttest/qemu_devices/qcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
# Internal imports
from virttest import vt_iothread
from virttest import utils_qemu
from virttest import utils_logfile
from virttest import utils_misc
from virttest import arch, storage, data_dir, virt_vm
from virttest import qemu_storage
Expand Down Expand Up @@ -3193,7 +3194,7 @@ def tpm_define_by_params(self, name, params):

def _handle_log(line):
try:
utils_misc.log_line('%s_swtpm_setup.log' % name, line)
utils_logfile.log_line('%s_swtpm_setup.log' % name, line)
except Exception as e:
LOG.warn("Can't log %s_swtpm_setup output: %s.", name, e)

Expand Down
5 changes: 3 additions & 2 deletions virttest/qemu_devices/qdevices.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import aexpect

from virttest import qemu_monitor, utils_numeric
from virttest import utils_logfile
from virttest import utils_misc
from virttest.qemu_devices.utils import DeviceError
from virttest.qemu_devices.utils import none_or_int
Expand Down Expand Up @@ -2082,7 +2083,7 @@ def _handle_log(self, line):
"""Handle the log of virtiofs daemon."""
name = self.get_param('name')
try:
utils_misc.log_line('%s-%s.log' % (self.get_qid(), name), line)
utils_logfile.log_line('%s-%s.log' % (self.get_qid(), name), line)
except Exception as e:
LOG.warn("Can't log %s-%s, output: '%s'.", self.get_qid(), name, e)

Expand Down Expand Up @@ -2155,7 +2156,7 @@ def start_daemon(self):
if self.get_param('version') in ('2.0', ):
tpm_cmd += ' --tpm2'

log_dir = utils_misc.get_log_file_dir()
log_dir = utils_logfile.get_log_file_dir()
log_file = os.path.join(log_dir, '%s_swtpm.log' % self.get_qid())
Path(log_file).touch(mode=0o644, exist_ok=True)
tpm_cmd += ' --log file=%s' % log_file
Expand Down
3 changes: 2 additions & 1 deletion virttest/qemu_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import aexpect
from avocado.utils import process

from virttest import utils_logfile
from virttest import utils_misc
from virttest import error_context

Expand All @@ -27,7 +28,7 @@ def __init__(self, test, params, image_name, blkdebug_cfg="",
self.type = ""
if log_filename:
log_filename += "-" + utils_misc.generate_random_string(4)
self.output_func = utils_misc.log_line
self.output_func = utils_logfile.log_line
self.output_params = (log_filename,)
else:
self.output_func = None
Expand Down
4 changes: 2 additions & 2 deletions virttest/qemu_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import six

from . import utils_logfile
from . import utils_misc
from . import cartesian_config
from . import data_dir
Expand Down Expand Up @@ -449,8 +450,7 @@ def _log_lines(self, log_str):
raise MonitorLockError("Could not acquire exclusive lock to access"
" %s" % self.open_log_files)
try:
log_file_dir = utils_misc.get_log_file_dir()
log = utils_misc.get_path(log_file_dir, self.log_file)
log = utils_logfile.get_log_filename(self.log_file)
timestr = time.strftime("%Y-%m-%d %H:%M:%S")
try:
if log not in self.open_log_files:
Expand Down
10 changes: 6 additions & 4 deletions virttest/qemu_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import six
from six.moves import xrange

from virttest import utils_logfile
from virttest import utils_misc
from virttest import utils_qemu
from virttest import cpu
Expand Down Expand Up @@ -2921,7 +2922,7 @@ def _create_serial_console(self):
self.serial_console = aexpect.ShellSession(
"nc -U %s" % file_name,
auto_close=False,
output_func=utils_misc.log_line,
output_func=utils_logfile.log_line,
output_params=(log_name,),
prompt=self.params.get("shell_prompt", "[\#\$]"),
status_test_command=self.params.get("status_test_command",
Expand Down Expand Up @@ -3520,13 +3521,14 @@ def create(self, name=None, params=None, root_dir=None,
self.create_serial_console()

for key, value in list(self.logs.items()):
outfile = "%s-%s.log" % (key, name)
outfile = os.path.join(utils_logfile.get_log_file_dir(),
"%s-%s.log" % (key, name))
self.logsessions[key] = aexpect.Tail(
"nc -U %s" % value,
auto_close=False,
output_func=utils_misc.log_line,
output_func=utils_logfile.log_line,
output_params=(outfile,))
self.logsessions[key].set_log_file(outfile)
self.logsessions[key].close_hooks += [utils_logfile.close_own_log_file(outfile)]

# Wait for IO channels setting up completely,
# such as serial console.
Expand Down
2 changes: 1 addition & 1 deletion virttest/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def remote_commander(client, host, port, username, password, prompt,
log_file = utils_logfile.get_log_filename(log_filename)
session.set_output_func(utils_logfile.log_line)
session.set_output_params((log_file,))
session.set_log_file(os.path.basename(log_file))
session.close_hooks += [utils_logfile.close_own_log_file(log_file)]

session.send_ctrl("raw")
# Wrap io interfaces.
Expand Down
3 changes: 2 additions & 1 deletion virttest/test_setup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from virttest import data_dir
from virttest import error_context
from virttest import cpu
from virttest import utils_logfile
from virttest import utils_misc
from virttest import versionable_class
from virttest import openvswitch
Expand Down Expand Up @@ -1647,7 +1648,7 @@ def sr_iov_setup(self):
file_name = "host_dmesg_after_load_%s.txt" % self.driver
LOG.info("Log dmesg after loading '%s' to '%s'.", self.driver,
file_name)
utils_misc.log_line(file_name, dmesg)
utils_logfile.log_line(file_name, dmesg)
self.setup = None
return True

Expand Down
33 changes: 24 additions & 9 deletions virttest/utils_logfile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""
Control log file utility functions.
An easy way to log lines to files when the logging system can't be used
An easy way to log lines to files when the logging system can't be used.
Naive module that keeps tacks of some opened files and somehow manages them.
:copyright: 2020 Red Hat Inc.
"""
Expand All @@ -13,7 +16,6 @@
from avocado.core import exceptions
from avocado.utils import aurl
from avocado.utils import path as utils_path
from aexpect.utils.genio import _open_log_files
from avocado.utils.astring import string_safe_encode

from virttest import data_dir
Expand All @@ -23,6 +25,9 @@
_log_file_dir = data_dir.get_tmp_dir()
_log_lock = threading.RLock()

# File descriptor dictionary for all open log files
_open_log_files = {} # pylint: disable=C0103


def _acquire_lock(lock, timeout=10):
"""
Expand Down Expand Up @@ -137,9 +142,9 @@ def get_log_filename(filename):
os.path.abspath(utils_path.get_path(_log_file_dir, filename)))


def close_log_file(filename):
def close_log_file(filename="*"):
"""
Close the log file
Close log files with the same base name as filename or all by default.
:param filename: Log file name
:raise: LogLockError if the lock is unavailable
Expand All @@ -150,13 +155,23 @@ def close_log_file(filename):
raise LogLockError("Could not acquire exclusive lock to access"
" _open_log_files")
try:
for k in _open_log_files:
if os.path.basename(k) == filename:
f = _open_log_files[k]
f.close()
remove.append(k)
for log_file, log_fd in _open_log_files.items():
if (filename == '*' or
os.path.basename(log_file) == os.path.basename(filename)):
log_fd.close()
remove.append(log_file)
if remove:
for key_to_remove in remove:
_open_log_files.pop(key_to_remove)

finally:
_log_lock.release()


def close_own_log_file(log_file):
"""Closing hook for sessions with log_file managed locally."""

def hook(self):
close_log_file(log_file)

return hook
Loading

0 comments on commit 24b13b1

Please sign in to comment.