+#include <babeltrace/babeltrace.h>
+#include <babeltrace/ctf/types.h>
+
+int babeltrace_debug, babeltrace_verbose;
+
+static uuid_t s_uuid;
+
+static
+void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid)
+{
+ struct ctf_stream_pos dummy;
+
+ /* magic */
+ ctf_dummy_pos(pos, &dummy);
+ ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+ ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+ assert(!ctf_pos_packet(&dummy));
+
+ ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+ *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1;
+ ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+
+ /* trace_uuid */
+ ctf_dummy_pos(pos, &dummy);
+ ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
+ ctf_move_pos(&dummy, 16 * CHAR_BIT);
+ assert(!ctf_pos_packet(&dummy));
+
+ ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
+ memcpy(ctf_get_pos_addr(pos), uuid, 16);
+ ctf_move_pos(pos, 16 * CHAR_BIT);
+}
+
+static
+void write_packet_context(struct ctf_stream_pos *pos)
+{
+ struct ctf_stream_pos dummy;
+
+ /* content_size */
+ ctf_dummy_pos(pos, &dummy);
+ ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+ ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+ assert(!ctf_pos_packet(&dummy));
+
+ ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+ *(uint32_t *) ctf_get_pos_addr(pos) = -1U; /* Not known yet */
+ pos->content_size_loc = (uint32_t *) ctf_get_pos_addr(pos);
+ ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+
+ /* packet_size */
+ ctf_dummy_pos(pos, &dummy);
+ ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+ ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
+ assert(!ctf_pos_packet(&dummy));
+
+ ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+ *(uint32_t *) ctf_get_pos_addr(pos) = pos->packet_size;
+ ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
+}
+
+static
+void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
+{
+ struct ctf_stream_pos dummy;
+ int attempt = 0;
+
+ printf_debug("read: %s\n", line);
+retry:
+ ctf_dummy_pos(pos, &dummy);
+ ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
+ ctf_move_pos(&dummy, len * CHAR_BIT);
+ if (ctf_pos_packet(&dummy)) {
+ /* TODO write content size */
+ ctf_pos_pad_packet(pos);
+ write_packet_header(pos, s_uuid);
+ write_packet_context(pos);
+ if (attempt++ == 1) {
+ fprintf(stdout, "[Error] Line too large for packet size (%zukB) (discarded)\n",
+ pos->packet_size / CHAR_BIT / 1024);
+ return;
+ }
+ goto retry;
+ }
+
+ ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
+ memcpy(ctf_get_pos_addr(pos), line, len);
+ ctf_move_pos(pos, len * CHAR_BIT);
+}
+
+static
+void trace_text(FILE *input, int output)
+{
+ struct ctf_stream_pos pos;
+ ssize_t len;
+ char *line = NULL, *nl;
+ size_t linesize;
+
+ ctf_init_pos(&pos, output, O_WRONLY);
+
+ write_packet_header(&pos, s_uuid);
+ write_packet_context(&pos);
+ for (;;) {
+ len = getline(&line, &linesize, input);
+ if (len < 0)
+ break;
+ nl = strrchr(line, '\n');
+ if (nl)
+ *nl = '\0';
+ trace_string(line, &pos, nl - line + 1);
+ }
+ ctf_fini_pos(&pos);
+}