mirror of
https://git.telodendria.io/Telodendria/Cytoplasm.git
synced 2025-04-25 18:36:03 +00:00
redesign logging
This commit is contained in:
parent
39c25e5a17
commit
36575b947f
2 changed files with 62 additions and 63 deletions
107
src/Log.c
107
src/Log.c
|
@ -26,6 +26,7 @@
|
|||
#include <Memory.h>
|
||||
#include <Util.h>
|
||||
|
||||
#include <cstdarg>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
@ -39,7 +40,7 @@ struct LogConfig
|
|||
{
|
||||
int level;
|
||||
size_t indent;
|
||||
Stream *out;
|
||||
Stream *file;
|
||||
int flags;
|
||||
char *tsFmt;
|
||||
|
||||
|
@ -65,8 +66,8 @@ LogConfigCreate(void)
|
|||
|
||||
LogConfigLevelSet(config, LOG_INFO);
|
||||
LogConfigIndentSet(config, 0);
|
||||
LogConfigOutputSet(config, NULL); /* Will set to stdout */
|
||||
LogConfigFlagSet(config, LOG_FLAG_COLOR);
|
||||
LogConfigFileSet(config, NULL);
|
||||
LogConfigFlagSet(config, LOG_FLAG_COLOR | LOG_FLAG_STDOUT);
|
||||
LogConfigTimeStampFormatSet(config, "%y-%m-%d %H:%M:%S");
|
||||
|
||||
return config;
|
||||
|
@ -185,20 +186,20 @@ LogConfigLevelSet(LogConfig * config, int level)
|
|||
}
|
||||
|
||||
void
|
||||
LogConfigOutputSet(LogConfig * config, Stream * out)
|
||||
LogConfigFileSet(LogConfig * config, Stream * file)
|
||||
{
|
||||
if (!config)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (out)
|
||||
if (file)
|
||||
{
|
||||
config->out = out;
|
||||
config->file = file;
|
||||
}
|
||||
else
|
||||
{
|
||||
config->out = StreamStdout();
|
||||
config->file = StreamStdout();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -222,42 +223,14 @@ LogConfigUnindent(LogConfig * config)
|
|||
}
|
||||
|
||||
void
|
||||
Logv(LogConfig * config, int level, const char *msg, va_list argp)
|
||||
LogPrint(Stream * stream, LogConfig * config, int level, const char *msg, va_list argp)
|
||||
{
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
doColor = LogConfigFlagGet(config, LOG_FLAG_COLOR)
|
||||
&& isatty(StreamFileno(config->out));
|
||||
&& isatty(StreamFileno(stream));
|
||||
|
||||
if (doColor)
|
||||
{
|
||||
|
@ -293,10 +266,10 @@ Logv(LogConfig * config, int level, const char *msg, va_list argp)
|
|||
break;
|
||||
}
|
||||
|
||||
StreamPuts(config->out, ansi);
|
||||
StreamPuts(stream, ansi);
|
||||
}
|
||||
|
||||
StreamPutc(config->out, '[');
|
||||
StreamPutc(stream, '[');
|
||||
|
||||
if (config->tsFmt)
|
||||
{
|
||||
|
@ -309,15 +282,15 @@ Logv(LogConfig * config, int level, const char *msg, va_list argp)
|
|||
|
||||
if (tsLength)
|
||||
{
|
||||
StreamPuts(config->out, tsBuffer);
|
||||
StreamPuts(stream, tsBuffer);
|
||||
if (!isspace((unsigned char) tsBuffer[tsLength - 1]))
|
||||
{
|
||||
StreamPutc(config->out, ' ');
|
||||
StreamPutc(stream, ' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StreamPrintf(config->out, "(%lu) ", UtilThreadNo());
|
||||
StreamPrintf(stream, "(%lu) ", UtilThreadNo());
|
||||
|
||||
switch (level)
|
||||
{
|
||||
|
@ -350,24 +323,60 @@ Logv(LogConfig * config, int level, const char *msg, va_list argp)
|
|||
break;
|
||||
}
|
||||
|
||||
StreamPrintf(config->out, "%c]", indicator);
|
||||
StreamPrintf(stream, "%c]", indicator);
|
||||
|
||||
if (doColor)
|
||||
{
|
||||
/* ANSI Reset */
|
||||
StreamPuts(config->out, "\033[0m");
|
||||
StreamPuts(stream, "\033[0m");
|
||||
}
|
||||
|
||||
StreamPutc(config->out, ' ');
|
||||
StreamPutc(stream, ' ');
|
||||
for (i = 0; i < config->indent; i++)
|
||||
{
|
||||
StreamPutc(config->out, ' ');
|
||||
StreamPutc(config->file, ' ');
|
||||
}
|
||||
|
||||
StreamVprintf(config->out, msg, argp);
|
||||
StreamPutc(config->out, '\n');
|
||||
StreamVprintf(stream, msg, argp);
|
||||
StreamPutc(stream, '\n');
|
||||
|
||||
StreamFlush(config->out);
|
||||
StreamFlush(stream);
|
||||
}
|
||||
|
||||
void
|
||||
Logv(LogConfig * config, int level, const char *msg, va_list argp)
|
||||
{
|
||||
/*
|
||||
* 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->file)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&config->lock);
|
||||
|
||||
if (LogConfigFlagGet(config, LOG_FLAG_SYSLOG))
|
||||
{
|
||||
vsyslog(level, msg, argp);
|
||||
}
|
||||
|
||||
if (LogConfigFlagGet(config, LOG_FLAG_STDOUT)) {
|
||||
LogPrint(StreamStdout(), config, level, msg, argp);
|
||||
}
|
||||
|
||||
if (config->file) {
|
||||
LogPrint(config->file, config, level, msg, argp);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&config->lock);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue