AmiTimeKeeper/logger.c

282 lines
7.4 KiB
C
Raw Permalink Normal View History

2021-01-12 22:02:29 +00:00
/*-
2021-01-12 22:16:18 +00:00
* Copyright (c) 2017-2021 Carsten Sonne Larsen <cs@innolan.net>
2021-01-12 22:02:29 +00:00
* 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>
2021-01-12 22:16:18 +00:00
#include <proto/intuition.h>
2021-01-12 22:02:29 +00:00
#include <exec/ports.h>
#include <exec/memory.h>
2021-01-12 22:16:18 +00:00
#include "message.h"
#include "global.h"
2021-01-12 22:02:29 +00:00
#include "string.h"
2021-01-12 22:16:18 +00:00
#ifndef INTUITIONNAME
#define INTUITIONNAME "intuition.library"
#endif
#ifndef UTILITYNAME
#define UTILITYNAME "utility.library"
#endif
2021-01-12 22:02:29 +00:00
2021-01-31 19:10:59 +00:00
#define TIMELOGGER "TimeLogger 1.12 (31.01.2021)"
2021-01-12 22:16:18 +00:00
#define ARGSFORMAT "D=DEBUG/K/N,M=MODULEOFF/S,S=SEVERITYOFF/S,H=HELP/S"
#define MODULENAME "Logger"
2021-01-12 22:02:29 +00:00
2021-01-12 22:16:18 +00:00
struct progargs
2021-01-12 22:02:29 +00:00
{
2021-01-12 22:16:18 +00:00
LONG level;
LONG module;
LONG severity;
LONG help;
2021-01-12 22:02:29 +00:00
};
2021-01-12 22:16:18 +00:00
static void LogMessage(enum LogSeverity, struct timeval *, const char *, const char *);
static void MsgLoop(void);
static void ShowHelp(void);
2021-01-12 22:02:29 +00:00
2021-01-12 22:16:18 +00:00
struct IntuitionBase *IntuitionBase = NULL;
struct UtilityBase *UtilityBase = NULL;
static struct MsgPort *LoggerPort = NULL;
static struct RDArgs *rdargs = NULL;
static struct progargs args = {0, 0, 0, 0};
static long SeverityLevel;
static char LogTemplate[50];
const char *vers = "\0$VER: " TIMELOGGER;
static void Listen(void)
{
LogMessage(InfoMessage, NULL, MODULENAME, "Starting " TIMELOGGER);
2021-01-12 22:02:29 +00:00
LoggerPort->mp_Node.ln_Name = LOGGER_PORT_NAME;
LoggerPort->mp_Node.ln_Pri = 0;
AddPort(LoggerPort);
MsgLoop();
RemPort(LoggerPort);
2021-01-12 22:16:18 +00:00
}
static void ProcessArgs(void)
{
if (args.level != 0)
SeverityLevel = *((LONG *)args.level);
else
SeverityLevel = 15;
2021-01-12 22:02:29 +00:00
2021-01-12 22:16:18 +00:00
char *p = LogTemplate;
p = AppendText(p, "%02ld:%02ld:%02ld.%03ld ");
if (!args.module)
p = AppendText(p, "[%-8s]");
else
p = AppendText(p, "%s");
if (!args.severity)
p = AppendText(p, "[%-5s]");
else
p = AppendText(p, "%s");
if (!(args.severity && args.module))
p = AppendChar(p, ' ', 1);
AppendText(p, "%s");
}
bool Init(void)
{
rdargs = ReadArgs((STRPTR)ARGSFORMAT, (APTR)&args, NULL);
if (!rdargs)
return false;
LoggerPort = CreateMsgPort();
if (LoggerPort == NULL)
return false;
if (!(UtilityBase = (struct UtilityBase *)OpenLibrary((STRPTR)UTILITYNAME, 0)))
return false;
if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary((STRPTR)INTUITIONNAME, 0)))
return false;
return true;
}
void Cleanup(void)
{
if (LoggerPort != NULL)
DeleteMsgPort(LoggerPort);
if (rdargs != NULL)
FreeArgs(rdargs);
if (IntuitionBase != NULL)
CloseLibrary((struct Library *)IntuitionBase);
if (UtilityBase != NULL)
CloseLibrary((struct Library *)UtilityBase);
}
int main(int argc, char **argv)
{
if (!Init())
{
Cleanup();
return RETURN_FAIL;
}
if (args.help)
{
ShowHelp();
}
else
{
ProcessArgs();
Listen();
}
Cleanup();
return RETURN_OK;
2021-01-12 22:02:29 +00:00
}
static const char *SeverityText(enum LogSeverity level)
{
switch (level)
{
case ErrorMessage:
2021-01-12 22:16:18 +00:00
return "Error";
2021-01-12 22:02:29 +00:00
case WarningMessage:
2021-01-12 22:16:18 +00:00
return "Warn";
case NoticeMessage:
return "Notice";
2021-01-12 22:02:29 +00:00
case InfoMessage:
2021-01-12 22:16:18 +00:00
return "Info";
2021-01-12 22:02:29 +00:00
case DebugMessage:
2021-01-12 22:16:18 +00:00
return "Debug";
2021-01-12 22:02:29 +00:00
case TraceMessage:
case MemTraceMessage:
2021-01-12 22:16:18 +00:00
return "Trace";
2021-01-12 22:02:29 +00:00
default:
2021-01-12 22:16:18 +00:00
return "Unspec";
2021-01-12 22:02:29 +00:00
}
}
2021-01-12 22:16:18 +00:00
static void LogMessage(enum LogSeverity level, struct timeval *tv, const char *module, const char *log)
2021-01-12 22:02:29 +00:00
{
2021-01-12 22:16:18 +00:00
if (SeverityLevel >= (long)level)
2021-01-12 22:02:29 +00:00
{
2021-01-12 22:16:18 +00:00
ULONG secs;
ULONG micro;
if (tv == NULL)
{
CurrentTime(&secs, &micro);
}
else
{
secs = tv->tv_secs;
micro = tv->tv_micro;
}
micro /= 1000;
struct ClockData cd;
Amiga2Date(secs, &cd);
2021-01-12 22:02:29 +00:00
char line[MAXLOGLINESIZE + 32];
const char *severity = SeverityText(level);
2021-01-12 22:16:18 +00:00
SNPrintf(line, MAXLOGLINESIZE + 32, LogTemplate,
(long)cd.hour,
(long)cd.min,
(long)cd.sec,
(long)micro,
(args.module ? "" : module),
(args.severity ? "" : severity),
log);
2021-01-12 22:02:29 +00:00
TrimRight(line);
// TODO: Option to log using a file
2021-01-12 22:16:18 +00:00
// TODO: Handle outout errors
2021-01-12 22:02:29 +00:00
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;
2021-01-12 22:16:18 +00:00
LogMessage(NoticeMessage, NULL, MODULENAME, "Waiting for log messages ...");
2021-01-12 22:02:29 +00:00
do
{
ULONG sigrcvd = Wait(sigMask);
if (sigrcvd & sigMask)
{
struct LogMessage *msg;
while ((msg = (struct LogMessage *)GetMsg(LoggerPort)))
{
2021-01-12 22:16:18 +00:00
struct timeval tv;
2021-01-12 22:02:29 +00:00
struct LogMessage copy;
2021-01-12 22:16:18 +00:00
CurrentTime(&tv.tv_secs, &tv.tv_micro);
2021-01-12 22:02:29 +00:00
CopyMem(msg, &copy, sizeof(struct LogMessage));
ReplyMsg((struct Message *)msg);
2021-01-12 22:16:18 +00:00
LogMessage(copy.Severity, &tv, copy.Module, copy.Text);
2021-01-12 22:02:29 +00:00
}
}
if (sigrcvd & SIGBREAKF_CTRL_C)
{
2021-01-12 22:16:18 +00:00
LogMessage(WarningMessage, NULL, MODULENAME, "Shutting down");
2021-01-12 22:02:29 +00:00
loop = false;
}
} while (loop);
}
2021-01-12 22:16:18 +00:00
static void ShowHelp(void)
{
Printf((STRPTR)TIMELOGGER "\n");
Printf((STRPTR) "Shows log messages from " APP_SHORT_NAME ".\n");
Printf((STRPTR) "Use arguments to control output.\n");
Printf((STRPTR) "Debug level (D or DEBUG):\n");
Printf((STRPTR) " 5 = Error conditions\n");
Printf((STRPTR) " 10 = Warning conditions\n");
Printf((STRPTR) " 15 = Normal but significant conditions\n");
Printf((STRPTR) " 20 = Informational messages\n");
Printf((STRPTR) " 30 = Debug-level messages\n");
Printf((STRPTR) " 40 = Trace program flow\n");
Printf((STRPTR) " 90 = Trace memory exceptions\n");
Printf((STRPTR) "MODULEOFF: Hide module name\n");
Printf((STRPTR) "SEVERITYOFF: Hide severity level\n");
Printf((STRPTR) "HELP: Show help text\n");
}