Complete dummy trace creation
[babeltrace.git] / tests / test-dummytrace.c
index ca79115ff782e407c763295cfdc4683d5bf986c3..aa855cc7cd95a2a9a62430e995a20399364d49fd 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * text-to-ctf.c
+ * babeltrace-log.c
  *
- * BabelTrace - Convert Text to CTF
+ * BabelTrace - Convert Text Log to CTF
  *
  * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/mman.h>
+#include <dirent.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <stdint.h>
 #include <unistd.h>
 #include <errno.h>
 #include <uuid/uuid.h>
 #include <string.h>
+#include <endian.h>
 
 #include <babeltrace/babeltrace.h>
 #include <babeltrace/ctf/types.h>
 
-int babeltrace_debug = 0;
+#ifndef UUID_STR_LEN
+#define UUID_STR_LEN   37      /* With \0 */
+#endif
+
+/* Metadata format string */
+static const char metadata_fmt[] =
+"typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
+"typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
+"\n"
+"trace {\n"
+"      major = %s;\n"                  /* major (e.g. 0) */
+"      minor = %s;\n"                  /* minor (e.g. 1) */
+"      uuid = \"%s\";\n"               /* UUID */
+"      byte_order = %s;\n"             /* be or le */
+"      packet.header := struct {\n"
+"              uint32_t magic;\n"
+"              uint8_t  trace_uuid[16];\n"
+"      };\n"
+"};\n"
+"\n"
+"stream {\n"
+"      packet.context := struct {\n"
+"              uint32_t content_size;\n"
+"              uint32_t packet_size;\n"
+"      };\n"
+"};\n"
+"\n"
+"event {\n"
+"      name = string;\n"
+"      fields := struct { string str; };\n"
+"};\n";
+
+int babeltrace_debug, babeltrace_verbose;
 
 static uuid_t s_uuid;
 
+static
+void print_metadata(FILE *fp)
+{
+       char uuid_str[UUID_STR_LEN];
+
+       uuid_unparse(s_uuid, uuid_str);
+       fprintf(fp, metadata_fmt,
+               __stringify(BABELTRACE_VERSION_MAJOR),
+               __stringify(BABELTRACE_VERSION_MINOR),
+               uuid_str,
+               BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be");
+}
+
+
 static
 void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid)
 {
@@ -126,7 +175,7 @@ void trace_text(FILE *input, int output)
        char *line = NULL, *nl;
        size_t linesize;
 
-       ctf_init_pos(&pos, output, O_WRONLY);
+       ctf_init_pos(&pos, output, O_RDWR);
 
        write_packet_header(&pos, s_uuid);
        write_packet_context(&pos);
@@ -142,29 +191,98 @@ void trace_text(FILE *input, int output)
        ctf_fini_pos(&pos);
 }
 
+static void usage(FILE *fp)
+{
+       fprintf(fp, "BabelTrace Log Converter %u.%u\n",
+               BABELTRACE_VERSION_MAJOR,
+               BABELTRACE_VERSION_MINOR);
+       fprintf(fp, "\n");
+       fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
+       fprintf(fp, "\n");
+       fprintf(fp, "usage : babeltrace-log OUTPUT\n");
+       fprintf(fp, "\n");
+       fprintf(fp, "  OUTPUT                         Output trace path\n");
+       fprintf(fp, "\n");
+}
+
 int main(int argc, char **argv)
 {
-       int fd, ret;
+       int fd, metadata_fd, ret;
+       char *outputname;
+       DIR *dir;
+       int dir_fd;
+       FILE *metadata_fp;
 
-       ret = unlink("dummystream");
-       if (ret < 0) {
-               perror("unlink");
-               return -1;
+       if (argc < 2) {
+               usage(stdout);
+               goto error;
        }
-       fd = open("dummystream", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
+       outputname = argv[1];
+
+       ret = mkdir(outputname, S_IRWXU|S_IRWXG);
+       if (ret) {
+               perror("mkdir");
+               goto error;
+       }
+
+       dir = opendir(outputname);
+       if (!dir) {
+               perror("opendir");
+               goto error_rmdir;
+       }
+       dir_fd = dirfd(dir);
+       if (dir_fd < 0) {
+               perror("dirfd");
+               goto error_closedir;
+       }
+
+       fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
+                   S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
        if (fd < 0) {
-               perror("open");
-               return -1;
+               perror("openat");
+               goto error_closedirfd;
        }
 
-       ret = uuid_parse("2a6422d0-6cee-11e0-8c08-cb07d7b3a564", s_uuid);
-       if (ret) {
-               printf("uuid parse error\n");
-               close(fd);
-               return -1;
+       metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT,
+                            S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
+       if (fd < 0) {
+               perror("openat");
+               goto error_closedatastream;
+       }
+       metadata_fp = fdopen(metadata_fd, "w");
+       if (!metadata_fp) {
+               perror("fdopen");
+               goto error_closemetadatafd;
        }
 
+       uuid_generate(s_uuid);
+       print_metadata(metadata_fp);
        trace_text(stdin, fd);
+
        close(fd);
-       return 0;
+       exit(EXIT_SUCCESS);
+
+       /* error handling */
+error_closemetadatafd:
+       ret = close(metadata_fd);
+       if (ret)
+               perror("close");
+error_closedatastream:
+       ret = close(fd);
+       if (ret)
+               perror("close");
+error_closedirfd:
+       ret = close(dir_fd);
+       if (ret)
+               perror("close");
+error_closedir:
+       ret = closedir(dir);
+       if (ret)
+               perror("closedir");
+error_rmdir:
+       ret = rmdir(outputname);
+       if (ret)
+               perror("rmdir");
+error:
+       exit(EXIT_FAILURE);
 }
This page took 0.024566 seconds and 4 git commands to generate.