/*
 * 2008+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>

#include <sys/syscall.h>

#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#include "fserver.h"

static FILE *el_log_stream;

void uloga(const char *f, ...)
{
	va_list ap;

	if (!el_log_stream)
		el_log_stream = stdout;

	va_start(ap, f);
	vfprintf(el_log_stream, f, ap);
	va_end(ap);

	fflush(el_log_stream);
}

#ifdef FSERVER_DEBUG
void ulog(const char *f, ...)
{
	char str[64];
	struct tm tm;
	struct timeval tv;
	va_list ap;

	if (!el_log_stream)
		el_log_stream = stdout;

	gettimeofday(&tv, NULL);
	localtime_r((time_t *)&tv.tv_sec, &tm);
	strftime(str, sizeof(str), "%F %R:%S", &tm);

	fprintf(el_log_stream, "%s.%lu %ld ", str, tv.tv_usec, syscall(__NR_gettid));

	va_start(ap, f);
	vfprintf(el_log_stream, f, ap);
	va_end(ap);

	fflush(el_log_stream);
}
#else
void ulog(const char *f __unused, ...)
{
}
#endif

int ulog_init(char *log)
{
	FILE *f;

	f = fopen(log, "a");
	if (!f) {
		ulog_err("Failed to open log file %s", log);
		return -errno;
	}

	el_log_stream = f;

	ulog("Logging has been started.\n");
	return 0;
}
