Allow logging to the syslog

This commit is contained in:
Jordan Bancino 2022-10-14 20:55:15 -04:00
parent e1827da071
commit 0c03c71081
6 changed files with 66 additions and 33 deletions

View file

@ -57,7 +57,7 @@ Phase 2: Building a foundation
[x] Properly implement the command line options as stated in telodendria(8)
[x] Remove "chroot" option, just chroot into the data dir, and make the log
file live there as well.
[~] Allow logging to the syslog
[x] Allow logging to the syslog
[ ] Fix bug where the socket stays open after quit.
Phase 3: Welcome to Matrix

View file

@ -1,4 +1,4 @@
.Dd $Mdocdate: October 11 2022 $
.Dd $Mdocdate: October 15 2022 $
.Dt TELODENDRIA.CONF 5
.Os Telodendria Project
.Sh NAME
@ -123,9 +123,16 @@ to run their own homeserver, you can aset this to
which will allow anyone to create an account. Telodendria should be capable of handling
a large amount of users without difficulty or security issues. This directive is
required.
.It Ic log Ar stdout|file
.It Ic log Ar stdout|file|syslog
The log configuration. Telodendria uses its own logging facility, which can output
logs to standard output or a file. A number of child directives can
logs to standard output, a file, or the syslog. If set to
.Ar file ,
Telodendria will log to
.Pa telodendria.log
inside the
.Ic data-dir .
.Pp
A number of child directives can
be added to this directive to customize the log output:
.Bl -tag -width Ds
.It Ic level Ar error|warning|task|message|debug
@ -146,8 +153,7 @@ altogether, you can do so via this option. Acceptable values are
.Ar default ,
or a formatter string as described by your system's
.Xr strftime 3 .
In the future, logging to the syslog may also be an option. In that case, this
option only applies if
This option only applies if
.Ic log
is "stdout" or "file".
.It Ic color Ar true|false

View file

@ -36,7 +36,7 @@
struct LogConfig
{
LogLevel level;
int level;
size_t indent;
FILE *out;
int flags;
@ -133,7 +133,7 @@ LogConfigIndentSet(LogConfig * config, size_t indent)
config->indent = indent;
}
LogLevel
int
LogConfigLevelGet(LogConfig * config)
{
if (!config)
@ -145,7 +145,7 @@ LogConfigLevelGet(LogConfig * config)
}
void
LogConfigLevelSet(LogConfig * config, LogLevel level)
LogConfigLevelSet(LogConfig * config, int level)
{
if (!config)
{
@ -202,7 +202,7 @@ LogConfigUnindent(LogConfig * config)
}
void
Log(LogConfig * config, LogLevel level, const char *msg,...)
Log(LogConfig * config, int level, const char *msg,...)
{
size_t i;
int doColor;
@ -228,6 +228,16 @@ Log(LogConfig * config, LogLevel level, const char *msg,...)
pthread_mutex_lock(&config->lock);
if (LogConfigFlagGet(config, LOG_FLAG_SYSLOG))
{
/* No further print logic is needed; syslog will handle it all
* for us. */
va_start(argp, msg);
vsyslog(level, msg, argp);
va_end(argp);
pthread_mutex_unlock(&config->lock);
return;
}
doColor = LogConfigFlagGet(config, LOG_FLAG_COLOR)
&& isatty(fileno(config->out));
@ -238,7 +248,10 @@ Log(LogConfig * config, LogLevel level, const char *msg,...)
switch (level)
{
case LOG_ERROR:
case LOG_EMERG:
case LOG_ALERT:
case LOG_CRIT:
case LOG_ERR:
/* Bold Red */
ansi = "\033[1;31m";
break;
@ -246,11 +259,11 @@ Log(LogConfig * config, LogLevel level, const char *msg,...)
/* Bold Yellow */
ansi = "\033[1;33m";
break;
case LOG_TASK:
case LOG_NOTICE:
/* Bold Magenta */
ansi = "\033[1;35m";
break;
case LOG_MESSAGE:
case LOG_INFO:
/* Bold Green */
ansi = "\033[1;32m";
break;
@ -289,6 +302,15 @@ Log(LogConfig * config, LogLevel level, const char *msg,...)
switch (level)
{
case LOG_EMERG:
indicator = '#';
break;
case LOG_ALERT:
indicator = '@';
break;
case LOG_CRIT:
indicator = 'X';
break;
case LOG_ERROR:
indicator = 'x';
break;

View file

@ -311,9 +311,13 @@ main(int argc, char **argv)
}
else if (tConfig->flags & TELODENDRIA_LOG_SYSLOG)
{
Log(lc, LOG_ERROR, "Logging to the syslog is not yet supported.");
exit = EXIT_FAILURE;
goto finish;
Log(lc, LOG_MESSAGE, "Logging to the syslog. Check there for all future messages.");
LogConfigFlagSet(lc, LOG_FLAG_SYSLOG);
openlog("telodendria", LOG_PID | LOG_NDELAY, LOG_DAEMON);
/* Always log everything, because the Log API will control what
* messages get passed to the syslog */
setlogmask(LOG_UPTO(LOG_DEBUG));
}
else
{

View file

@ -44,20 +44,19 @@
#include <stdio.h>
#include <stddef.h>
#include <syslog.h>
/*
* There are five log "levels," each one showing more information than
* the previous one. A level of LOG_ERROR shows only errors, while a
* level of LOG_DEBUG shows all output possible.
* I used to define all my own constants, but now I use
* those defined in syslog.h. Instead of replacing all the
* references, I just map the old names to the new ones. If
* you're ever bored one day, you can remove these, and then
* go fix all the compiler errors that arise. Should be pretty
* easy, just mind numbing.
*/
typedef enum LogLevel
{
LOG_ERROR,
LOG_WARNING,
LOG_TASK,
LOG_MESSAGE,
LOG_DEBUG
} LogLevel;
#define LOG_ERROR LOG_ERR
#define LOG_TASK LOG_NOTICE
#define LOG_MESSAGE LOG_INFO
/*
* The possible flags that can be applied to alter the behavior of
@ -65,7 +64,9 @@ typedef enum LogLevel
*/
typedef enum LogFlag
{
LOG_FLAG_COLOR = (1 << 0) /* Enable color output on TTYs */
LOG_FLAG_COLOR = (1 << 0), /* Enable color output on TTYs */
LOG_FLAG_SYSLOG = (1 << 1) /* Log to the syslog instead of a
* file */
} LogFlag;
/*
@ -110,10 +111,10 @@ extern void
* Params:
*
* (LogConfig *) The log configuration to set the log level on.
* (LogLevel) The log level to set.
* (int) The log level to set.
*/
extern void
LogConfigLevelSet(LogConfig *, LogLevel);
LogConfigLevelSet(LogConfig *, int);
/*
* Indent the log output by two spaces. This can be helpful in
@ -185,12 +186,12 @@ extern void
* Params:
*
* (LogConfig *) The logging configuration.
* (LogLevel) The level of the message to log.
* (int) The level of the message to log.
* (const char *) The format string, or a plain message string.
* (...) Any items to map into the format string, printf()
* style.
*/
extern void
Log(LogConfig *, LogLevel, const char *,...);
Log(LogConfig *, int, const char *,...);
#endif

View file

@ -69,7 +69,7 @@ typedef struct TelodendriaConfig
unsigned int maxConnections;
char *logTimestamp;
LogLevel logLevel;
int logLevel;
} TelodendriaConfig;
/*