Rename cmd.h for command.h
[lttng-tools.git] / src / bin / lttng / commands / create.c
CommitLineData
f3ed775e
DG
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
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
f3ed775e
DG
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
c399183f 29#include "../command.h"
f3ed775e
DG
30
31static char *opt_output_path;
32static char *opt_session_name;
33
34enum {
35 OPT_HELP = 1,
36};
37
38static 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},
42 {0, 0, 0, 0, 0, 0, 0}
43};
44
45/*
46 * usage
47 */
48static void usage(FILE *ofp)
49{
50 fprintf(ofp, "usage: lttng create [options] [NAME]\n");
51 fprintf(ofp, "\n");
52 fprintf(ofp, " -h, --help Show this help\n");
58a97671 53 fprintf(ofp, " -o, --output PATH Specify output path for traces\n");
f3ed775e
DG
54 fprintf(ofp, "\n");
55}
56
57/*
58 * create_session
59 *
60 * Create a tracing session. If no name specified, a default name will be
61 * generated.
62 */
63static int create_session()
64{
31e43e7f 65 int ret, have_name = 0;
d6175221 66 char datetime[16];
58a97671 67 char *session_name, *traces_path = NULL, *alloc_path = NULL;
f3ed775e
DG
68 time_t rawtime;
69 struct tm *timeinfo;
70
d6175221
DG
71 /* Get date and time for automatic session name/path */
72 time(&rawtime);
73 timeinfo = localtime(&rawtime);
74 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
75
f3ed775e
DG
76 /* Auto session name creation */
77 if (opt_session_name == NULL) {
d6175221
DG
78 ret = asprintf(&session_name, "auto-%s", datetime);
79 if (ret < 0) {
80 perror("asprintf session name");
81 goto error;
82 }
f3ed775e
DG
83 DBG("Auto session name set to %s", session_name);
84 } else {
85 session_name = opt_session_name;
31e43e7f 86 have_name = 1;
f3ed775e
DG
87 }
88
89 /* Auto output path */
90 if (opt_output_path == NULL) {
58a97671 91 alloc_path = strdup(config_get_default_path());
f3ed775e 92 if (alloc_path == NULL) {
b082db07
DG
93 ERR("Home path not found.\n \
94 Please specify an output path using -o, --output PATH");
f3ed775e
DG
95 ret = CMD_FATAL;
96 goto error;
97 }
f3ed775e 98
f2e0110c
DG
99 if (have_name) {
100 ret = asprintf(&traces_path, "%s/" LTTNG_DEFAULT_TRACE_DIR_NAME
101 "/%s-%s", alloc_path, session_name, datetime);
102 } else {
103 ret = asprintf(&traces_path, "%s/" LTTNG_DEFAULT_TRACE_DIR_NAME
104 "/%s", alloc_path, session_name);
105 }
106
58a97671
DG
107 if (ret < 0) {
108 perror("asprintf trace dir name");
109 goto error;
110 }
111 } else {
112 traces_path = opt_output_path;
f3ed775e
DG
113 }
114
58a97671 115 ret = lttng_create_session(session_name, traces_path);
f3ed775e
DG
116 if (ret < 0) {
117 goto error;
118 }
119
58a97671
DG
120 /* Init lttng session config */
121 ret = config_init(session_name);
f3ed775e 122 if (ret < 0) {
e0ac0dd5
DG
123 if (ret == -1) {
124 ret = CMD_ERROR;
125 }
f3ed775e
DG
126 goto error;
127 }
128
129 MSG("Session %s created.", session_name);
0052ffe6 130 MSG("Traces will be written in %s" , traces_path);
f3ed775e
DG
131
132 ret = CMD_SUCCESS;
133
134error:
135 if (alloc_path) {
136 free(alloc_path);
137 }
138
58a97671
DG
139 if (traces_path) {
140 free(traces_path);
f3ed775e
DG
141 }
142 return ret;
143}
144
145/*
146 * cmd_list
147 *
148 * The 'list <options>' first level command
149 */
150int cmd_create(int argc, const char **argv)
151{
152 int opt, ret = CMD_SUCCESS;
153 static poptContext pc;
154
155 pc = poptGetContext(NULL, argc, argv, long_options, 0);
156 poptReadDefaultConfig(pc, 0);
157
158 while ((opt = poptGetNextOpt(pc)) != -1) {
159 switch (opt) {
160 case OPT_HELP:
161 usage(stderr);
162 goto end;
163 default:
164 usage(stderr);
165 ret = CMD_UNDEFINED;
166 goto end;
167 }
168 }
169
170 opt_session_name = (char*) poptGetArg(pc);
171
172 ret = create_session();
173
174end:
175 return ret;
176}
This page took 0.031537 seconds and 4 git commands to generate.