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] 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 [x] Remove "chroot" option, just chroot into the data dir, and make the log
file live there as well. 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. [ ] Fix bug where the socket stays open after quit.
Phase 3: Welcome to Matrix 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 .Dt TELODENDRIA.CONF 5
.Os Telodendria Project .Os Telodendria Project
.Sh NAME .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 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 a large amount of users without difficulty or security issues. This directive is
required. 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 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: be added to this directive to customize the log output:
.Bl -tag -width Ds .Bl -tag -width Ds
.It Ic level Ar error|warning|task|message|debug .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 , .Ar default ,
or a formatter string as described by your system's or a formatter string as described by your system's
.Xr strftime 3 . .Xr strftime 3 .
In the future, logging to the syslog may also be an option. In that case, this This option only applies if
option only applies if
.Ic log .Ic log
is "stdout" or "file". is "stdout" or "file".
.It Ic color Ar true|false .It Ic color Ar true|false

View file

@ -36,7 +36,7 @@
struct LogConfig struct LogConfig
{ {
LogLevel level; int level;
size_t indent; size_t indent;
FILE *out; FILE *out;
int flags; int flags;
@ -133,7 +133,7 @@ LogConfigIndentSet(LogConfig * config, size_t indent)
config->indent = indent; config->indent = indent;
} }
LogLevel int
LogConfigLevelGet(LogConfig * config) LogConfigLevelGet(LogConfig * config)
{ {
if (!config) if (!config)
@ -145,7 +145,7 @@ LogConfigLevelGet(LogConfig * config)
} }
void void
LogConfigLevelSet(LogConfig * config, LogLevel level) LogConfigLevelSet(LogConfig * config, int level)
{ {
if (!config) if (!config)
{ {
@ -202,7 +202,7 @@ LogConfigUnindent(LogConfig * config)
} }
void void
Log(LogConfig * config, LogLevel level, const char *msg,...) Log(LogConfig * config, int level, const char *msg,...)
{ {
size_t i; size_t i;
int doColor; int doColor;
@ -228,6 +228,16 @@ Log(LogConfig * config, LogLevel level, const char *msg,...)
pthread_mutex_lock(&config->lock); 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) doColor = LogConfigFlagGet(config, LOG_FLAG_COLOR)
&& isatty(fileno(config->out)); && isatty(fileno(config->out));
@ -238,7 +248,10 @@ Log(LogConfig * config, LogLevel level, const char *msg,...)
switch (level) switch (level)
{ {
case LOG_ERROR: case LOG_EMERG:
case LOG_ALERT:
case LOG_CRIT:
case LOG_ERR:
/* Bold Red */ /* Bold Red */
ansi = "\033[1;31m"; ansi = "\033[1;31m";
break; break;
@ -246,11 +259,11 @@ Log(LogConfig * config, LogLevel level, const char *msg,...)
/* Bold Yellow */ /* Bold Yellow */
ansi = "\033[1;33m"; ansi = "\033[1;33m";
break; break;
case LOG_TASK: case LOG_NOTICE:
/* Bold Magenta */ /* Bold Magenta */
ansi = "\033[1;35m"; ansi = "\033[1;35m";
break; break;
case LOG_MESSAGE: case LOG_INFO:
/* Bold Green */ /* Bold Green */
ansi = "\033[1;32m"; ansi = "\033[1;32m";
break; break;
@ -289,6 +302,15 @@ Log(LogConfig * config, LogLevel level, const char *msg,...)
switch (level) switch (level)
{ {
case LOG_EMERG:
indicator = '#';
break;
case LOG_ALERT:
indicator = '@';
break;
case LOG_CRIT:
indicator = 'X';
break;
case LOG_ERROR: case LOG_ERROR:
indicator = 'x'; indicator = 'x';
break; break;

View file

@ -311,9 +311,13 @@ main(int argc, char **argv)
} }
else if (tConfig->flags & TELODENDRIA_LOG_SYSLOG) else if (tConfig->flags & TELODENDRIA_LOG_SYSLOG)
{ {
Log(lc, LOG_ERROR, "Logging to the syslog is not yet supported."); Log(lc, LOG_MESSAGE, "Logging to the syslog. Check there for all future messages.");
exit = EXIT_FAILURE; LogConfigFlagSet(lc, LOG_FLAG_SYSLOG);
goto finish;
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 else
{ {

View file

@ -44,20 +44,19 @@
#include <stdio.h> #include <stdio.h>
#include <stddef.h> #include <stddef.h>
#include <syslog.h>
/* /*
* There are five log "levels," each one showing more information than * I used to define all my own constants, but now I use
* the previous one. A level of LOG_ERROR shows only errors, while a * those defined in syslog.h. Instead of replacing all the
* level of LOG_DEBUG shows all output possible. * 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 #define LOG_ERROR LOG_ERR
{ #define LOG_TASK LOG_NOTICE
LOG_ERROR, #define LOG_MESSAGE LOG_INFO
LOG_WARNING,
LOG_TASK,
LOG_MESSAGE,
LOG_DEBUG
} LogLevel;
/* /*
* The possible flags that can be applied to alter the behavior of * The possible flags that can be applied to alter the behavior of
@ -65,7 +64,9 @@ typedef enum LogLevel
*/ */
typedef enum LogFlag 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; } LogFlag;
/* /*
@ -110,10 +111,10 @@ extern void
* Params: * Params:
* *
* (LogConfig *) The log configuration to set the log level on. * (LogConfig *) The log configuration to set the log level on.
* (LogLevel) The log level to set. * (int) The log level to set.
*/ */
extern void extern void
LogConfigLevelSet(LogConfig *, LogLevel); LogConfigLevelSet(LogConfig *, int);
/* /*
* Indent the log output by two spaces. This can be helpful in * Indent the log output by two spaces. This can be helpful in
@ -185,12 +186,12 @@ extern void
* Params: * Params:
* *
* (LogConfig *) The logging configuration. * (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. * (const char *) The format string, or a plain message string.
* (...) Any items to map into the format string, printf() * (...) Any items to map into the format string, printf()
* style. * style.
*/ */
extern void extern void
Log(LogConfig *, LogLevel, const char *,...); Log(LogConfig *, int, const char *,...);
#endif #endif

View file

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