From: Tom Hromatka Date: Fri, 21 Jun 2019 15:34:28 +0000 (-0600) Subject: tests: Add functional test logging class X-Git-Tag: v0.42.rc1~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dd324beed63e2e236f861224421e0d8f20128789;p=thirdparty%2Flibcgroup.git tests: Add functional test logging class This commit adds a Log() class for the functional test suite. This class provides several static methods for logging information during a test run. The data logged is often useful for debugging failed tests. Example usages: Log.log_debug('Running test %s' % test name) except Exception as e: Log.log_error(e) Both the log level and log file are configurable. By default, the functional tests will log all messages of criticality LOG_WARNING or higher to libcgroup-ftests.log. These settings can be overridden by the following command line options: -l LOGLEVEL, --loglevel LOGLEVEL -L LOGFILE, --logfile LOGFILE Signed-off-by: Tom Hromatka Reviewed-by: Dhaval Giani Signed-off-by: Dhaval Giani --- diff --git a/tests/ftests/consts.py b/tests/ftests/consts.py index 6c7db192..81223567 100644 --- a/tests/ftests/consts.py +++ b/tests/ftests/consts.py @@ -18,3 +18,10 @@ # You should have received a copy of the GNU Lesser General Public License # along with this library; if not, see . # + +DEFAULT_LOG_FILE = 'libcgroup-ftests.log' + +LOG_CRITICAL = 1 +LOG_WARNING = 5 +LOG_DEBUG = 8 +DEFAULT_LOG_LEVEL = 5 diff --git a/tests/ftests/log.py b/tests/ftests/log.py new file mode 100644 index 00000000..2b10d858 --- /dev/null +++ b/tests/ftests/log.py @@ -0,0 +1,56 @@ +# +# Log class for the libcgroup functional tests +# +# Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. +# Author: Tom Hromatka +# + +# +# This library is free software; you can redistribute it and/or modify it +# under the terms of version 2.1 of the GNU Lesser General Public License as +# published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this library; if not, see . +# + +import consts +import datetime +import log + +log_level = consts.DEFAULT_LOG_LEVEL +log_file = consts.DEFAULT_LOG_FILE +log_fd = None + + +class Log(object): + + @staticmethod + def log(msg, msg_level=consts.DEFAULT_LOG_LEVEL): + if log_level >= msg_level: + if log.log_fd is None: + Log.open_logfd(log.log_file) + + timestamp = datetime.datetime.now().strftime('%b %d %H:%M:%S') + log_fd.write("{}: {}\n".format(timestamp, msg)) + + @staticmethod + def open_logfd(log_file): + log.log_fd = open(log_file, "a") + + @staticmethod + def log_critical(msg): + Log.log("CRITICAL: {}".format(msg), consts.LOG_CRITICAL) + + @staticmethod + def log_warning(msg): + Log.log("WARNING: {}".format(msg), consts.LOG_WARNING) + + @staticmethod + def log_debug(msg): + Log.log("DEBUG: {}".format(msg), consts.LOG_DEBUG)