From: Lucas Meneghel Rodrigues Subject: [PATCH 01/13] KVM test: kvm_utils.py: add a primitive logging mechanism for kvm_subprocess Date: Mon, 21 Jun 2010 22:52:13 -0300 Message-ID: <1277171545-23003-1-git-send-email-lmr@redhat.com> Cc: kvm@vger.kernel.org, Michael Goldish To: autotest@test.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:27273 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752568Ab0FVBwb (ORCPT ); Mon, 21 Jun 2010 21:52:31 -0400 Sender: kvm-owner@vger.kernel.org List-ID: From: Michael Goldish Add log_line() which logs a single line to a given file. The file's path is given relative to a certain base dir. Add set_log_dir() which sets the base dir. This is useful for logging the output of kvm_subprocess. kvm_subprocess can take a callback function, which it calls with each line of output it gets from the running subprocess. Redirecting kvm_subprocess's output to the regular log files is done by passing it logging.debug or logging.info. However, in order to log to other files, we'd have to pass kvm_subprocess a custom logger method, e.g. our_custom_logger.debug. Unfortunately, such methods (called instancemethods) cannot be pickled, and kvm_subprocess relies on pickling. This patch offers an easy yet somewhat dirty solution to the problem. Signed-off-by: Michael Goldish --- client/tests/kvm/kvm_utils.py | 37 +++++++++++++++++++++++++++++++++++++ 1 files changed, 37 insertions(+), 0 deletions(-) diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py index 040124b..367e1e5 100644 --- a/client/tests/kvm/kvm_utils.py +++ b/client/tests/kvm/kvm_utils.py @@ -733,6 +733,43 @@ def find_free_ports(start_port, end_port, count): return ports +# An easy way to log lines to files when the logging system can't be used + +_open_log_files = {} +_log_file_dir = "/tmp" + + +def log_line(filename, line): + """ + Write a line to a file. '\n' is appended to the line. + + @param filename: Path of file to write to, either absolute or relative to + the dir set by set_log_file_dir(). + @param line: Line to write. + """ + global _open_log_files, _log_file_dir + if filename not in _open_log_files: + path = get_path(_log_file_dir, filename) + try: + os.makedirs(os.path.dirname(path)) + except OSError: + pass + _open_log_files[filename] = open(path, "w") + timestr = time.strftime("%Y-%m-%d %H:%M:%S") + _open_log_files[filename].write("%s: %s\n" % (timestr, line)) + _open_log_files[filename].flush() + + +def set_log_file_dir(dir): + """ + Set the base directory for log files created by log_line(). + + @param dir: Directory for log files. + """ + global _log_file_dir + _log_file_dir = dir + + # The following are miscellaneous utility functions. def get_path(base_path, user_path): -- 1.7.0.1