Commit 6d213cdf authored by Shawn Webb's avatar Shawn Webb
Browse files

Add dummy logging implementation



The dummy log is the default and is effectively a NOP. It's set in case
the user forgets to use a real logging backend (syslog is the only other
supported logging backend right now).
Signed-off-by: Shawn Webb's avatarShawn Webb <shawn.webb@hardenedbsd.org>
parent c20368a8
......@@ -3,6 +3,7 @@ SHLIB_MAJOR= 0
INCS= liblattutil.h
SRCS+= config.c
SRCS+= log-dummy.c
SRCS+= log-main.c
SRCS+= log-syslog.c
......
......@@ -159,6 +159,33 @@ void lattutil_log_free(lattutil_log_t **);
*/
bool lattutil_log_syslog_init(lattutil_log_t *, int, int);
/**
* Initialize NULL logging
*
* This is the default logger. It simply discards all logging
* messages.
*
* @param Logging object
*/
void lattutil_log_dummy_init(lattutil_log_t *);
/**
* Determine if the logging subsystem is ready to receive messages
*
* @param Pointer to the logging object
*/
bool lattutil_log_ready(lattutil_log_t *);
/**
* Get the configured minimum verbosity level
*
* @param Logging object
* @return Verbosity level
*/
int lattutil_log_verbosity(lattutil_log_t *);
int lattutil_log_set_verbosity(lattutil_log_t *, int);
#ifdef _lattutil_internal
ssize_t lattutil_log_syslog_err(lattutil_log_t *, int,
const char *, ...);
......@@ -167,6 +194,14 @@ ssize_t lattutil_log_syslog_info(lattutil_log_t *, int,
ssize_t lattutil_log_syslog_warn(lattutil_log_t *, int,
const char *, ...);
void lattutil_log_syslog_close(lattutil_log_t *);
ssize_t lattutil_log_dummy_err(lattutil_log_t *, int,
const char *, ...);
ssize_t lattutil_log_dummy_info(lattutil_log_t *, int,
const char *, ...);
ssize_t lattutil_log_dummy_warn(lattutil_log_t *, int,
const char *, ...);
void lattutil_log_dummy_close(lattutil_log_t *);
#endif /* _lattutil_internal */
#endif /* !_LIBLATTUTIL_H */
/*-
* Copyright (c) 2021 Shawn Webb <shawn.webb@hardenedbsd.org>
*
* 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 AUTHOR ``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 AUTHOR 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "liblattutil.h"
EXPORTED_SYM
void
lattutil_log_dummy_init(lattutil_log_t *res)
{
res->ll_log_close = lattutil_log_dummy_close;
res->ll_log_err = lattutil_log_dummy_err;
res->ll_log_info = lattutil_log_dummy_info;
res->ll_log_warn = lattutil_log_dummy_warn;
}
ssize_t
lattutil_log_dummy_err(lattutil_log_t *logp, int verbose,
const char *fmt, ...)
{
return (0);
}
ssize_t
lattutil_log_dummy_info(lattutil_log_t *logp, int verbose,
const char *fmt, ...)
{
return (0);
}
ssize_t
lattutil_log_dummy_warn(lattutil_log_t *logp, int verbose,
const char *fmt, ...)
{
return (0);
}
void
lattutil_log_dummy_close(lattutil_log_t *logp)
{
return;
}
......@@ -52,6 +52,8 @@ lattutil_log_init(char *path, int verbosity)
}
}
lattutil_log_dummy_init(res);
return (res);
}
......@@ -78,24 +80,51 @@ lattutil_log_free(lattutil_log_t **logp)
EXPORTED_SYM
bool
lattutil_log_syslog_init(lattutil_log_t *logp, int logopt, int facility)
lattutil_log_ready(lattutil_log_t *logp)
{
const char *name;
logp->ll_log_close = lattutil_log_syslog_close;
logp->ll_log_err = lattutil_log_syslog_err;
logp->ll_log_info = lattutil_log_syslog_info;
logp->ll_log_warn = lattutil_log_syslog_warn;
name = logp->ll_path;
if (name == NULL) {
name = getprogname();
if (name == NULL) {
name = LATTUTIL_LOG_DEFAULT_NAME;
}
if (logp == NULL) {
return (false);
}
openlog(name, logopt, facility);
if (logp->ll_log_err == NULL ||
logp->ll_log_info == NULL ||
logp->ll_log_warn == NULL) {
return (false);
}
return (true);
}
EXPORTED_SYM
int
lattutil_log_verbosity(lattutil_log_t *logp)
{
if (logp == NULL) {
return (-1);
}
return (logp->ll_verbosity);
}
EXPORTED_SYM
int
lattutil_log_set_verbosity(lattutil_log_t *logp, int verbosity)
{
int old;
if (logp == NULL) {
return (-1);
}
old = logp->ll_verbosity;
if (verbosity == -1) {
verbosity = LATTUTIL_LOG_VERBOSITY_DEFAULT;
}
logp->ll_verbosity = verbosity;
return (old);
}
......@@ -31,6 +31,30 @@
#include "liblattutil.h"
EXPORTED_SYM
bool
lattutil_log_syslog_init(lattutil_log_t *logp, int logopt, int facility)
{
const char *name;
logp->ll_log_close = lattutil_log_syslog_close;
logp->ll_log_err = lattutil_log_syslog_err;
logp->ll_log_info = lattutil_log_syslog_info;
logp->ll_log_warn = lattutil_log_syslog_warn;
name = logp->ll_path;
if (name == NULL) {
name = getprogname();
if (name == NULL) {
name = LATTUTIL_LOG_DEFAULT_NAME;
}
}
openlog(name, logopt, facility);
return (true);
}
ssize_t
lattutil_log_syslog_err(lattutil_log_t *logp, int verbose,
const char *fmt, ...)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment