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