Commit 916e3276 authored by Shawn Webb's avatar Shawn Webb
Browse files

Add stdio-based logging



Only info goes to stdout, all the rest go to stderr.
Signed-off-by: Shawn Webb's avatarShawn Webb <shawn.webb@hardenedbsd.org>
parent 84a7d3e1
......@@ -5,6 +5,7 @@ INCS= liblattutil.h
SRCS+= config.c
SRCS+= log-dummy.c
SRCS+= log-main.c
SRCS+= log-stdio.c
SRCS+= log-syslog.c
SRCS+= sqlite3.c
......
......@@ -215,6 +215,14 @@ bool lattutil_log_syslog_init(lattutil_log_t *, int, int);
*/
void lattutil_log_dummy_init(lattutil_log_t *);
/**
* Initialize stdio-based logging
*
* @param Logging object
* @return True on success, False otherwise
*/
bool lattutil_log_stdio_init(lattutil_log_t *);
/**
* Determine if the logging subsystem is ready to receive messages
*
......@@ -399,6 +407,16 @@ ssize_t lattutil_log_dummy_info(lattutil_log_t *, int,
ssize_t lattutil_log_dummy_warn(lattutil_log_t *, int,
const char *, ...);
void lattutil_log_dummy_close(lattutil_log_t *);
ssize_t lattutil_log_stdio_debug(lattutil_log_t *, int,
const char *, ...);
ssize_t lattutil_log_stdio_err(lattutil_log_t *, int,
const char *, ...);
ssize_t lattutil_log_stdio_info(lattutil_log_t *, int,
const char *, ...);
ssize_t lattutil_log_stdio_warn(lattutil_log_t *, int,
const char *, ...);
void lattutil_log_stdio_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
bool
lattutil_log_stdio_init(lattutil_log_t *logp)
{
logp->ll_log_close = lattutil_log_stdio_close;
logp->ll_log_debug = lattutil_log_stdio_debug;
logp->ll_log_err = lattutil_log_stdio_err;
logp->ll_log_info = lattutil_log_stdio_info;
logp->ll_log_warn = lattutil_log_stdio_warn;
return (true);
}
ssize_t
lattutil_log_stdio_debug(lattutil_log_t *logp, int verbose,
const char *fmt, ...)
{
va_list args;
size_t len;
char *msg;
va_start(args, fmt);
if (verbose == -1 || verbose >= logp->ll_verbosity) {
msg = NULL;
vasprintf(&msg, fmt, args);
if (msg == NULL) {
len = -1;
goto end;
}
len = strlen(msg);
fprintf(stderr, "DEBUG: %s", msg);
free(msg);
}
va_end(args);
end:
return (len);
}
ssize_t
lattutil_log_stdio_err(lattutil_log_t *logp, int verbose,
const char *fmt, ...)
{
va_list args;
size_t len;
char *msg;
va_start(args, fmt);
if (verbose == -1 || verbose >= logp->ll_verbosity) {
msg = NULL;
vasprintf(&msg, fmt, args);
if (msg == NULL) {
len = -1;
goto end;
}
len = strlen(msg);
fprintf(stderr, "ERROR: %s", msg);
free(msg);
}
va_end(args);
end:
return (len);
}
ssize_t
lattutil_log_stdio_info(lattutil_log_t *logp, int verbose,
const char *fmt, ...)
{
va_list args;
size_t len;
char *msg;
va_start(args, fmt);
if (verbose == -1 || verbose >= logp->ll_verbosity) {
msg = NULL;
vasprintf(&msg, fmt, args);
if (msg == NULL) {
len = -1;
goto end;
}
len = strlen(msg);
fprintf(stdout, "INFO: %s", msg);
free(msg);
}
va_end(args);
end:
return (len);
}
ssize_t
lattutil_log_stdio_warn(lattutil_log_t *logp, int verbose,
const char *fmt, ...)
{
va_list args;
size_t len;
char *msg;
va_start(args, fmt);
if (verbose == -1 || verbose >= logp->ll_verbosity) {
msg = NULL;
vasprintf(&msg, fmt, args);
if (msg == NULL) {
len = -1;
goto end;
}
len = strlen(msg);
fprintf(stderr, "WARNING: %s", msg);
free(msg);
}
va_end(args);
end:
return (len);
}
void
lattutil_log_stdio_close(lattutil_log_t *logp)
{
}
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