2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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
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.
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.
25 #include <sys/types.h>
29 #include "../command.h"
31 static char *opt_output_path
;
32 static char *opt_session_name
;
38 static struct poptOption long_options
[] = {
39 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
40 {"help", 'h', POPT_ARG_NONE
, 0, OPT_HELP
, 0, 0},
41 {"output", 'o', POPT_ARG_STRING
, &opt_output_path
, 0, 0, 0},
48 static void usage(FILE *ofp
)
50 fprintf(ofp
, "usage: lttng create [options] [NAME]\n");
52 fprintf(ofp
, " -h, --help Show this help\n");
53 fprintf(ofp
, " -o, --output PATH Specify output path for traces\n");
60 * Create a tracing session. If no name specified, a default name will be
63 static int create_session()
65 int ret
, have_name
= 0;
67 char *session_name
, *traces_path
= NULL
, *alloc_path
= NULL
;
71 /* Get date and time for automatic session name/path */
73 timeinfo
= localtime(&rawtime
);
74 strftime(datetime
, sizeof(datetime
), "%Y%m%d-%H%M%S", timeinfo
);
76 /* Auto session name creation */
77 if (opt_session_name
== NULL
) {
78 ret
= asprintf(&session_name
, "auto-%s", datetime
);
80 perror("asprintf session name");
83 DBG("Auto session name set to %s", session_name
);
85 session_name
= opt_session_name
;
89 /* Auto output path */
90 if (opt_output_path
== NULL
) {
91 alloc_path
= strdup(config_get_default_path());
92 if (alloc_path
== NULL
) {
93 ERR("Home path not found.\n \
94 Please specify an output path using -o, --output PATH");
100 ret
= asprintf(&traces_path
, "%s/" LTTNG_DEFAULT_TRACE_DIR_NAME
101 "/%s-%s", alloc_path
, session_name
, datetime
);
103 ret
= asprintf(&traces_path
, "%s/" LTTNG_DEFAULT_TRACE_DIR_NAME
104 "/%s", alloc_path
, session_name
);
108 perror("asprintf trace dir name");
112 traces_path
= opt_output_path
;
115 ret
= lttng_create_session(session_name
, traces_path
);
120 /* Init lttng session config */
121 ret
= config_init(session_name
);
129 MSG("Session %s created.", session_name
);
130 MSG("Traces will be written in %s" , traces_path
);
148 * The 'list <options>' first level command
150 int cmd_create(int argc
, const char **argv
)
152 int opt
, ret
= CMD_SUCCESS
;
153 static poptContext pc
;
155 pc
= poptGetContext(NULL
, argc
, argv
, long_options
, 0);
156 poptReadDefaultConfig(pc
, 0);
158 while ((opt
= poptGetNextOpt(pc
)) != -1) {
170 opt_session_name
= (char*) poptGetArg(pc
);
172 ret
= create_session();