/* Logging routines.
 *
 * IRC Services is copyright (c) 1996-2002 Andrew Church.
 *     E-mail: <achurch@achurch.org>
 * Parts written by Andrew Kempe and others.
 * This program is free but copyrighted software; see the file COPYING for
 * details.
 */

#include "services.h"

static FILE *logfile;
static char current_filename[PATH_MAX+1];

/* Are we in fatal() or fatal_perror()?  (Use this to avoid infinite
 * recursion if wallops() does a fatal().) */
static int in_fatal = 0;

/*************************************************************************/
/*************************************************************************/

/* Local routine to generate a filename from a filename pattern (possibly
 * containing %y/%m/%d for year/month/day).  Result is returned in a static
 * buffer, and will not be longer than PATH_MAX characters.
 */

static char *gen_log_filename(void)
{
    static char result[PATH_MAX+1];
    const char *s, *from;
    char *to;

    time_t now = time(NULL);
    struct tm *tm = localtime(&now);
    tm->tm_year += 1900;
    tm->tm_mon++;

    from = LogFilename;
    if (!*from) {
	*result = 0;
	return result;
    }
    to = result;

    while ((s = strchr(from, '%')) != NULL) {
	to += snprintf(to, sizeof(result)-(to-result), "%.*s", s-from, from);
	s++;
	switch (*s) {
	  case 'y':
	    to += snprintf(to, sizeof(result)-(to-result), "%d", tm->tm_year);
	    break;
	  case 'm':
	    to += snprintf(to, sizeof(result)-(to-result), "%02d", tm->tm_mon);
	    break;
	  case 'd':
	    to += snprintf(to, sizeof(result)-(to-result), "%02d",tm->tm_mday);
	    break;
	  default:
	    if (to-result < sizeof(result)-1)
		*to++ = *s;
	    break;
	}
	from = s+1;
    }
    to += snprintf(to, sizeof(result)-(to-result), "%s", from);

    *to = 0;
    return result;
}

/*************************************************************************/

/* Local routine to check whether the log file needs to be rotated, and
 * rotate it if so.  Assumes the log file is already open.
 */

static void check_log_rotate(void)
{
    char *newname = gen_log_filename();
    if (strlen(newname) > sizeof(current_filename)-1)
	newname[sizeof(current_filename)-1] = 0;
    if (strcmp(current_filename, gen_log_filename()) != 0) {
	if (!reopen_log())
	    log("Warning: Unable to rotate log file: %s", strerror(errno));
    }
}

/*************************************************************************/
/*************************************************************************/

/* Local routines to write text to the log file and/or stderr as needed. */

static void logputs(const char *str)
{
    if (logfile) {
	check_log_rotate();
	fputs(str, logfile);
    }
    if (nofork)
	fputs(str, stderr);
}

static void vlogprintf(const char *fmt, va_list args)
{
    if (logfile) {
	check_log_rotate();
	vfprintf(logfile, fmt, args);
    }
    if (nofork)
	vfprintf(stderr, fmt, args);
}

static void logprintf(const char *fmt, ...) FORMAT(printf,1,2);
static void logprintf(const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    vlogprintf(fmt, args);
    va_end(args);
}

/*************************************************************************/

/* Local routine to write the time of day to the log. */

static void write_time()
{
    time_t t;
    struct tm tm;
    char buf[256];

    time(&t);
    tm = *localtime(&t);
#if HAVE_GETTIMEOFDAY
    if (debug) {
	char *s;
	struct timeval tv;
	gettimeofday(&tv, NULL);
	strftime(buf, sizeof(buf)-1, "[%b %d %H:%M:%S", &tm);
	s = buf + strlen(buf);
	s += snprintf(s, sizeof(buf)-(s-buf), ".%06d", tv.tv_usec);
	strftime(s, sizeof(buf)-(s-buf)-1, " %Y] ", &tm);
    } else {
#endif
	strftime(buf, sizeof(buf)-1, "[%b %d %H:%M:%S %Y] ", &tm);
#if HAVE_GETTIMEOFDAY
    }
#endif
    logputs(buf);
}

/*************************************************************************/
/*************************************************************************/

/* Open the log file.  Return zero if the log file could not be opened,
 * else return nonzero (success).
 */

int open_log(void)
{
    if (logfile)
	return 1;
    strscpy(current_filename, gen_log_filename(), sizeof(current_filename));
    logfile = fopen(current_filename, "a");
    if (logfile)
	setbuf(logfile, NULL);
    return logfile!=NULL ? 1 : 0;
}

/*************************************************************************/

/* Close the log file. */

void close_log(void)
{
    if (!logfile)
	return;
    fclose(logfile);
    logfile = NULL;
}

/*************************************************************************/

/* Reopen the log file with the current value of LogFilename (for use when
 * rehashing configuration files or rotating the log file).  Return value
 * is like open_log().
 */

int reopen_log(void)
{
    char *newname;
    FILE *f;

    newname = gen_log_filename();
    /* Make sure it will fit in current_filename later */
    if (strlen(newname) > sizeof(current_filename)-1)
	newname[sizeof(current_filename)-1] = 0;
    f = fopen(newname, "a");
    if (!f)
	return 0;
    setbuf(f, NULL);
    if (logfile)
	fclose(logfile);
    logfile = f;
    strcpy(current_filename, newname);  /* safe b/c of length check above */
    return 1;
}

/*************************************************************************/

/* Return nonzero if the log file is currently open, zero if closed. */

int log_is_open(void)
{
    return logfile != NULL;
}

/*************************************************************************/
/*************************************************************************/

/* Log stuff to the log file with a datestamp.  Note that errno is
 * preserved by this routine and log_perror().
 */

void log(const char *fmt, ...)
{
    va_list args;
    int errno_save = errno;

    va_start(args, fmt);
    write_time();
    vlogprintf(fmt, args);
    logputs("\n");
    va_end(args);
    errno = errno_save;
}

/*************************************************************************/

/* Like log(), but tack a ": " and a system error message (as returned by
 * strerror()) onto the end.
 */

void log_perror(const char *fmt, ...)
{
    va_list args;
    int errno_save = errno;

    va_start(args, fmt);
    write_time();
    vlogprintf(fmt, args);
    logprintf(": %s\n",
	      (errno_save<0) ? hstrerror(-errno_save) : strerror(errno_save));
    va_end(args);
    errno = errno_save;
}

/*************************************************************************/
/*************************************************************************/

/* Similar to log[_perror](), but used by modules to include the module
 * name at the beginning of the log message.  Modules actually call
 * module_log[_perror](), which is a macro that inserts MODULE_NAME as the
 * first parameter in the call (since we can't access it ourselves).
 */

void _module_log(const char *modname, const char *fmt, ...)
{
    va_list args;
    int errno_save = errno;

    va_start(args, fmt);
    write_time();
    logprintf("%s: ", modname);
    vlogprintf(fmt, args);
    logputs("\n");
    va_end(args);
    errno = errno_save;
}

/*************************************************************************/

void _module_log_perror(const char *modname, const char *fmt, ...)
{
    va_list args;
    int errno_save = errno;

    va_start(args, fmt);
    write_time();
    logprintf("%s: ", modname);
    vlogprintf(fmt, args);
    logprintf(": %s\n",
	      (errno_save<0) ? hstrerror(-errno_save) : strerror(errno_save));
    va_end(args);
    errno = errno_save;
}

/*************************************************************************/
/*************************************************************************/

/* We've hit something we can't recover from.  Let people know what
 * happened, then go down.
 */

void fatal(const char *fmt, ...)
{
    va_list args;
    char buf[4096];

    if (in_fatal)
	return;
    in_fatal++;
    va_start(args, fmt);
    vsnprintf(buf, sizeof(buf), fmt, args);
    va_end(args);
    write_time();
    logprintf("FATAL: %s\n", buf);
    if (servsock && sock_isconn(servsock))
	wallops(NULL, "FATAL ERROR!  %s", buf);
    exit(1);
}

/*************************************************************************/

/* Same thing, but do it like perror(). */

void fatal_perror(const char *fmt, ...)
{
    va_list args;
    char buf[4096];
    const char *errstr;

    if (in_fatal)
	return;
    in_fatal++;
    errstr = (errno<0) ? hstrerror(-errno) : strerror(errno);
    va_start(args, fmt);
    vsnprintf(buf, sizeof(buf), fmt, args);
    va_end(args);
    write_time();
    logprintf("FATAL: %s: %s\n", buf, errstr);
    if (servsock && sock_isconn(servsock))
	wallops(NULL, "FATAL ERROR!  %s: %s", buf, errstr);
    exit(1);
}

/*************************************************************************/
