/*- * Copyright (c) 2026, Mr. Goblin * * SPDX-License-Identifier: BSD-2-Clause * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. */ #include #include #include #include #include #include "logger.h" static int use_syslog = 0; void logger_init(const char *name, int daemon_flag) { use_syslog = daemon_flag; if (use_syslog) openlog(name, LOG_PID, LOG_DAEMON); } void logger_close(void) { if (use_syslog) closelog(); } void logger_log(enum logger_lvl level, const char *format, ...) { int saved_errno = errno; va_list vargs; va_start(vargs, format); if (use_syslog) { switch (level) { case log_lvl_info: vsyslog(LOG_INFO, format, vargs); break; case log_lvl_warning: vsyslog(LOG_WARNING, format, vargs); break; case log_lvl_err: vsyslog(LOG_ERR, format, vargs); break; } } else { FILE *f = NULL; switch (level) { case log_lvl_info: f = stdout; fprintf(f, "[INFO] "); break; case log_lvl_warning: f = stderr; fprintf(f, "[WARNING] "); break; case log_lvl_err: f = stderr; fprintf(f, "[ERROR] "); break; default: f = stderr; fprintf(f, "[UNKNOWN] "); break; } vfprintf(f, format, vargs); } errno = saved_errno; } void logger_perror(const char *s) { int saved_errno = errno; if (use_syslog) { syslog(LOG_ERR, "%s: %s\n", s, strerror(errno)); } else { perror(s); } errno = saved_errno; }