Add a '--list-options' option to each command.
[lttng-tools.git] / src / bin / lttng / commands / create.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <time.h>
27 #include <unistd.h>
28
29 #include "../command.h"
30 #include "../utils.h"
31
32 static char *opt_output_path;
33 static char *opt_session_name;
34
35 enum {
36 OPT_HELP = 1,
37 OPT_LIST_OPTIONS,
38 };
39
40 static struct poptOption long_options[] = {
41 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
42 {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
43 {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
44 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
45 {0, 0, 0, 0, 0, 0, 0}
46 };
47
48 /*
49 * usage
50 */
51 static void usage(FILE *ofp)
52 {
53 fprintf(ofp, "usage: lttng create [options] [NAME]\n");
54 fprintf(ofp, "\n");
55 fprintf(ofp, " -h, --help Show this help\n");
56 fprintf(ofp, " --list-options Simple listing of options\n");
57 fprintf(ofp, " -o, --output PATH Specify output path for traces\n");
58 fprintf(ofp, "\n");
59 }
60
61 /*
62 * create_session
63 *
64 * Create a tracing session. If no name specified, a default name will be
65 * generated.
66 */
67 static int create_session()
68 {
69 int ret, have_name = 0;
70 char datetime[16];
71 char *session_name, *traces_path = NULL, *alloc_path = NULL;
72 time_t rawtime;
73 struct tm *timeinfo;
74
75 /* Get date and time for automatic session name/path */
76 time(&rawtime);
77 timeinfo = localtime(&rawtime);
78 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
79
80 /* Auto session name creation */
81 if (opt_session_name == NULL) {
82 ret = asprintf(&session_name, "auto-%s", datetime);
83 if (ret < 0) {
84 perror("asprintf session name");
85 goto error;
86 }
87 DBG("Auto session name set to %s", session_name);
88 } else {
89 session_name = opt_session_name;
90 have_name = 1;
91 }
92
93 /* Auto output path */
94 if (opt_output_path == NULL) {
95 alloc_path = strdup(config_get_default_path());
96 if (alloc_path == NULL) {
97 ERR("Home path not found.\n \
98 Please specify an output path using -o, --output PATH");
99 ret = CMD_FATAL;
100 goto error;
101 }
102
103 if (have_name) {
104 ret = asprintf(&traces_path, "%s/" LTTNG_DEFAULT_TRACE_DIR_NAME
105 "/%s-%s", alloc_path, session_name, datetime);
106 } else {
107 ret = asprintf(&traces_path, "%s/" LTTNG_DEFAULT_TRACE_DIR_NAME
108 "/%s", alloc_path, session_name);
109 }
110
111 if (ret < 0) {
112 perror("asprintf trace dir name");
113 goto error;
114 }
115 } else {
116 traces_path = opt_output_path;
117 }
118
119 ret = lttng_create_session(session_name, traces_path);
120 if (ret < 0) {
121 goto error;
122 }
123
124 /* Init lttng session config */
125 ret = config_init(session_name);
126 if (ret < 0) {
127 if (ret == -1) {
128 ret = CMD_ERROR;
129 }
130 goto error;
131 }
132
133 MSG("Session %s created.", session_name);
134 MSG("Traces will be written in %s" , traces_path);
135
136 ret = CMD_SUCCESS;
137
138 error:
139 if (alloc_path) {
140 free(alloc_path);
141 }
142
143 if (traces_path) {
144 free(traces_path);
145 }
146 return ret;
147 }
148
149 /*
150 * cmd_list
151 *
152 * The 'list <options>' first level command
153 */
154 int cmd_create(int argc, const char **argv)
155 {
156 int opt, ret = CMD_SUCCESS;
157 static poptContext pc;
158
159 pc = poptGetContext(NULL, argc, argv, long_options, 0);
160 poptReadDefaultConfig(pc, 0);
161
162 while ((opt = poptGetNextOpt(pc)) != -1) {
163 switch (opt) {
164 case OPT_HELP:
165 usage(stderr);
166 goto end;
167 case OPT_LIST_OPTIONS:
168 list_cmd_options(stdout, long_options);
169 ret = CMD_SUCCESS;
170 goto end;
171 default:
172 usage(stderr);
173 ret = CMD_UNDEFINED;
174 goto end;
175 }
176 }
177
178 opt_session_name = (char*) poptGetArg(pc);
179
180 ret = create_session();
181
182 end:
183 return ret;
184 }
This page took 0.045757 seconds and 4 git commands to generate.