#include "config.h"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include "logger.h"
#include "var.h"
#include "gcmalloc.h"
#define MAX_LOG_FILESIZE_BYTES 2097152 // 2MB
#define MAX_LOG_FILESIZE_KBYTES (MAX_LOG_FILESIZE_BYTES/1024)
#define MAX_LOG_FILESIZE_MBYTES (MAX_LOG_FILESIZE_BYTES/(1024 * 1024))
#define LOG_DISCARD_BYTES (MAX_LOG_FILESIZE_BYTES/3)
static int log_flush (struct log *p, int newbytes)
{
struct stat st;
if (!p || !p->fp) {
return -1;
}
if (!p->byteswritten) {
bzero(&st, sizeof(st));
if (fstat(fileno(p->fp), &st) < 0) {
return -1;
}
if (st.st_size < 0) {
return -1;
}
p->byteswritten = st.st_size;
}
if (newbytes > 0) {
p->byteswritten += newbytes;
}
if (p->byteswritten > MAX_LOG_FILESIZE_BYTES) {
char *buf = NULL;
size_t discard, saved = 0;
FILE *fp;
discard = p->byteswritten/3;
if (discard < LOG_DISCARD_BYTES) {
discard = LOG_DISCARD_BYTES;
}
fp = fopen(p->fname, "r");
buf = malloc(MAX_LOG_FILESIZE_BYTES);
if (fp && buf) {
if (fseeko(fp, discard, SEEK_SET) == 0) {
saved = fread(buf, MAX_LOG_FILESIZE_BYTES, sizeof(*buf), fp);
if (saved == MAX_LOG_FILESIZE_BYTES) {
saved = 0;
}
}
}
if (fp) {
fclose(fp);
}
p->byteswritten = 0;
(void)fpurge(p->fp);
p->fp = freopen(p->fname, "wa", p->fp);
if (p->fp == NULL)
return -1;
fprintf(p->fp, "logfile turned over due to size>%d%s\n",
(MAX_LOG_FILESIZE_MBYTES > 0)? MAX_LOG_FILESIZE_MBYTES:MAX_LOG_FILESIZE_KBYTES,
(MAX_LOG_FILESIZE_MBYTES > 0)? "MB":"KB");
if (buf && saved) {
(void)fwrite(buf, saved, sizeof(*buf), p->fp);
}
if (buf) {
free(buf);
}
}
(void)fflush(p->fp);
return 0;
}
struct log *
log_open(siz, fname)
size_t siz;
char *fname;
{
struct log *p;
p = (struct log *)racoon_malloc(sizeof(*p));
if (p == NULL)
return NULL;
memset(p, 0, sizeof(*p));
p->buf = (char **)racoon_malloc(sizeof(char *) * siz);
if (p->buf == NULL) {
racoon_free(p);
return NULL;
}
memset(p->buf, 0, sizeof(char *) * siz);
p->tbuf = (time_t *)racoon_malloc(sizeof(time_t *) * siz);
if (p->tbuf == NULL) {
racoon_free(p->buf);
racoon_free(p);
return NULL;
}
memset(p->tbuf, 0, sizeof(time_t *) * siz);
p->siz = siz;
if (fname)
p->fname = racoon_strdup(fname);
return p;
}
void
log_add(p, str)
struct log *p;
char *str;
{
if (p->buf[p->head])
racoon_free(p->buf[p->head]);
p->buf[p->head] = racoon_strdup(str);
p->tbuf[p->head] = time(NULL);
p->head++;
p->head %= p->siz;
}
int
log_print(p, str)
struct log *p;
char *str;
{
int bytes;
if (p->fname == NULL)
return -1;
if (p->fp == NULL) {
p->fp = fopen(p->fname, "a");
}
if (p->fp == NULL)
return -1;
bytes = fprintf(p->fp, "%s", str);
if (log_flush(p, bytes)) {
return -1;
}
return 0;
}
int
log_vprint(struct log *p, const char *fmt, ...)
{
va_list ap;
int bytes;
if (p->fname == NULL)
return -1;
if (p->fp == NULL) {
p->fp = fopen(p->fname, "a");
}
if (p->fp == NULL)
return -1;
va_start(ap, fmt);
bytes = vfprintf(p->fp, fmt, ap);
va_end(ap);
if (log_flush(p, bytes)) {
return -1;
}
return 0;
}
int
log_vaprint(struct log *p, const char *fmt, va_list ap)
{
int bytes;
if (p->fname == NULL)
return -1;
if (p->fp == NULL) {
p->fp = fopen(p->fname, "a");
}
if (p->fp == NULL)
return -1;
bytes = vfprintf(p->fp, fmt, ap);
if (log_flush(p, bytes)) {
return -1;
}
return 0;
}
int
log_close(p)
struct log *p;
{
int i, j;
char ts[256];
struct tm *tm;
int bytes;
if (p->fname == NULL)
goto nowrite;
if (p->fp == NULL) {
p->fp = fopen(p->fname, "a");
}
if (p->fp == NULL)
goto nowrite;
for (i = 0; i < p->siz; i++) {
j = (p->head + i) % p->siz;
if (p->buf[j]) {
tm = localtime(&p->tbuf[j]);
strftime(ts, sizeof(ts), "%B %d %T", tm);
bytes = fprintf(p->fp, "%s: %s\n", ts, p->buf[j]);
(void)log_flush(p, bytes);
if (*(p->buf[j] + strlen(p->buf[j]) - 1) != '\n') {
bytes = fprintf(p->fp, "\n");
(void)log_flush(p, bytes);
}
}
}
nowrite:
log_free(p);
return 0;
}
void
log_free(p)
struct log *p;
{
int i;
for (i = 0; i < p->siz; i++)
racoon_free(p->buf[i]);
racoon_free(p->buf);
racoon_free(p->tbuf);
if (p->fname)
racoon_free(p->fname);
if (p->fp) {
fclose(p->fp);
}
racoon_free(p);
}
#ifdef TEST
struct log *l;
void
vatest(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_vaprint(l, fmt, ap);
va_end(ap);
}
int
main(argc, argv)
int argc;
char **argv;
{
int i;
l = log_open(30, "/tmp/hoge");
if (l == NULL)
errx(1, "hoge");
for (i = 0; i < 50; i++) {
log_add(l, "foo");
log_add(l, "baa");
log_add(l, "baz");
}
log_print(l, "hoge\n");
log_vprint(l, "hoge %s\n", "this is test");
vatest("%s %s\n", "this is", "vprint test");
abort();
log_free(l);
}
#endif