diff mbox

[v5,5/9] qemu-log: support simple pid substitution in logfile

Message ID 1454597781-18115-6-git-send-email-alex.bennee@linaro.org (mailing list archive)
State New, archived
Headers show

Commit Message

Alex Bennée Feb. 4, 2016, 2:56 p.m. UTC
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().

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Leandro Dorileo <l@dorileo.org>
Reviewed-by: Aurelien Jarno <aurelien@aurel32.net>

---
v5
  - add another r-b
---
 qemu-log.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

Comments

Richard Henderson Feb. 4, 2016, 10:26 p.m. UTC | #1
On 02/05/2016 01:56 AM, Alex Bennée wrote:
> +    if (g_strrstr(filename, "%d")) {
> +        /* if we are going to format this we'd better validate first */
> +        if (g_regex_match_simple("^[^%]+%d[^%]+$", filename, 0, 0)) {

Why g_strrstr instead of strstr?  There should be only one, so why look for the 
last?


r~
Eric Blake Feb. 4, 2016, 11:32 p.m. UTC | #2
On 02/04/2016 03:26 PM, Richard Henderson wrote:
> On 02/05/2016 01:56 AM, Alex Bennée wrote:
>> +    if (g_strrstr(filename, "%d")) {
>> +        /* if we are going to format this we'd better validate first */
>> +        if (g_regex_match_simple("^[^%]+%d[^%]+$", filename, 0, 0)) {
> 
> Why g_strrstr instead of strstr?  There should be only one, so why look
> for the last?

For that matter, why use a heavyweight regex, when you can achieve the
same validation with the faster:

char *tmp = strchr(filename, '%');
if (tmp) {
    if (tmp[1] != 'd' || strchr(tmp + 2, '%')) {
        ...report invalid string
    }
Alex Bennée Feb. 5, 2016, 1:34 p.m. UTC | #3
Eric Blake <eblake@redhat.com> writes:

> On 02/04/2016 03:26 PM, Richard Henderson wrote:
>> On 02/05/2016 01:56 AM, Alex Bennée wrote:
>>> +    if (g_strrstr(filename, "%d")) {
>>> +        /* if we are going to format this we'd better validate first */
>>> +        if (g_regex_match_simple("^[^%]+%d[^%]+$", filename, 0, 0)) {
>>
>> Why g_strrstr instead of strstr?  There should be only one, so why look
>> for the last?

Yeah, my fault for using the glib functions, I guess
g_strstr_len(filename, -1, "%d") would be the glib equivalent for
strstr.

> For that matter, why use a heavyweight regex, when you can achieve the
> same validation with the faster:
>
> char *tmp = strchr(filename, '%');
> if (tmp) {
>     if (tmp[1] != 'd' || strchr(tmp + 2, '%')) {
>         ...report invalid string
>     }

For option parsing I'm not too worried about speed. At least a regex
gives the explicit format we expect (for those that read regex). I guess
I can do it manually if preferred.

--
Alex Bennée
diff mbox

Patch

diff --git a/qemu-log.c b/qemu-log.c
index 8c59063..2eebac0 100644
--- a/qemu-log.c
+++ b/qemu-log.c
@@ -70,11 +70,24 @@  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)
 {
     g_free(logfilename);
-    logfilename = g_strdup(filename);
+    if (g_strrstr(filename, "%d")) {
+        /* if we are going to format this we'd better validate first */
+        if (g_regex_match_simple("^[^%]+%d[^%]+$", filename, 0, 0)) {
+            logfilename = g_strdup_printf(filename, getpid());
+        } else {
+            g_error("Bad logfile format: %s", filename);
+        }
+    } else {
+        logfilename = g_strdup(filename);
+    }
     qemu_log_close();
     qemu_set_log(qemu_loglevel);
 }