log: make more logging options configurable

* Add --log-colorize=never|always|auto
* Add --log-no-syslog
This commit is contained in:
Daniel Eklöf 2020-02-05 19:42:48 +01:00
parent 3f940ec2a8
commit 7397ab9457
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
5 changed files with 74 additions and 19 deletions

View file

@ -7,4 +7,6 @@ _arguments \
'(-b --backend)'{-b,--backend}'[backend to use (default: auto)]:backend:(xcb wayland auto)' \ '(-b --backend)'{-b,--backend}'[backend to use (default: auto)]:backend:(xcb wayland auto)' \
'(-c --config)'{-c,--config}'[alternative configuration file]:filename:_files' \ '(-c --config)'{-c,--config}'[alternative configuration file]:filename:_files' \
'(-C --validate)'{-C,--validate}'[verify configuration then quit]' \ '(-C --validate)'{-C,--validate}'[verify configuration then quit]' \
'(-p --print-pid)'{-p,--print-pid}'[print PID to this file or FD when up and running]:pidfile:_files' '(-p --print-pid)'{-p,--print-pid}'[print PID to this file or FD when up and running]:pidfile:_files' \
'(-l --log-colorize)'{-l,--log-colorize}'[enable or disable colorization of log output on stderr]:logcolor:(never always auto)' \
'(-s --log-no-syslog)'{-s,--log-no-syslog}'[disable syslog logging]'

View file

@ -27,6 +27,12 @@ yambar - modular status panel for X11 and Wayland
(or FD) is closed immediately after writing the PID. When a _FILE_ (or FD) is closed immediately after writing the PID. When a _FILE_
as been specified, the file is unlinked when yambar exits. as been specified, the file is unlinked when yambar exits.
*-l*,*--log-colorize*=[{*never*,*always*,*auto*}]
Enables or disables colorization of log output on stderr.
*-s*,*--log-no-syslog*
Disables syslog logging. Logging is only done on stderr.
*-v*,*--version* *-v*,*--version*
Show the version number and quit Show the version number and quit

22
log.c
View file

