From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35299) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1abnka-0006Co-Gw for qemu-devel@nongnu.org; Fri, 04 Mar 2016 06:18:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1abnkY-0003sb-V1 for qemu-devel@nongnu.org; Fri, 04 Mar 2016 06:18:28 -0500 Received: from mail-wm0-x22e.google.com ([2a00:1450:400c:c09::22e]:35945) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1abnkY-0003sU-Hm for qemu-devel@nongnu.org; Fri, 04 Mar 2016 06:18:26 -0500 Received: by mail-wm0-x22e.google.com with SMTP id n186so30041933wmn.1 for ; Fri, 04 Mar 2016 03:18:26 -0800 (PST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= Date: Fri, 4 Mar 2016 11:18:06 +0000 Message-Id: <1457090287-8021-9-git-send-email-alex.bennee@linaro.org> In-Reply-To: <1457090287-8021-1-git-send-email-alex.bennee@linaro.org> References: <1457090287-8021-1-git-send-email-alex.bennee@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [Qemu-devel] [PATCH v8 8/9] qemu-log: support simple pid substitution for logs List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, crosthwaitepeter@gmail.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= , aurelien@aurel32.net, rth@twiddle.net When debugging stuff that occurs over several forks it would be useful not to keep overwriting the one logfile you've set-up. This allows a simple %d to be included once in the logfile parameter which is substituted with getpid(). As the test cases involve checking user output they need g_test_trap_subprocess() support. As a result they are currently skipped on Travis builds due to the older glib involved. Signed-off-by: Alex Bennée Reviewed-by: Leandro Dorileo Reviewed-by: Aurelien Jarno Reviewed-by: Richard Henderson --- v5 - add another r-b v7 - simpler error check as suggested by Eric Blake - don't g_error, just error_report (so we don't crash from monitor) - add some unit tests v8 - added rth's r-b tag - tweak subprocess tests --- tests/test-logging.c | 36 +++++++++++++++++++++++++++++++++++- util/log.c | 22 ++++++++++++++++++++-- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/tests/test-logging.c b/tests/test-logging.c index 11e20d0..afb1473 100644 --- a/tests/test-logging.c +++ b/tests/test-logging.c @@ -89,7 +89,37 @@ static void test_parse_zero_range(void) g_test_trap_assert_stdout(""); g_test_trap_assert_stderr("*Failed to parse range in: 0x1000+0\n"); } -#endif + +/* As the only real failure from a bad log filename path spec is + * reporting to the user we have to use the g_test_trap_subprocess + * mechanism and check no errors reported on stderr. + */ +static void test_parse_path_subprocess(void) +{ + /* All these should work without issue */ + qemu_set_log_filename("/tmp/qemu.log"); + qemu_set_log_filename("/tmp/qemu-%d.log"); + qemu_set_log_filename("/tmp/qemu.log.%d"); +} +static void test_parse_path(void) +{ + g_test_trap_subprocess ("/logging/parse_path/subprocess", 0, 0); + g_test_trap_assert_passed(); + g_test_trap_assert_stdout(""); + g_test_trap_assert_stderr(""); +} +static void test_parse_invalid_path_subprocess(void) +{ + qemu_set_log_filename("/tmp/qemu-%d%d.log"); +} +static void test_parse_invalid_path(void) +{ + g_test_trap_subprocess ("/logging/parse_invalid_path/subprocess", 0, 0); + g_test_trap_assert_passed(); + g_test_trap_assert_stdout(""); + g_test_trap_assert_stderr("Bad logfile format: /tmp/qemu-%d%d.log\n"); +} +#endif /* CONFIG_HAS_GLIB_SUBPROCESS_TESTS */ int main(int argc, char **argv) { @@ -101,6 +131,10 @@ int main(int argc, char **argv) g_test_add_func("/logging/parse_invalid_range", test_parse_invalid_range); g_test_add_func("/logging/parse_zero_range/subprocess", test_parse_zero_range_subprocess); g_test_add_func("/logging/parse_zero_range", test_parse_zero_range); + g_test_add_func("/logging/parse_path", test_parse_path); + g_test_add_func("/logging/parse_path/subprocess", test_parse_path_subprocess); + g_test_add_func("/logging/parse_invalid_path", test_parse_invalid_path); + g_test_add_func("/logging/parse_invalid_path/subprocess", test_parse_invalid_path_subprocess); #endif return g_test_run(); diff --git a/util/log.c b/util/log.c index 09ac3fd..2553acb 100644 --- a/util/log.c +++ b/util/log.c @@ -21,6 +21,7 @@ #include "qemu-common.h" #include "qemu/log.h" #include "qemu/range.h" +#include "qemu/error-report.h" #include "trace/control.h" static char *logfilename; @@ -84,11 +85,28 @@ void do_qemu_set_log(int log_flags, bool use_own_buffers) qemu_log_close(); } } - +/* + * Allow the user to include %d in their logfile which will be + * substituted with the current PID. This is useful for debugging many + * nested linux-user tasks but will result in lots of logs. + */ void qemu_set_log_filename(const char *filename) { + char *pidstr; g_free(logfilename); - logfilename = g_strdup(filename); + + pidstr = strstr(filename, "%"); + if (pidstr) { + /* We only accept one %d, no other format strings */ + if (pidstr[1] != 'd' || strchr(pidstr + 2, '%')) { + error_report("Bad logfile format: %s", filename); + logfilename = NULL; + } else { + logfilename = g_strdup_printf(filename, getpid()); + } + } else { + logfilename = g_strdup(filename); + } qemu_log_close(); qemu_set_log(qemu_loglevel); } -- 2.7.2