Telodendria/src/Log.c

390 lines
7.5 KiB
C
Raw Normal View History

/*
2022-12-26 15:52:52 +00:00
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
2022-07-28 16:00:52 +00:00
* included in all copies or portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2022-07-23 00:19:12 +00:00
#include <Log.h>
#include <Memory.h>
#include <string.h>
2022-07-23 00:19:12 +00:00
#include <time.h>
#include <unistd.h>
#include <ctype.h>
#include <stdarg.h>
2022-07-29 19:36:21 +00:00
#include <pthread.h>
2022-07-23 00:19:12 +00:00
#define LOG_TSBUFFER 64
2022-07-25 19:18:35 +00:00
struct LogConfig
{
2022-10-15 00:55:15 +00:00
int level;
2022-07-23 00:19:12 +00:00
size_t indent;
Stream *out;
2022-07-23 00:19:12 +00:00
int flags;
char *tsFmt;
2022-07-29 19:36:21 +00:00
pthread_mutex_t lock;
};
2022-07-23 00:19:12 +00:00
LogConfig *globalConfig = NULL;
2022-07-23 00:19:12 +00:00
LogConfig *
LogConfigCreate(void)
{
LogConfig *config;
config = Malloc(sizeof(LogConfig));
2022-07-23 00:19:12 +00:00
if (!config)
{
return NULL;
}
memset(config, 0, sizeof(LogConfig));
2022-11-24 18:51:07 +00:00
LogConfigLevelSet(config, LOG_INFO);
2022-07-23 00:19:12 +00:00
LogConfigIndentSet(config, 0);
2022-07-25 19:18:35 +00:00
LogConfigOutputSet(config, NULL); /* Will set to stdout */
2022-07-23 00:19:12 +00:00
LogConfigFlagSet(config, LOG_FLAG_COLOR);
LogConfigTimeStampFormatSet(config, "%y-%m-%d %H:%M:%S");
return config;
}
LogConfig *
LogConfigGlobal(void)
{
if (!globalConfig)
{
globalConfig = LogConfigCreate();
}
return globalConfig;
}
2022-07-23 00:19:12 +00:00
void
2022-07-25 19:18:35 +00:00
LogConfigFlagClear(LogConfig * config, int flags)
2022-07-23 00:19:12 +00:00
{
if (!config)
{
return;
}
config->flags &= ~flags;
}
2022-07-29 19:36:21 +00:00
static int
2022-07-25 19:18:35 +00:00
LogConfigFlagGet(LogConfig * config, int flags)
2022-07-23 00:19:12 +00:00
{
if (!config)
{
return 0;
}
return config->flags & flags;
}
void
2022-07-25 19:18:35 +00:00
LogConfigFlagSet(LogConfig * config, int flags)
2022-07-23 00:19:12 +00:00
{
if (!config)
{
return;
}
config->flags |= flags;
}
void
2022-07-25 19:18:35 +00:00
LogConfigFree(LogConfig * config)
2022-07-23 00:19:12 +00:00
{
if (!config)
{
return;
}
StreamClose(config->out);
Free(config);
if (config == globalConfig)
{
globalConfig = NULL;
}
2022-07-23 00:19:12 +00:00
}
void
2022-07-25 19:18:35 +00:00
LogConfigIndent(LogConfig * config)
2022-07-23 00:19:12 +00:00
{
if (config)
{
config->indent += 2;
}
}
void
2022-07-25 19:18:35 +00:00
LogConfigIndentSet(LogConfig * config, size_t indent)
2022-07-23 00:19:12 +00:00
{
if (!config)
{
return;
}
config->indent = indent;
}
2022-10-15 00:55:15 +00:00
int
2022-07-25 19:18:35 +00:00
LogConfigLevelGet(LogConfig * config)
2022-07-23 00:19:12 +00:00
{
if (!config)
{
return -1;
}
return config->level;
}
void
2022-10-15 00:55:15 +00:00
LogConfigLevelSet(LogConfig * config, int level)
2022-07-23 00:19:12 +00:00
{
if (!config)
{
return;
}
switch (level)
{
2022-11-24 18:51:07 +00:00
case LOG_ERR:
2022-07-23 00:19:12 +00:00
case LOG_WARNING:
2022-11-24 18:51:07 +00:00
case LOG_INFO:
2022-07-23 00:19:12 +00:00
case LOG_DEBUG:
config->level = level;
default:
break;
}
}
void
LogConfigOutputSet(LogConfig * config, Stream * out)
2022-07-23 00:19:12 +00:00
{
if (!config)
{
return;
}
if (out)
{
config->out = out;
}
else
{
config->out = StreamStdout();
2022-07-23 00:19:12 +00:00
}
}
void
2022-07-25 19:18:35 +00:00
LogConfigTimeStampFormatSet(LogConfig * config, char *tsFmt)
2022-07-23 00:19:12 +00:00
{
if (config)
{
config->tsFmt = tsFmt;
}
}
void
2022-07-25 19:18:35 +00:00
LogConfigUnindent(LogConfig * config)
2022-07-23 00:19:12 +00:00
{
if (config && config->indent >= 2)
{
config->indent -= 2;
}
}
2022-07-29 19:36:21 +00:00
void
Logv(LogConfig * config, int level, const char *msg, va_list argp)
2022-07-29 19:36:21 +00:00
{
size_t i;
int doColor;
char indicator;
/*
* Only proceed if we have a config and its log level is set to a
* value that permits us to log. This is as close as we can get
* to a no-op function if we aren't logging anything, without doing
* some crazy macro magic.
*/
if (!config || level > config->level)
{
return;
}
/* Misconfiguration */
if (!config->out)
{
return;
}
pthread_mutex_lock(&config->lock);
2022-10-15 00:55:15 +00:00
if (LogConfigFlagGet(config, LOG_FLAG_SYSLOG))
{
/* No further print logic is needed; syslog will handle it all
* for us. */
vsyslog(level, msg, argp);
pthread_mutex_unlock(&config->lock);
return;
}
2022-07-29 19:36:21 +00:00
doColor = LogConfigFlagGet(config, LOG_FLAG_COLOR)
&& isatty(StreamFileno(config->out));
2022-07-29 19:36:21 +00:00
if (doColor)
{
char *ansi;
switch (level)
{
2022-10-15 00:55:15 +00:00
case LOG_EMERG:
case LOG_ALERT:
case LOG_CRIT:
case LOG_ERR:
2022-07-29 19:36:21 +00:00
/* Bold Red */
ansi = "\033[1;31m";
break;
case LOG_WARNING:
/* Bold Yellow */
ansi = "\033[1;33m";
break;
2022-10-15 00:55:15 +00:00
case LOG_NOTICE:
2022-07-29 19:36:21 +00:00
/* Bold Magenta */
ansi = "\033[1;35m";
break;
2022-10-15 00:55:15 +00:00
case LOG_INFO:
2022-07-29 19:36:21 +00:00
/* Bold Green */
ansi = "\033[1;32m";
break;
case LOG_DEBUG:
/* Bold Blue */
ansi = "\033[1;34m";
break;
default:
ansi = "";
break;
}
StreamPuts(config->out, ansi);
2022-07-29 19:36:21 +00:00
}
StreamPutc(config->out, '[');
2022-07-29 19:36:21 +00:00
if (config->tsFmt)
{
time_t timer = time(NULL);
struct tm *timeInfo = localtime(&timer);
char tsBuffer[LOG_TSBUFFER];
int tsLength = strftime(tsBuffer, LOG_TSBUFFER, config->tsFmt,
timeInfo);
if (tsLength)
{
StreamPuts(config->out, tsBuffer);
if (!isspace((unsigned char) tsBuffer[tsLength - 1]))
2022-07-29 19:36:21 +00:00
{
StreamPutc(config->out, ' ');
2022-07-29 19:36:21 +00:00
}
}
}
switch (level)
{
2022-10-15 00:55:15 +00:00
case LOG_EMERG:
indicator = '#';
break;
case LOG_ALERT:
indicator = '@';
break;
case LOG_CRIT:
indicator = 'X';
break;
2022-11-24 18:51:07 +00:00
case LOG_ERR:
2022-07-29 19:36:21 +00:00
indicator = 'x';
break;
case LOG_WARNING:
indicator = '!';
break;
2022-11-24 18:51:07 +00:00
case LOG_NOTICE:
2022-07-29 19:36:21 +00:00
indicator = '~';
break;
2022-11-24 18:51:07 +00:00
case LOG_INFO:
2022-07-29 19:36:21 +00:00
indicator = '>';
break;
case LOG_DEBUG:
indicator = '*';
break;
default:
indicator = '?';
break;
}
StreamPrintf(config->out, "%c]", indicator);
2022-07-29 19:36:21 +00:00
if (doColor)
{
/* ANSI Reset */
StreamPuts(config->out, "\033[0m");
2022-07-29 19:36:21 +00:00
}
StreamPutc(config->out, ' ');
for (i = 0; i < config->indent; i++)
{
StreamPutc(config->out, ' ');
}
2022-07-29 19:36:21 +00:00
StreamVprintf(config->out, msg, argp);
StreamPutc(config->out, '\n');
2022-07-29 19:36:21 +00:00
StreamFlush(config->out);
2022-07-29 19:36:21 +00:00
pthread_mutex_unlock(&config->lock);
}
void
LogTo(LogConfig * config, int level, const char *fmt,...)
{
va_list argp;
va_start(argp, fmt);
Logv(config, level, fmt, argp);
va_end(argp);
}
extern void
Log(int level, const char *fmt,...)
{
va_list argp;
va_start(argp, fmt);
Logv(LogConfigGlobal(), level, fmt, argp);
va_end(argp);
}