Commit | Line | Data |
---|---|---|
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" |
679b4943 | 30 | #include "../utils.h" |
f3ed775e DG |
31 | |
32 | static char *opt_output_path; | |
33 | static char *opt_session_name; | |
34 | ||
35 | enum { | |
36 | OPT_HELP = 1, | |
679b4943 | 37 | OPT_LIST_OPTIONS, |
f3ed775e DG |
38 | }; |
39 | ||
40 | static struct poptOption long_options[] = { | |
41 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
679b4943 SM |
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}, | |
f3ed775e DG |
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"); | |
679b4943 | 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 | /* | |
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 | { | |
31e43e7f | 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) { | |
d6175221 DG |
82 | ret = asprintf(&session_name, "auto-%s", datetime); |
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; | |
31e43e7f | 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) { |
b082db07 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 | |
f2e0110c | 103 | if (have_name) { |
bbccc3d2 | 104 | ret = asprintf(&traces_path, "%s/" DEFAULT_TRACE_DIR_NAME |
f2e0110c DG |
105 | "/%s-%s", alloc_path, session_name, datetime); |
106 | } else { | |
bbccc3d2 | 107 | ret = asprintf(&traces_path, "%s/" DEFAULT_TRACE_DIR_NAME |
f2e0110c DG |
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 DG |
120 | if (ret < 0) { |
121 | goto error; | |
122 | } | |
123 | ||
58a97671 DG |
124 | /* Init lttng session config */ |
125 | ret = config_init(session_name); | |
f3ed775e | 126 | if (ret < 0) { |
e0ac0dd5 DG |
127 | if (ret == -1) { |
128 | ret = CMD_ERROR; | |
129 | } | |
f3ed775e DG |
130 | goto error; |
131 | } | |
132 | ||
133 | MSG("Session %s created.", session_name); | |
0052ffe6 | 134 | MSG("Traces will be written in %s" , traces_path); |
f3ed775e DG |
135 | |
136 | ret = CMD_SUCCESS; | |
137 | ||
138 | error: | |
139 | if (alloc_path) { | |
140 | free(alloc_path); | |
141 | } | |
142 | ||
58a97671 DG |
143 | if (traces_path) { |
144 | free(traces_path); | |
f3ed775e DG |
145 | } |
146 | return ret; | |
147 | } | |
148 | ||
149 | /* | |
74cc1d0f | 150 | * cmd_create |
f3ed775e | 151 | * |
74cc1d0f | 152 | * The 'create <options>' first level command |
f3ed775e DG |
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; | |
679b4943 SM |
167 | case OPT_LIST_OPTIONS: |
168 | list_cmd_options(stdout, long_options); | |
169 | ret = CMD_SUCCESS; | |
170 | goto end; | |
f3ed775e DG |
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 | } |