AmiTimeKeeper/logger.c

159 lines
4.2 KiB
C

/*-
* Copyright (c) 2017-2020 Carsten Sonne Larsen <cs@innolan.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stddef.h>
#include <stdarg.h>
#include <stdbool.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <exec/ports.h>
#include <exec/memory.h>
#include "logger.h"
#include "string.h"
static void MsgLoop(void);
static int VerboseLevel = 10;
static struct MsgPort *LoggerPort;
const char *vers = "\0$VER: TimeLogger 1.10 (20.03.2020)";
/*
struct LogMessage
{
struct timeval time;
enum LogSeverity severity;
const char *module;
const char *message;
struct LogMessage *next;
};
*/
int main(int argc, char **argv)
{
Printf((STRPTR) "Starting TimeLogger.\n");
LoggerPort = CreateMsgPort();
if (LoggerPort == NULL)
{
Printf((STRPTR) "Could not message port\n");
return 10;
}
LoggerPort->mp_Node.ln_Name = LOGGER_PORT_NAME;
LoggerPort->mp_Node.ln_Pri = 0;
AddPort(LoggerPort);
MsgLoop();
RemPort(LoggerPort);
DeleteMsgPort(LoggerPort);
return 0;
}
static const char *SeverityText(enum LogSeverity level)
{
switch (level)
{
case ErrorMessage:
return "[Error]";
case WarningMessage:
return "[Warn ]";
case InfoMessage:
return "[Info ]";
case DebugMessage:
return "[Debug]";
case TraceMessage:
case MemTraceMessage:
return "[Trace]";
default:
return "[Trace]";
}
}
static void LogMessage(enum LogSeverity level, const char *module, const char *log)
{
if (VerboseLevel * 10 >= (int)level)
{
char line[MAXLOGLINESIZE + 32];
const char *severity = SeverityText(level);
SNPrintf(line, MAXLOGLINESIZE + 32, "%s[%s] %s", severity, module, log);
TrimRight(line);
// TODO: Option to log using a file
if (Output() != (BPTR)0)
{
Printf((STRPTR)line);
Printf((STRPTR) "\n");
}
}
}
static void MsgLoop(void)
{
bool loop = true;
ULONG loggerSigMask = (1 << LoggerPort->mp_SigBit);
ULONG sigMask = loggerSigMask | SIGBREAKF_CTRL_C;
Printf((STRPTR) "Waiting for log messages ...\n");
do
{
ULONG sigrcvd = Wait(sigMask);
if (sigrcvd & sigMask)
{
struct LogMessage *msg;
while ((msg = (struct LogMessage *)GetMsg(LoggerPort)))
{
struct LogMessage copy;
CopyMem(msg, &copy, sizeof(struct LogMessage));
ReplyMsg((struct Message *)msg);
switch (copy.Type)
{
case LogMessageText:
LogMessage(copy.Severity, copy.Module, copy.Text);
break;
case LogMessageShutdown:
loop = false;
break;
default:
break;
}
}
}
if (sigrcvd & SIGBREAKF_CTRL_C)
{
Printf((STRPTR) "Shutting down\n");
loop = false;
}
} while (loop);
}