/* b01902062 藍ĉŒşç‘‹ */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "logger.h"
#include "xwrap.h"

#include <fcntl.h>
#include <locale.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

void comp135_init (Comp135* comp, const char* static_name, bool no_init) {
	if (!no_init) {
		setlocale (LC_ALL, "");
		tzset ();
	}

	comp->name = strrchr (static_name, '/');
	if (comp->name == NULL) {
		comp->name = (char*)static_name;
	} else {
		comp->name++;
	}
	comp->pid = getpid ();
	comp->debug = false;
	comp->is_term = false;

	const char* comp135_debug = getenv ("COMP135_DEBUG");
	const char* comp135_logfile = getenv ("COMP135_LOGFILE");
	if (comp135_debug == NULL ||
		strcmp (comp135_debug, "0")     == 0 ||
		strcmp (comp135_debug, "n")     == 0 ||
		strcmp (comp135_debug, "no")    == 0 ||
		strcmp (comp135_debug, "f")     == 0 ||
		strcmp (comp135_debug, "false") == 0) {

		return;
	}

	comp->debug = true;
	comp->log = NULL;
	comp->log_fd = STDERR_FILENO;
	comp->log_file = stderr;

	if (isatty (comp->log_fd)) {
		comp->is_term = true;
	}

	if (comp135_logfile == NULL || *comp135_logfile == '\0') {
		return;
	}

	int fd = open (
		comp135_logfile, O_WRONLY | O_CREAT | O_APPEND, S_IWUSR | S_IRUSR);
	if (fd < 0) {
		return;
	}

	xfaddfd (fd, FD_CLOEXEC);

	if (isatty (fd)) {
		comp->is_term = true;
	} else {
		comp->is_term = false;
	}

	FILE* fp = fdopen (fd, "wb");
	if (fp == NULL) {
		close (fd);
		return;
	}

	setvbuf (fp, NULL, _IOLBF, 0);
	comp->log = xstrdup (comp135_logfile);
	comp->log_fd = fd;
	comp->log_file = fp;
}

void comp135_destroy (Comp135* comp) {
	if (!comp->debug) {
		return;
	}

	if (comp->log != NULL) {
		free (comp->log);
		fclose (comp->log_file);
	}
}

void comp135_log (Comp135* comp, const char* format, ...) {
	if (!comp->debug) {
		return;
	}

	time_t t;
	struct tm tmd;

	time (&t);
	localtime_r (&t, &tmd);

	struct flock lock_info = {
		.l_type = F_WRLCK,
		.l_whence = SEEK_SET,
		.l_start = 0,
		.l_len = 0
	};
	fcntl (comp->log_fd, F_SETLKW, &lock_info);

	char pid_color[] = "\033[1;44;37m";
	pid_color[5] = comp->pid % 6 + '1';
	fprintf (comp->log_file,
		"%s%04d-%02d-%02d %s%02d:%02d:%02d %s%s[%s%d%s]: %s",
		comp->is_term ? "\033[31m" : "",
		tmd.tm_year + 1900, tmd.tm_mon + 1, tmd.tm_mday,
		comp->is_term ? "\033[32m" : "",
		tmd.tm_hour, tmd.tm_min, tmd.tm_sec,
		comp->is_term ? "\033[33m" : "",
		comp->name,
		comp->is_term ? pid_color : "",
		comp->pid,
		comp->is_term ? "\033[0m\033[33m" : "",
		comp->is_term ? "\033[0m" : "");

	va_list ap;
	va_start (ap, format);
	vfprintf (comp->log_file, format, ap);
	va_end (ap);

	putc ('\n', comp->log_file);

	lock_info.l_type = F_UNLCK;
	fcntl (comp->log_fd, F_SETLK, &lock_info);
}