From patchwork Fri Mar 4 11:18:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 8502771 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 4E8809F38C for ; Fri, 4 Mar 2016 11:20:17 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7ED86201F2 for ; Fri, 4 Mar 2016 11:20:15 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8D70C20160 for ; Fri, 4 Mar 2016 11:20:14 +0000 (UTC) Received: from localhost ([::1]:40353 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1abnmH-0000O3-Qk for patchwork-qemu-devel@patchwork.kernel.org; Fri, 04 Mar 2016 06:20:13 -0500 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) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IOrXCgtB1Iwap+WS273Cbm5lK1qK6Qce9GvEtRzvmms=; b=FjHWTsipBOQH5CFpDBAVcDjfkGY2rTNyJ3zkFOzCWDA0fUQtDATZabknWJyvGiE4fG l1MAepV8XHFQp0hHQ0c5hask0RDxul9xZ0hkUzJj4/5K4gXqkzlD5mONxC5E2f+Xuq82 ZDySHjIBRoAAF3X3JNOsbULbAvct58/jxMzrE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IOrXCgtB1Iwap+WS273Cbm5lK1qK6Qce9GvEtRzvmms=; b=GXrS9UrGR+HWGWBzgKq7NN0kRCGidrW7TZv3RWSSZT2mYG3AtRd5OmvQDz0ji1B9z/ JEFe7ZogQLi0KfwMncwwecYORRdnNEQ91eLVsdev7tV4OG7E/j+yDL47rozZk0fkboE8 hQbzcDqL//nUXlLRxacWIrWXVHeO4s3f9uIhVOsL1Rqn+x2xNiK0axSrlGxTdcdfA1W7 1kE3954dyfxvEph83mv1aHuR9IKVb6oVcIFu1yHG8M4Fxd/S3tWjpKkN4jLR63IwDRzH 3kD83JOIVeGElN1ukEQfwT4lcomZvWdm96PH8eqBpyNfcS1t7DM0BxQJ+tSpoGf+PO7Z XDDQ== X-Gm-Message-State: AD7BkJKMEp04QJSP4p9B12ZVmL6hBelGyokwm5tuTL+C654gSnyO3tne+pbZtg8f6Uc7zwUk X-Received: by 10.28.54.229 with SMTP id y98mr4751622wmh.89.1457090306039; Fri, 04 Mar 2016 03:18:26 -0800 (PST) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id g3sm2840902wjw.31.2016.03.04.03.18.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Mar 2016 03:18:20 -0800 (PST) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id CF40E3E027F; Fri, 4 Mar 2016 11:18:13 +0000 (GMT) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Date: Fri, 4 Mar 2016 11:18:06 +0000 Message-Id: <1457090287-8021-9-git-send-email-alex.bennee@linaro.org> X-Mailer: git-send-email 2.7.2 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 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::22e Cc: pbonzini@redhat.com, crosthwaitepeter@gmail.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= , aurelien@aurel32.net, rth@twiddle.net Subject: [Qemu-devel] [PATCH v8 8/9] qemu-log: support simple pid substitution for logs X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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); }