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