#include <ex_common.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <libpmemlog.h>
#include "logentry.h"
int
main(int argc, char *argv[])
{
PMEMlogpool *plp;
struct logentry header;
struct iovec *iovp;
struct iovec *next_iovp;
int iovcnt;
if (argc < 3) {
fprintf(stderr, "usage: %s filename lines...\n", argv[0]);
exit(1);
}
const char *path = argv[1];
plp = pmemlog_create(path, 0, CREATE_MODE_RW);
if (plp == NULL &&
(plp = pmemlog_open(path)) == NULL) {
perror(path);
exit(1);
}
time(&header.timestamp);
header.pid = getpid();
iovcnt = (argc - 2) * 2 + 2;
if ((iovp = malloc(sizeof(*iovp) * iovcnt)) == NULL) {
perror("malloc");
exit(1);
}
next_iovp = iovp;
next_iovp->iov_base = &header;
next_iovp->iov_len = sizeof(header);
next_iovp++;
header.len = 0;
for (int arg = 2; arg < argc; arg++) {
next_iovp->iov_base = argv[arg];
next_iovp->iov_len = strlen(argv[arg]);
header.len += next_iovp->iov_len;
next_iovp++;
next_iovp->iov_base = "\n";
next_iovp->iov_len = 1;
header.len += 1;
next_iovp++;
}
int a = sizeof(long long);
int len_to_round = 1 + (a - (header.len + 1) % a) % a;
char *buf[sizeof(long long)] = {0};
next_iovp->iov_base = buf;
next_iovp->iov_len = len_to_round;
header.len += len_to_round;
next_iovp++;
if (pmemlog_appendv(plp, iovp, iovcnt) < 0) {
perror("pmemlog_appendv");
free(iovp);
exit(1);
}
free(iovp);
pmemlog_close(plp);
}