static
void trace_text(FILE *input, int output)
{
	struct ctf_stream_pos pos;
	ssize_t len;
	char *line = NULL, *nl;
	size_t linesize;
	int ret;

	memset(&pos, 0, sizeof(pos));
	ret = ctf_init_pos(&pos, NULL, output, O_RDWR);
	if (ret) {
		fprintf(stderr, "Error in ctf_init_pos\n");
		return;
	}
	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);
		} else {
			trace_string(line, &pos, strlen(line) + 1);
		}
	}
	ret = ctf_fini_pos(&pos);
	if (ret) {
		fprintf(stderr, "Error in ctf_fini_pos\n");
	}
}
static
void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
{
	struct ctf_stream_pos dummy;
	int attempt = 0;
	char *tline = line;	/* tline is start of text, after timestamp */
	size_t tlen = len;
	uint64_t ts = 0;

	printf_debug("read: %s\n", line);

	for (;;) {
		int packet_filled = 0;

		ctf_dummy_pos(pos, &dummy);
		write_event_header(&dummy, line, &tline, len, &tlen, &ts);
		if (!ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT))
			packet_filled = 1;
		if (!ctf_move_pos(&dummy, tlen * CHAR_BIT))
			packet_filled = 1;
		if (packet_filled || ctf_pos_packet(&dummy)) {
			ctf_pos_pad_packet(pos);
			write_packet_header(pos, s_uuid);
			write_packet_context(pos);
			if (attempt++ == 1) {
				fprintf(stderr, "[Error] Line too large for packet size (%" PRIu64 "kB) (discarded)\n",
					pos->packet_size / CHAR_BIT / 1024);
				return;
			}
			continue;
		} else {
			break;
		}
	}

	write_event_header(pos, line, &tline, len, &tlen, &ts);
	if (!ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT))
		goto error;
	memcpy(ctf_get_pos_addr(pos), tline, tlen);
	if (!ctf_move_pos(pos, tlen * CHAR_BIT))
		goto error;
	return;

error:
	fprintf(stderr, "[error] Out of packet bounds when writing event payload\n");
	abort();
}