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"); | |
1c8d13c8 | 55 | fprintf(ofp, " The default NAME is 'auto-yyyymmdd-hhmmss'\n"); |
f3ed775e | 56 | fprintf(ofp, " -h, --help Show this help\n"); |
1c8d13c8 | 57 | fprintf(ofp, " --list-options Simple listing of options\n"); |
58a97671 | 58 | fprintf(ofp, " -o, --output PATH Specify output path for traces\n"); |
f3ed775e DG |
59 | fprintf(ofp, "\n"); |
60 | } | |
61 | ||
62 | /* | |
1c8d13c8 TD |
63 | * Create a tracing session. |
64 | * If no name is specified, a default name is generated. | |
f3ed775e | 65 | * |
1c8d13c8 | 66 | * Returns one of the CMD_* result constants. |
f3ed775e DG |
67 | */ |
68 | static int create_session() | |
69 | { | |
318e4c83 | 70 | int ret; |
d6175221 | 71 | char datetime[16]; |
58a97671 | 72 | char *session_name, *traces_path = NULL, *alloc_path = NULL; |
f3ed775e DG |
73 | time_t rawtime; |
74 | struct tm *timeinfo; | |
75 | ||
d6175221 DG |
76 | /* Get date and time for automatic session name/path */ |
77 | time(&rawtime); | |
78 | timeinfo = localtime(&rawtime); | |
79 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
80 | ||
f3ed775e DG |
81 | /* Auto session name creation */ |
82 | if (opt_session_name == NULL) { | |
318e4c83 | 83 | ret = asprintf(&session_name, "auto"); |
d6175221 DG |
84 | if (ret < 0) { |
85 | perror("asprintf session name"); | |
318e4c83 | 86 | ret = CMD_ERROR; |
d6175221 DG |
87 | goto error; |
88 | } | |
f3ed775e DG |
89 | DBG("Auto session name set to %s", session_name); |
90 | } else { | |
91 | session_name = opt_session_name; | |
92 | } | |
93 | ||
94 | /* Auto output path */ | |
95 | if (opt_output_path == NULL) { | |
58a97671 | 96 | alloc_path = strdup(config_get_default_path()); |
f3ed775e | 97 | if (alloc_path == NULL) { |
318e4c83 TD |
98 | ERR("Home path not found.\n" |
99 | "Please specify an output path using -o, --output PATH\n"); | |
f3ed775e DG |
100 | ret = CMD_FATAL; |
101 | goto error; | |
102 | } | |
f3ed775e | 103 | |
54bd3caf TD |
104 | ret = asprintf(&traces_path, "%s/" DEFAULT_TRACE_DIR_NAME "/%s-%s", |
105 | alloc_path, session_name, datetime); | |
58a97671 DG |
106 | if (ret < 0) { |
107 | perror("asprintf trace dir name"); | |
318e4c83 | 108 | ret = CMD_ERROR; |
58a97671 DG |
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 | 116 | if (ret < 0) { |
ae856491 | 117 | /* Don't set ret so lttng can interpret the sessiond error. */ |
f3ed775e DG |
118 | goto error; |
119 | } | |
120 | ||
58a97671 DG |
121 | /* Init lttng session config */ |
122 | ret = config_init(session_name); | |
f3ed775e | 123 | if (ret < 0) { |
27089920 | 124 | ret = CMD_ERROR; |
f3ed775e DG |
125 | goto error; |
126 | } | |
127 | ||
128 | MSG("Session %s created.", session_name); | |
0052ffe6 | 129 | MSG("Traces will be written in %s" , traces_path); |
f3ed775e DG |
130 | |
131 | ret = CMD_SUCCESS; | |
132 | ||
133 | error: | |
54bd3caf TD |
134 | if (opt_session_name == NULL) { |
135 | free(session_name); | |
136 | } | |
137 | ||
f3ed775e DG |
138 | if (alloc_path) { |
139 | free(alloc_path); | |
140 | } | |
141 | ||
58a97671 DG |
142 | if (traces_path) { |
143 | free(traces_path); | |
f3ed775e DG |
144 | } |
145 | return ret; | |
146 | } | |
147 | ||
148 | /* | |
74cc1d0f | 149 | * The 'create <options>' first level command |
1c8d13c8 TD |
150 | * |
151 | * Returns one of the CMD_* result constants. | |
f3ed775e DG |
152 | */ |
153 | int cmd_create(int argc, const char **argv) | |
154 | { | |
155 | int opt, ret = CMD_SUCCESS; | |
156 | static poptContext pc; | |
157 | ||
158 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
159 | poptReadDefaultConfig(pc, 0); | |
160 | ||
161 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
162 | switch (opt) { | |
163 | case OPT_HELP: | |
27089920 | 164 | usage(stdout); |
f3ed775e | 165 | goto end; |
679b4943 SM |
166 | case OPT_LIST_OPTIONS: |
167 | list_cmd_options(stdout, long_options); | |
679b4943 | 168 | goto end; |
f3ed775e DG |
169 | default: |
170 | usage(stderr); | |
171 | ret = CMD_UNDEFINED; | |
172 | goto end; | |
173 | } | |
174 | } | |
175 | ||
176 | opt_session_name = (char*) poptGetArg(pc); | |
177 | ||
178 | ret = create_session(); | |
179 | ||
180 | end: | |
ca1c3607 | 181 | poptFreeContext(pc); |
f3ed775e DG |
182 | return ret; |
183 | } |