@ -12,18 +12,24 @@
#include <syslog.h> #include <syslog.h>
static bool colorize = false; static bool colorize = false;
static bool do_syslog = true;
static void __attribute__((constructor)) void
init(void) log_init(enum log_colorize _colorize, bool _do_syslog)
{ {
colorize = isatty(STDERR_FILENO); 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); openlog(NULL, /*LOG_PID*/0, LOG_USER);
setlogmask(LOG_UPTO(LOG_WARNING)); setlogmask(LOG_UPTO(LOG_WARNING));
} }
}
static void __attribute__((destructor)) void
fini(void) log_deinit(void)
{ {
if (do_syslog)
closelog(); closelog();
} }
@ -66,6 +72,9 @@ static void
_sys_log(enum log_class log_class, const char *module, const char *file, _sys_log(enum log_class log_class, const char *module, const char *file,
int lineno, const char *fmt, int sys_errno, va_list va) int lineno, const char *fmt, int sys_errno, va_list va)
{ {
if (!do_syslog)
return;
/* Map our log level to syslog's level */ /* Map our log level to syslog's level */
int level = -1; int level = -1;
switch (log_class) { 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 */ /* Calculate required size of buffer holding the entire log message */
int required_len = 0; int required_len = 0;
required_len += strlen(module) + 2; /* "%s: " */ 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) if (sys_errno != 0)
required_len += strlen(sys_err) + 2; /* ": %s" */ required_len += strlen(sys_err) + 2; /* ": %s" */

6
log.h
View file

@ -1,7 +1,13 @@
#pragma once #pragma once
#include <stdbool.h>
enum log_colorize { LOG_COLORIZE_NEVER, LOG_COLORIZE_ALWAYS, LOG_COLORIZE_AUTO };
enum log_class { LOG_CLASS_ERROR, LOG_CLASS_WARNING, LOG_CLASS_INFO, LOG_CLASS_DEBUG }; enum log_class { LOG_CLASS_ERROR, LOG_CLASS_WARNING, LOG_CLASS_INFO, LOG_CLASS_DEBUG };
void log_init(enum log_colorize colorize, bool do_syslog);
void log_deinit(void);
void log_msg(enum log_class log_class, const char *module, void log_msg(enum log_class log_class, const char *module,
const char *file, int lineno, const char *file, int lineno,
const char *fmt, ...) __attribute__((format (printf, 5, 6))); const char *fmt, ...) __attribute__((format (printf, 5, 6)));

41
main.c
View file

@ -131,7 +131,9 @@ print_usage(const char *prog_name)
" -c,--config=FILE alternative configuration file\n" " -c,--config=FILE alternative configuration file\n"
" -C,--validate verify configuration then quit\n" " -C,--validate verify configuration then quit\n"
" -p,--print-pid=FILE|FD print PID to file or FD\n" " -p,--print-pid=FILE|FD print PID to file or FD\n"
" -v,--version print f00sel version and quit\n"); " -l,--log-colorize=[never|always|auto] enable/disable colorization of log output on stderr\n"
" -s,--log-no-syslog disable syslog logging\n"
" -v,--version show the version number and quit\n");
} }
static bool static bool
@ -181,6 +183,8 @@ main(int argc, char *const *argv)
{"config", required_argument, 0, 'c'}, {"config", required_argument, 0, 'c'},
{"validate", no_argument, 0, 'C'}, {"validate", no_argument, 0, 'C'},
{"print-pid", required_argument, 0, 'p'}, {"print-pid", required_argument, 0, 'p'},
{"log-colorize", optional_argument, 0, 'l'},
{"log-no-syslog", no_argument, 0, 's'},
{"version", no_argument, 0, 'v'}, {"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{NULL, no_argument, 0, 0}, {NULL, no_argument, 0, 0},
@ -193,8 +197,11 @@ main(int argc, char *const *argv)
char *config_path = NULL; char *config_path = NULL;
enum bar_backend backend = BAR_BACKEND_AUTO; enum bar_backend backend = BAR_BACKEND_AUTO;
enum log_colorize log_colorize = LOG_COLORIZE_AUTO;
bool log_syslog = true;
while (true) { while (true) {
int c = getopt_long(argc, argv, ":b:c:Cp:vh", longopts, NULL); int c = getopt_long(argc, argv, ":b:c:Cp:l::svh", longopts, NULL);
if (c == -1) if (c == -1)
break; break;
@ -205,7 +212,7 @@ main(int argc, char *const *argv)
else if (strcmp(optarg, "wayland") == 0) else if (strcmp(optarg, "wayland") == 0)
backend = BAR_BACKEND_WAYLAND; backend = BAR_BACKEND_WAYLAND;
else { else {
LOG_ERR("%s: invalid backend", optarg); fprintf(stderr, "%s: invalid backend\n", optarg);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
break; break;
@ -213,10 +220,10 @@ main(int argc, char *const *argv)
case 'c': { case 'c': {
struct stat st; struct stat st;
if (stat(optarg, &st) == -1) { if (stat(optarg, &st) == -1) {
LOG_ERRNO("%s: invalid configuration file", optarg); fprintf(stderr, "%s: invalid configuration file: %s\n", optarg, strerror(errno));
return EXIT_FAILURE; return EXIT_FAILURE;
} else if (!S_ISREG(st.st_mode)) { } else if (!S_ISREG(st.st_mode)) {
LOG_ERR("%s: invalid configuration file: not a regular file", fprintf(stderr, "%s: invalid configuration file: not a regular file\n",
optarg); optarg);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -233,6 +240,23 @@ main(int argc, char *const *argv)
pid_file = optarg; pid_file = optarg;
break; break;
case 'l':
if (optarg == NULL || strcmp(optarg, "auto") == 0)
log_colorize = LOG_COLORIZE_AUTO;
else if (strcmp(optarg, "never") == 0)
log_colorize = LOG_COLORIZE_NEVER;
else if (strcmp(optarg, "always") == 0)
log_colorize = LOG_COLORIZE_ALWAYS;
else {
fprintf(stderr, "%s: argument must be one of 'never', 'always' or 'auto'\n", optarg);
return EXIT_FAILURE;
}
break;
case 's':
log_syslog = false;
break;
case 'v': case 'v':
printf("yambar version %s\n", YAMBAR_VERSION); printf("yambar version %s\n", YAMBAR_VERSION);
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -251,6 +275,8 @@ main(int argc, char *const *argv)
} }
} }
log_init(log_colorize, log_syslog);
const struct sigaction sa = {.sa_handler = &signal_handler}; const struct sigaction sa = {.sa_handler = &signal_handler};
sigaction(SIGINT, &sa, NULL); sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL); sigaction(SIGTERM, &sa, NULL);
@ -266,6 +292,7 @@ main(int argc, char *const *argv)
int abort_fd = eventfd(0, EFD_CLOEXEC); int abort_fd = eventfd(0, EFD_CLOEXEC);
if (abort_fd == -1) { if (abort_fd == -1) {
LOG_ERRNO("failed to create eventfd (for abort signalling)"); LOG_ERRNO("failed to create eventfd (for abort signalling)");
log_deinit();
return 1; return 1;
} }
@ -273,6 +300,7 @@ main(int argc, char *const *argv)
config_path = get_config_path(); config_path = get_config_path();
if (config_path == NULL) { if (config_path == NULL) {
LOG_ERR("could not find a configuration (see man 5 yambar)"); LOG_ERR("could not find a configuration (see man 5 yambar)");
log_deinit();
return 1; return 1;
} }
} }
@ -282,12 +310,14 @@ main(int argc, char *const *argv)
if (bar == NULL) { if (bar == NULL) {
close(abort_fd); close(abort_fd);
log_deinit();
return 1; return 1;
} }
if (verify_config) { if (verify_config) {
bar->destroy(bar); bar->destroy(bar);
close(abort_fd); close(abort_fd);
log_deinit();
return 0; return 0;
} }
@ -335,5 +365,6 @@ done:
if (unlink_pid_file) if (unlink_pid_file)
unlink(pid_file); unlink(pid_file);
log_deinit();
return res; return res;
} }