Mirror of the lttng-dev mailing list
 help / color / mirror / Atom feed
From: simon.marchi@polymtl.ca (simon.marchi@polymtl.ca)
Subject: [lttng-dev] [PATCH] Add --simple option to the list command
Date: Fri, 22 Jun 2012 02:29:27 -0400	[thread overview]
Message-ID: <1340346567-11748-1-git-send-email-simon.marchi@polymtl.ca> (raw)

From: Simon Marchi <simon.marchi@polymtl.ca>

The --simple option makes "list" print one item per line, a nice format to be
parsed by scripts. For example, the bash completion script could benefit from
this feature. For now, only session listing is affected, but it would be
useful to have other kind of listing behave the same way.

Also left some comments on my path.

Signed-off-by: Simon Marchi <simark at nova.polymtl.ca>
---
 src/bin/lttng/commands/list.c |   41 ++++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 13 deletions(-)

diff --git a/src/bin/lttng/commands/list.c b/src/bin/lttng/commands/list.c
index 966be2d..cb4fb58 100644
--- a/src/bin/lttng/commands/list.c
+++ b/src/bin/lttng/commands/list.c
@@ -30,6 +30,7 @@ static int opt_kernel;
 static char *opt_channel;
 static int opt_domain;
 static int opt_fields;
+static int simple;
 #if 0
 /* Not implemented yet */
 static char *opt_cmd_name;
@@ -44,6 +45,7 @@ enum {
 	OPT_HELP = 1,
 	OPT_USERSPACE,
 	OPT_LIST_OPTIONS,
+	OPT_SIMPLE,
 };
 
 static struct lttng_handle *handle;
@@ -63,6 +65,7 @@ static struct poptOption long_options[] = {
 	{"domain",    'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0},
 	{"fields",    'f', POPT_ARG_VAL, &opt_fields, 1, 0, 0},
 	{"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
+	{"simple", 0, POPT_ARG_NONE, &simple, 0, NULL, NULL},
 	{0, 0, 0, 0, 0, 0, 0}
 };
 
@@ -80,6 +83,8 @@ static void usage(FILE *ofp)
 	fprintf(ofp, "\n");
 	fprintf(ofp, "  -h, --help              Show this help\n");
 	fprintf(ofp, "      --list-options      Simple listing of options\n");
+	fprintf(ofp, "      --simple            Prints one item per line, suitable "
+	                                       "for parsing\n");
 	fprintf(ofp, "  -k, --kernel            Select kernel domain\n");
 	fprintf(ofp, "  -u, --userspace         Select user-space domain.\n");
 	fprintf(ofp, "  -f, --fields            List event fields.\n");
@@ -566,33 +571,43 @@ static int list_sessions(const char *session_name)
 	count = lttng_list_sessions(&sessions);
 	DBG("Session count %d", count);
 	if (count < 0) {
+		/* Some error happened */
 		ret = count;
 		goto error;
 	} else if (count == 0) {
-		MSG("Currently no available tracing session");
+		/* No session available */
+		if (!simple) {
+			MSG("Currently no available tracing session");
+		}
 		goto end;
 	}
 
-	if (session_name == NULL) {
+	if (session_name == NULL && !simple) {
 		MSG("Available tracing sessions:");
 	}
 
 	for (i = 0; i < count; i++) {
 		if (session_name != NULL) {
+			/* We want to list info only about a particular session */
 			if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
 				session_found = 1;
-				MSG("Tracing session %s:%s", session_name, active_string(sessions[i].enabled));
-				MSG("%sTrace path: %s\n", indent4, sessions[i].path);
+
+				if (simple) {
+					MSG("%s", session_name);
+				} else {
+					MSG("Tracing session %s:%s", session_name, active_string(sessions[i].enabled));
+					MSG("%sTrace path: %s\n", indent4, sessions[i].path);
+				}
 				break;
 			}
-			continue;
-		}
-
-		MSG("  %d) %s (%s)%s", i + 1, sessions[i].name, sessions[i].path,
-				active_string(sessions[i].enabled));
-
-		if (session_found) {
-			break;
+		} else {
+			/* We want to list all sessions */
+			if (simple) {
+				MSG("%s", sessions[i].name);
+			} else {
+				MSG("  %d) %s (%s)%s", i + 1, sessions[i].name, sessions[i].path,
+					active_string(sessions[i].enabled));
+			}
 		}
 	}
 
@@ -604,7 +619,7 @@ static int list_sessions(const char *session_name)
 		goto error;
 	}
 
-	if (session_name == NULL) {
+	if (session_name == NULL && !simple) {
 		MSG("\nUse lttng list <session_name> for more details");
 	}
 
-- 
1.7.10




                 reply	other threads:[~2012-06-22  6:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1340346567-11748-1-git-send-email-simon.marchi@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox