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