mirror of
https://codeberg.org/dnkl/yambar.git
synced 2025-06-16 00:05:40 +02:00
log: make more logging options configurable
* Add --log-colorize=never|always|auto * Add --log-no-syslog
This commit is contained in:
parent
3f940ec2a8
commit
7397ab9457
5 changed files with 74 additions and 19 deletions
28
log.c
28
log.c
|
@ -12,19 +12,25 @@
|
|||
#include <syslog.h>
|
||||
|
||||
static bool colorize = false;
|
||||
static bool do_syslog = true;
|
||||
|
||||
static void __attribute__((constructor))
|
||||
init(void)
|
||||
void
|
||||
log_init(enum log_colorize _colorize, bool _do_syslog)
|
||||
{
|
||||
colorize = isatty(STDERR_FILENO);
|
||||
openlog(NULL, /*LOG_PID*/0, LOG_USER);
|
||||
setlogmask(LOG_UPTO(LOG_WARNING));
|
||||
colorize = _colorize == LOG_COLORIZE_NEVER ? false : _colorize == LOG_COLORIZE_ALWAYS ? true : isatty(STDERR_FILENO);
|
||||
do_syslog = _do_syslog;
|
||||
|
||||
if (do_syslog) {
|
||||
openlog(NULL, /*LOG_PID*/0, LOG_USER);
|
||||
setlogmask(LOG_UPTO(LOG_WARNING));
|
||||
}
|
||||
}
|
||||
|
||||
static void __attribute__((destructor))
|
||||
fini(void)
|
||||
void
|
||||
log_deinit(void)
|
||||
{
|
||||
closelog();
|
||||
if (do_syslog)
|
||||
closelog();
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -66,6 +72,9 @@ static void
|
|||
_sys_log(enum log_class log_class, const char *module, const char *file,
|
||||
int lineno, const char *fmt, int sys_errno, va_list va)
|
||||
{
|
||||
if (!do_syslog)
|
||||
return;
|
||||
|
||||
/* Map our log level to syslog's level */
|
||||
int level = -1;
|
||||
switch (log_class) {
|
||||
|
@ -85,7 +94,8 @@ _sys_log(enum log_class log_class, const char *module, const char *file,
|
|||
/* Calculate required size of buffer holding the entire log message */
|
||||
int required_len = 0;
|
||||
required_len += strlen(module) + 2; /* "%s: " */
|
||||
required_len += vsnprintf(NULL, 0, fmt, va2); va_end(va2);
|
||||
required_len += vsnprintf(NULL, 0, fmt, va2);
|
||||
va_end(va2);
|
||||
|
||||
if (sys_errno != 0)
|
||||
required_len += strlen(sys_err) + 2; /* ": %s" */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue