Commit bad2c936 authored by Bradley C. Kuszmaul's avatar Bradley C. Kuszmaul

More log stuff

git-svn-id: file:///svn/tokudb@107 c7de825b-a66e-492c-adef-691d508d4ae1
parent ec3a43ed
......@@ -6,8 +6,9 @@ typedef struct tokulogger *TOKULOGGER;
struct tokulogger {
enum typ_tag tag;
char *directory;
FILE *f;
int next_log_file_number;
int fd;
int n_in_file;
long long next_log_file_number;
char buf[LOGGER_BUF_SIZE];
int n_in_buf;
};
......
#include "log-internal.h"
#include "memory.h"
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
int tokulogger_find_next_unused_log_file(const char *directory, long long *result) {
DIR *d=opendir(directory);
......@@ -20,7 +25,7 @@ int tokulogger_find_next_unused_log_file(const char *directory, long long *resul
return 0;
}
int tokulogger_create_and_open_logger (const char *directory) {
int tokulogger_create_and_open_logger (const char *directory, TOKULOGGER *resultp) {
TAGMALLOC(TOKULOGGER, result);
if (result==0) return -1;
int r;
......@@ -33,8 +38,42 @@ int tokulogger_create_and_open_logger (const char *directory) {
}
result->directory = toku_strdup(directory);
if (result->directory!=0) goto died0;
result->f = 0;
result->fd = -1;
result->next_log_file_number = nexti;
result->n_in_buf = 0;
*resultp=result;
return 0;
}
int tokulogger_log_bytes(TOKULOGGER logger, int nbytes, char *bytes) {
int r;
if (logger->fd==-1) {
int fnamelen = strlen(logger->directory)+50;
char fname[fnamelen];
snprintf(fname, fnamelen, "%s/log%012llu.tokulog", logger->directory, logger->next_log_file_number);
logger->fd = creat(fname, O_EXCL | 0700);
if (logger->fd==-1) return errno;
}
logger->next_log_file_number++;
if (logger->n_in_buf + nbytes > LOGGER_BUF_SIZE) {
struct iovec v[2];
v[0].iov_base = logger->buf;
v[0].iov_len = logger->n_in_buf;
v[1].iov_base = bytes;
v[1].iov_len = nbytes;
r=writev(logger->fd, v, 2);
if (r!=logger->n_in_buf + nbytes) return errno;
logger->n_in_file += logger->n_in_buf+nbytes;
logger->n_in_buf=0;
if (logger->n_in_file > 100<<20) {
r = close(logger->fd);
if (r!=0) return errno;
logger->fd=0;
logger->n_in_file = 0;
}
} else {
memcpy(logger->buf+logger->n_in_buf, bytes, nbytes);
logger->n_in_buf += nbytes;
}
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment