
/*
 *  LOG.C
 *
 *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
 *
 *  ulog(level, ctl, args...)
 */

#include <proto/all.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>

#include "log.h"

int	LogLevel = -1;
int	LogToStdout = 0;
char	*LogProgram = "-";
char	*LogHost = "-";
char	*LogWho = "-";
char	*LogFile = "UUSPOOL:LOGFILE";
char	LogBuf[512];

void
ulog(level, ctl, arg1, arg2, arg3, arg4, arg5, arg6)
int level;
char *ctl;
long arg1, arg2, arg3, arg4, arg5, arg6;
{
    long clock;
    struct tm *ut;
    int logfd;
    int len;

    if (level > LogLevel)
	return;

    (void)time(&clock);
    ut = localtime(&clock);

    sprintf(LogBuf, "(%d/%d-%d:%02d:%02d) %s,%s,%s ",
	ut->tm_mon+1, ut->tm_mday, ut->tm_hour, ut->tm_min, ut->tm_sec,
	LogProgram,
	LogHost,
	LogWho
    );
    sprintf(LogBuf + strlen(LogBuf), ctl, arg1, arg2, arg3, arg4, arg5, arg6);

    len = strlen(LogBuf);
    LogBuf[len++] = '\n';
    LogBuf[len] = 0;

    DEBUG(0, "%s", LogBuf);

    if (LogToStdout) {
	write(1, LogBuf, len);
	return;
    }

    logfd = open(LogFile, O_CREAT|O_WRONLY|O_APPEND, 0644);
    if (logfd >= 0) {
	write(logfd, LogBuf, len);
	close(logfd);
    } else {
	fprintf(stderr, "Can't open %s\n", LogFile);
    }
}

