Commit 9a226fc3 authored by Shawn Webb's avatar Shawn Webb
Browse files

Add a basic logger



Only syslog is supported at this time.
Signed-off-by: Shawn Webb's avatarShawn Webb <shawn.webb@hardenedbsd.org>
parent 4ed65553
SHLIB= lattutil
SHLIB_MAJOR= 0
SRCS= config.c
INCS= liblattutil.h
SRCS+= config.c
SRCS+= log-main.c
SRCS+= log-syslog.c
.PATH: ${.CURDIR}/src
.PATH: ${.CURDIR}/include
CFLAGS+= -D_lattutil_internal
CFLAGS+= -I${.CURDIR}/include
CFLAGS+= -I/usr/local/include
LDFLAGS+= -L/usr/local/lib
......
......@@ -28,11 +28,20 @@
#include <stdbool.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <ucl.h>
struct _lllog;
#define EXPORTED_SYM __attribute__((visibility("default")))
#define LATTUTIL_LOG_VERBOSITY_DEFAULT 1000
#define LATTUTIL_LOG_DEFAULT_NAME "lattutil"
typedef ssize_t (*log_cb)(struct _lllog *, int, const char *, ...);
typedef void (*log_close)(struct _lllog *);
/*
* Though we export the underlying data structure, using the API is
* preferred over directly accessing the ABI.
......@@ -45,6 +54,13 @@ typedef struct _llconfig {
const ucl_object_t *l_rootobj;
} lattutil_config_path_t;
typedef struct _lllog {
int ll_verbosity;
char *ll_path;
log_cb ll_log_info;
log_close ll_log_close;
} lattutil_log_t;
/**
* Find a configuration file in a set of given paths
*
......@@ -111,4 +127,38 @@ char *lattutil_find_config_string(const ucl_object_t *, const char *,
*/
int64_t find_config_int(const ucl_object_t *, const char *, int64_t);
/**
* Initialize logger
*
* @param Path
* @param Verbosity
* @return Pointer to the logging object
*/
lattutil_log_t *lattutil_log_init(char *, int);
/**
* Free logger
*
* The underlying variable is set to NULL.
*
* @param Double pointer to the logging object
*/
void lattutil_log_free(lattutil_log_t **);
/**
* Initialize syslog-based logging
*
* @param Logging object
* @param syslog(3) logopt
* @param syslog(3) facility
* @return True on success, False otherwise
*/
bool lattutil_log_syslog_init(lattutil_log_t *, int, int);
#ifdef _lattutil_internal
ssize_t lattutil_log_syslog_info(lattutil_log_t *, int,
const char *, ...);
void lattutil_log_syslog_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 <syslog.h>
#include "liblattutil.h"
EXPORTED_SYM
lattutil_log_t *
lattutil_log_init(char *path, int verbosity)
{
lattutil_log_t *res;
res = calloc(1, sizeof(*res));
if (res == NULL) {
return (NULL);
}
res->ll_verbosity = (verbosity == -1) ? LATTUTIL_LOG_VERBOSITY_DEFAULT :
verbosity;
if (path != NULL) {
res->ll_path = strdup(path);
if (res->ll_path == NULL) {
free(res);
return (NULL);
}
}
return (res);
}
EXPORTED_SYM
void
lattutil_log_free(lattutil_log_t **logp)
{
lattutil_log_t *logp2;
if (logp == NULL || *logp == NULL) {
return;
}
logp2 = *logp;
if (logp2->ll_log_close != NULL) {
logp2->ll_log_close(logp2);
}
free(logp2->ll_path);
memset(logp2, 0, sizeof(*logp2));
*logp = NULL;
}
EXPORTED_SYM
bool
lattutil_log_syslog_init(lattutil_log_t *logp, int logopt, int facility)
{
const char *name;
logp->ll_log_info = lattutil_log_syslog_info;
logp->ll_log_close = lattutil_log_syslog_close;
name = logp->ll_path;
if (name == NULL) {
name = getprogname();
if (name == NULL) {
name = LATTUTIL_LOG_DEFAULT_NAME;
}
}
printf("Opening log %s\n", name);
openlog(name, logopt, facility);
return (true);
}
/*-
* 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 <syslog.h>
#include "liblattutil.h"
ssize_t
lattutil_log_syslog_info(lattutil_log_t *logp, int verbose,
const char *fmt, ...)
{
va_list args;
size_t len;
char *msg;
va_start(args, fmt);
printf("Hey there!\n");
if (verbose == -1 || verbose >= logp->ll_verbosity) {
msg = NULL;
vasprintf(&msg, fmt, args);
printf("Attempting to log: %s\n", msg);
if (msg == NULL) {
len = -1;
goto end;
}
len = strlen(msg);
syslog(LOG_INFO, "%s", msg);
}
va_end(args);
end:
return (len);
}
void
lattutil_log_syslog_close(lattutil_log_t *logp)
{
closelog();
}
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