Commit ffb40d96 authored by Shawn Webb's avatar Shawn Webb
Browse files

Add file-based logging



We can now log to a specific file on the filesystem.
Signed-off-by: Shawn Webb's avatarShawn Webb <shawn.webb@hardenedbsd.org>
Sponsored-by:	BlackhawkNest, Inc
parent c116f92d
......@@ -4,6 +4,7 @@ INCS= liblattutil.h
SRCS+= config.c
SRCS+= log-dummy.c
SRCS+= log-file.c
SRCS+= log-main.c
SRCS+= log-stdio.c
SRCS+= log-sqlite3.c
......
......@@ -284,6 +284,15 @@ bool lattutil_log_syslog_init(lattutil_log_t *, int, int);
*/
void lattutil_log_dummy_init(lattutil_log_t *);
/*
* Initialize file-based logging
*
* @param Logging object
* @param Path to file, or NULL for "/dev/stdout"
* @return True on success, false otherwise
*/
bool lattutil_log_file_init(lattutil_log_t *, const char *);
/**
* Initialize stdio-based logging
*
......
/*-
* 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 <pthread.h>
#include "liblattutil.h"
typedef struct _log_file_aux {
FILE *lfa_file;
pthread_mutex_t lfa_mtx;
} log_file_aux_t;
static ssize_t _lattutil_log_file_debug(lattutil_log_t *, int,
const char *, ...);
static ssize_t _lattutil_log_file_err(lattutil_log_t *, int,
const char *, ...);
static ssize_t _lattutil_log_file_info(lattutil_log_t *, int,
const char *, ...);
static ssize_t _lattutil_log_file_warn(lattutil_log_t *, int,
const char *, ...);
static void _lattutil_log_file_close(lattutil_log_t *);
EXPORTED_SYM
bool
lattutil_log_file_init(lattutil_log_t *logp, const char *path)
{
log_file_aux_t *lfile;
if (path == NULL) {
path = "/dev/stdout";
}
lfile = calloc(1, sizeof(*lfile));
if (lfile == NULL) {
return (false);
}
if (pthread_mutex_init(&(lfile->lfa_mtx), NULL)) {
free(lfile);
return (false);
}
lfile->lfa_file = fopen(path, "ae");
if (lfile->lfa_file == NULL) {
pthread_mutex_destroy(&(lfile->lfa_mtx));
free(lfile);
return (false);
}
lattutil_log_set_aux(logp, lfile, sizeof(*lfile));
logp->ll_log_close = _lattutil_log_file_close;
logp->ll_log_debug = _lattutil_log_file_debug;
logp->ll_log_err = _lattutil_log_file_err;
logp->ll_log_info = _lattutil_log_file_info;
logp->ll_log_warn = _lattutil_log_file_warn;
return (true);
}
static ssize_t
_lattutil_log_file_debug(lattutil_log_t *logp, int verbose,
const char *fmt, ...)
{
log_file_aux_t *lfile;
va_list args;
size_t len;
char *msg;
if (logp == NULL || fmt == NULL) {
return (-1);
}
va_start(args, fmt);
if (verbose == -1 || verbose >= logp->ll_verbosity) {
len = 0;
lfile = lattutil_log_get_aux(logp, &len);
if (lfile == NULL || len != sizeof(*lfile)) {
len = -1;
goto end;
}
msg = NULL;
vasprintf(&msg, fmt, args);
if (msg == NULL) {
len = -1;
goto end;
}
len = strlen(msg);
pthread_mutex_lock(&(lfile->lfa_mtx));
fprintf(lfile->lfa_file, "DEBUG: %s\n", msg);
fflush(lfile->lfa_file);
pthread_mutex_unlock(&(lfile->lfa_mtx));
free(msg);
}
va_end(args);
end:
return (len);
}
static ssize_t
_lattutil_log_file_err(lattutil_log_t *logp, int verbose,
const char *fmt, ...)
{
log_file_aux_t *lfile;
va_list args;
size_t len;
char *msg;
if (logp == NULL || fmt == NULL) {
return (-1);
}
va_start(args, fmt);
if (verbose == -1 || verbose >= logp->ll_verbosity) {
len = 0;
lfile = lattutil_log_get_aux(logp, &len);
if (lfile == NULL || len != sizeof(*lfile)) {
len = -1;
goto end;
}
msg = NULL;
vasprintf(&msg, fmt, args);
if (msg == NULL) {
len = -1;
goto end;
}
len = strlen(msg);
pthread_mutex_lock(&(lfile->lfa_mtx));
fprintf(lfile->lfa_file, "ERROR: %s\n", msg);
fflush(lfile->lfa_file);
pthread_mutex_unlock(&(lfile->lfa_mtx));
free(msg);
}
va_end(args);
end:
return (len);
}
static ssize_t
_lattutil_log_file_info(lattutil_log_t *logp, int verbose,
const char *fmt, ...)
{
log_file_aux_t *lfile;
va_list args;
size_t len;
char *msg;
if (logp == NULL || fmt == NULL) {
return (-1);
}
va_start(args, fmt);
if (verbose == -1 || verbose >= logp->ll_verbosity) {
len = 0;
lfile = lattutil_log_get_aux(logp, &len);
if (lfile == NULL || len != sizeof(*lfile)) {
len = -1;
goto end;
}
msg = NULL;
vasprintf(&msg, fmt, args);
if (msg == NULL) {
len = -1;
goto end;
}
len = strlen(msg);
pthread_mutex_lock(&(lfile->lfa_mtx));
fprintf(lfile->lfa_file, "INFO: %s\n", msg);
fflush(lfile->lfa_file);
pthread_mutex_unlock(&(lfile->lfa_mtx));
free(msg);
}
va_end(args);
end:
return (len);
}
static ssize_t
_lattutil_log_file_warn(lattutil_log_t *logp, int verbose,
const char *fmt, ...)
{
log_file_aux_t *lfile;
va_list args;
size_t len;
char *msg;
if (logp == NULL || fmt == NULL) {
return (-1);
}
va_start(args, fmt);
if (verbose == -1 || verbose >= logp->ll_verbosity) {
len = 0;
lfile = lattutil_log_get_aux(logp, &len);
if (lfile == NULL || len != sizeof(*lfile)) {
len = -1;
goto end;
}
msg = NULL;
vasprintf(&msg, fmt, args);
if (msg == NULL) {
len = -1;
goto end;
}
len = strlen(msg);
pthread_mutex_lock(&(lfile->lfa_mtx));
fprintf(lfile->lfa_file, "WARNING: %s\n", msg);
fflush(lfile->lfa_file);
pthread_mutex_unlock(&(lfile->lfa_mtx));
free(msg);
}
va_end(args);
end:
return (len);
}
static void
_lattutil_log_file_close(lattutil_log_t *logp)
{
}
Supports Markdown
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