log: rewrite log subsystem
The new logger is much more powerful, allows online log-filter modifications and precise debugging. We still avoid heavy file-operations as we rely on the caller to forward stderr to a log-file and perform log-rotations. However, we now provide a simple way to perform this forward inside the application. Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
This commit is contained in:
1 parent
4790d9f7ab
commit
7eafca0e9f
2 files changed
+731
-90
No files matched your search
@@ -1,58 +1,502 @@
|
||||
/*
|
||||
* kmscon - Log Control
|
||||
*
|
||||
* Copyright (c) 2011 David Herrmann <dh.herrmann@googlemail.com>
|
||||
* Copyright (c) 2011 University of Tuebingen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files
|
||||
* (the "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
* Log/Debug Interface
|
||||
* Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
|
||||
* Dedicated to the Public Domain
|
||||
*/
|
||||
|
||||
/*
|
||||
* Log Control
|
||||
* Forward log messages to stderr. We use syslog and printk like severity
|
||||
* prefixes.
|
||||
* Log/Debug API Implementation
|
||||
* We provide thread-safety so we need a global lock. Function which
|
||||
* are prefixed with log__* need the lock to be held. All other functions must
|
||||
* be called without the lock held.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string.h>
|
||||
#include "log.h"
|
||||
|
||||
__attribute__ ((format (printf, 1, 2)))
|
||||
void log_printf(const char *format, ...)
|
||||
/*
|
||||
* Locking
|
||||
* We need a global locking mechanism. Use pthread here.
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
static pthread_mutex_t log__mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
static inline void log_lock()
|
||||
{
|
||||
pthread_mutex_lock(&log__mutex);
|
||||
}
|
||||
|
||||
static inline void log_unlock()
|
||||
{
|
||||
pthread_mutex_unlock(&log__mutex);
|
||||
}
|
||||
|
||||
/*
|
||||
* Time Management
|
||||
* We print seconds and microseconds since application start for each
|
||||
* log-message.
|
||||
*/
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
static struct timeval log__ftime;
|
||||
|
||||
static void log__time(long long *sec, long long *usec)
|
||||
{
|
||||
struct timeval t;
|
||||
|
||||
if (log__ftime.tv_sec == 0 && log__ftime.tv_usec == 0) {
|
||||
gettimeofday(&log__ftime, NULL);
|
||||
*sec = 0;
|
||||
*usec = 0;
|
||||
} else {
|
||||
gettimeofday(&t, NULL);
|
||||
*sec = t.tv_sec - log__ftime.tv_sec;
|
||||
*usec = t.tv_usec - log__ftime.tv_usec;
|
||||
if (*usec < 0) {
|
||||
*sec -= 1;
|
||||
*usec = 1000000 - *usec;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Build-Count
|
||||
* This is incremented for every build. ISO-C does not provide a method to do
|
||||
* this so we default to 1 if no external build-number is specified.
|
||||
*/
|
||||
|
||||
#ifdef LOG_BUILD
|
||||
static unsigned long log_build = LOG_BUILD;
|
||||
#else
|
||||
static unsigned long log_build = 1;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Default Values
|
||||
* Several logging-parameters may be omitted by applications. To provide sane
|
||||
* default values we provide constants here.
|
||||
*
|
||||
* LOG_SUBSYSTEM: By default no subsystem is specified
|
||||
*/
|
||||
|
||||
const struct log_config LOG_CONFIG = {
|
||||
.sev = {
|
||||
[LOG_DEBUG] = 2,
|
||||
[LOG_INFO] = 2,
|
||||
[LOG_NOTICE] = 2,
|
||||
[LOG_WARNING] = 2,
|
||||
[LOG_ERROR] = 2,
|
||||
[LOG_CRITICAL] = 2,
|
||||
[LOG_FATAL] = 2,
|
||||
}
|
||||
};
|
||||
|
||||
const char *LOG_SUBSYSTEM = NULL;
|
||||
|
||||
/*
|
||||
* Filters
|
||||
* By default DEBUG and INFO messages are disabled. If LOG_ENABLE_DEBUG is not
|
||||
* defined, then all log_debug() statements compile to zero-code and they cannot
|
||||
* be enabled on runtime.
|
||||
* To enable DEBUG or INFO messages at runtime, you can either specify that they
|
||||
* should be enabled globally, per file or specify a custom filter. Other
|
||||
* messages than DEBUG and INFO cannot be configured. However, additional
|
||||
* configuration options may be added later.
|
||||
*
|
||||
* Use log_set_config() to enable debug/info messages globally. If you
|
||||
* enable a global message type, then all other filters are skipped. If you
|
||||
* disable a global message type then fine-grained filters can take effect.
|
||||
*
|
||||
* To enable DEBUG/INFO messages for a specific source-file, you can add
|
||||
* this line to the top of the source file:
|
||||
* #define LOG_CONFIG LOG_STATIC_CONFIG(true, true)
|
||||
* So info and debug messages are enabled for this file on compile-time. First
|
||||
* parameter of LOG_STATIC_CONFIG is for debug, second one for info.
|
||||
*
|
||||
* Or you can add new configurations on runtime. Runtime configurations take a
|
||||
* filter parameter and a config parameter. The filter specifies what messages
|
||||
* are affected and the config parameter specifies what action is performed.
|
||||
*/
|
||||
|
||||
static struct log_config log__gconfig = {
|
||||
.sev = {
|
||||
[LOG_DEBUG] = 0,
|
||||
[LOG_INFO] = 0,
|
||||
[LOG_NOTICE] = 1,
|
||||
[LOG_WARNING] = 1,
|
||||
[LOG_ERROR] = 1,
|
||||
[LOG_CRITICAL] = 1,
|
||||
[LOG_FATAL] = 1,
|
||||
}
|
||||
};
|
||||
|
||||
struct log_dynconf {
|
||||
struct log_dynconf *next;
|
||||
int handle;
|
||||
struct log_filter filter;
|
||||
struct log_config config;
|
||||
};
|
||||
|
||||
static struct log_dynconf *log__dconfig = NULL;
|
||||
|
||||
void log_set_config(const struct log_config *config)
|
||||
{
|
||||
if (!config)
|
||||
return;
|
||||
|
||||
log_lock();
|
||||
log__gconfig = *config;
|
||||
log_unlock();
|
||||
}
|
||||
|
||||
int log_add_filter(const struct log_filter *filter,
|
||||
const struct log_config *config)
|
||||
{
|
||||
struct log_dynconf *dconf;
|
||||
int ret;
|
||||
|
||||
if (!filter || !config)
|
||||
return -EINVAL;
|
||||
|
||||
dconf = malloc(sizeof(*dconf));
|
||||
if (!dconf)
|
||||
return -ENOMEM;
|
||||
|
||||
memset(dconf, 0, sizeof(*dconf));
|
||||
memcpy(&dconf->filter, filter, sizeof(*filter));
|
||||
memcpy(&dconf->config, config, sizeof(*config));
|
||||
|
||||
log_lock();
|
||||
if (log__dconfig)
|
||||
dconf->handle = log__dconfig->handle + 1;
|
||||
dconf->next = log__dconfig;
|
||||
log__dconfig = dconf;
|
||||
ret = dconf->handle;
|
||||
log_unlock();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void log_rm_filter(int handle)
|
||||
{
|
||||
struct log_dynconf *dconf, *i;
|
||||
|
||||
dconf = NULL;
|
||||
|
||||
log_lock();
|
||||
if (log__dconfig) {
|
||||
if (log__dconfig->handle == handle) {
|
||||
dconf = log__dconfig;
|
||||
log__dconfig = dconf->next;
|
||||
} else for (i = log__dconfig; i->next; i = i->next) {
|
||||
dconf = i->next;
|
||||
if (dconf->handle == handle) {
|
||||
i->next = dconf->next;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
log_unlock();
|
||||
|
||||
free(dconf);
|
||||
}
|
||||
|
||||
void log_clean_filters()
|
||||
{
|
||||
struct log_dynconf *dconf;
|
||||
|
||||
log_lock();
|
||||
while ((dconf = log__dconfig)) {
|
||||
log__dconfig = dconf->next;
|
||||
free(dconf);
|
||||
}
|
||||
log_unlock();
|
||||
}
|
||||
|
||||
static bool log__matches(const struct log_filter *filter,
|
||||
const char *file,
|
||||
int line,
|
||||
const char *func,
|
||||
const char *subs)
|
||||
{
|
||||
if (*filter->file) {
|
||||
if (!file || strncmp(filter->file, file, LOG_STRMAX))
|
||||
return false;
|
||||
}
|
||||
if (filter->line >= 0 && filter->line != line)
|
||||
return false;
|
||||
if (*filter->func) {
|
||||
if (!func || strncmp(filter->func, func, LOG_STRMAX))
|
||||
return false;
|
||||
}
|
||||
if (*filter->subs) {
|
||||
if (!subs || strncmp(filter->subs, subs, LOG_STRMAX))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool log__omit(const char *file,
|
||||
int line,
|
||||
const char *func,
|
||||
const struct log_config *config,
|
||||
const char *subs,
|
||||
enum log_severity sev)
|
||||
{
|
||||
int val;
|
||||
struct log_dynconf *dconf;
|
||||
|
||||
if (sev >= LOG_SEV_NUM || sev < 0)
|
||||
return false;
|
||||
|
||||
if (config) {
|
||||
val = config->sev[sev];
|
||||
if (val == 0)
|
||||
return true;
|
||||
if (val == 1)
|
||||
return false;
|
||||
}
|
||||
|
||||
for (dconf = log__dconfig; dconf; dconf = dconf->next) {
|
||||
if (log__matches(&dconf->filter, file, line, func, subs)) {
|
||||
val = dconf->config.sev[sev];
|
||||
if (val == 0)
|
||||
return true;
|
||||
if (val == 1)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
val = log__gconfig.sev[sev];
|
||||
if (val == 0)
|
||||
return true;
|
||||
if (val == 1)
|
||||
return false;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Forward declaration so we can use the locked-versions in other functions
|
||||
* here. Be careful to avoid deadlocks, though.
|
||||
* Also set default log-subsystem to "log" for all logging inside this API.
|
||||
*/
|
||||
|
||||
static void log__submit(const char *file,
|
||||
int line,
|
||||
const char *func,
|
||||
const struct log_config *config,
|
||||
const char *subs,
|
||||
enum log_severity sev,
|
||||
const char *format,
|
||||
va_list args);
|
||||
|
||||
static void log__format(const char *file,
|
||||
int line,
|
||||
const char *func,
|
||||
const struct log_config *config,
|
||||
const char *subs,
|
||||
enum log_severity sev,
|
||||
const char *format,
|
||||
...);
|
||||
|
||||
#define LOG_SUBSYSTEM "log"
|
||||
|
||||
/*
|
||||
* Log-File
|
||||
* By default logging is done to stderr. However, you can set a file which is
|
||||
* used instead of stderr for logging. We do not provide complex log-rotation or
|
||||
* management functions, you can add them yourself or use a proper init-system
|
||||
* like systemd which does this for you.
|
||||
* We cannot set this to "stderr" as stderr might not be a compile-time
|
||||
* constant. Therefore, NULL means stderr.
|
||||
*/
|
||||
|
||||
static FILE *log__file = NULL;
|
||||
|
||||
int log_set_file(const char *file)
|
||||
{
|
||||
FILE *f, *old;
|
||||
|
||||
if (file) {
|
||||
f = fopen(file, "a");
|
||||
if (!f) {
|
||||
log_err("cannot change log-file to %s (%d): %s",
|
||||
file, errno, strerror(errno));
|
||||
return -EFAULT;
|
||||
}
|
||||
} else {
|
||||
f = NULL;
|
||||
file = "<default>";
|
||||
}
|
||||
|
||||
old = NULL;
|
||||
|
||||
log_lock();
|
||||
if (log__file != f) {
|
||||
log__format(LOG_DEFAULT, LOG_NOTICE,
|
||||
"set log-file to %s", file);
|
||||
old = log__file;
|
||||
log__file = f;
|
||||
f = NULL;
|
||||
}
|
||||
log_unlock();
|
||||
|
||||
if (f)
|
||||
fclose(f);
|
||||
if (old)
|
||||
fclose(old);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Basic logger
|
||||
* The log__submit function writes the message into the current log-target. It
|
||||
* must be called with log__mutex locked.
|
||||
* log__format does the same but first converts the argument list into a
|
||||
* va_list.
|
||||
* By default the current time elapsed since the first message was logged is
|
||||
* prepended to the message. file, line and func information are appended to the
|
||||
* message if sev == LOG_DEBUG.
|
||||
* The subsystem, if not NULL, is prepended as "SUBS: " to the message and a
|
||||
* newline is always appended by default. Multiline-messages are not allowed and
|
||||
* do not make sense here.
|
||||
*/
|
||||
|
||||
static const char *log__sev2str[] = {
|
||||
"DEBUG", /* LOG_DEBUG */
|
||||
"INFO", /* LOG_INFO */
|
||||
"NOTICE", /* LOG_NOTICE */
|
||||
"WARNING", /* LOG_WARNING */
|
||||
"ERROR", /* LOG_ERROR */
|
||||
"CRITICAL", /* LOG_CRITICAL */
|
||||
"FATAL", /* LOG_FATAL */
|
||||
};
|
||||
|
||||
static void log__submit(const char *file,
|
||||
int line,
|
||||
const char *func,
|
||||
const struct log_config *config,
|
||||
const char *subs,
|
||||
enum log_severity sev,
|
||||
const char *format,
|
||||
va_list args)
|
||||
{
|
||||
const char *prefix = NULL;
|
||||
FILE *out;
|
||||
long long sec, usec;
|
||||
|
||||
if (log__omit(file, line, func, config, subs, sev))
|
||||
return;
|
||||
|
||||
if (log__file)
|
||||
out = log__file;
|
||||
else
|
||||
out = stderr;
|
||||
|
||||
log__time(&sec, &usec);
|
||||
|
||||
if (sev < LOG_SEV_NUM && sev >= 0)
|
||||
prefix = log__sev2str[sev];
|
||||
|
||||
if (prefix) {
|
||||
if (subs)
|
||||
fprintf(out, "[%.4lld.%.6lld] %s: %s: ",
|
||||
sec, usec, prefix, subs);
|
||||
else
|
||||
fprintf(out, "[%.4lld.%.6lld] %s: ",
|
||||
sec, usec, prefix);
|
||||
} else {
|
||||
if (subs)
|
||||
fprintf(out, "[%.4lld.%.6lld] %s: ", sec, usec, subs);
|
||||
else
|
||||
fprintf(out, "[%.4lld.%.6lld] ", sec, usec);
|
||||
}
|
||||
|
||||
vfprintf(out, format, args);
|
||||
|
||||
if (sev == LOG_DEBUG) {
|
||||
if (!func)
|
||||
func = "<unknown>";
|
||||
if (!file)
|
||||
file = "<unknown>";
|
||||
if (line < 0)
|
||||
line = 0;
|
||||
fprintf(out, " (%s() in %s:%d)\n", func, file, line);
|
||||
} else {
|
||||
fprintf(out, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void log__format(const char *file,
|
||||
int line,
|
||||
const char *func,
|
||||
const struct log_config *config,
|
||||
const char *subs,
|
||||
enum log_severity sev,
|
||||
const char *format,
|
||||
...)
|
||||
{
|
||||
va_list list;
|
||||
|
||||
va_start(list, format);
|
||||
log_vprintf(format, list);
|
||||
log__submit(file, line, func, config, subs, sev, format, list);
|
||||
va_end(list);
|
||||
}
|
||||
|
||||
__attribute__ ((format (printf, 1, 0)))
|
||||
void log_vprintf(const char *format, va_list list)
|
||||
void log_submit(const char *file,
|
||||
int line,
|
||||
const char *func,
|
||||
const struct log_config *config,
|
||||
const char *subs,
|
||||
enum log_severity sev,
|
||||
const char *format,
|
||||
va_list args)
|
||||
{
|
||||
int saved_errno = errno;
|
||||
|
||||
vfprintf(stderr, format, list);
|
||||
log_lock();
|
||||
log__submit(file, line, func, config, subs, sev, format, args);
|
||||
log_unlock();
|
||||
|
||||
errno = saved_errno;
|
||||
}
|
||||
|
||||
void log_format(const char *file,
|
||||
int line,
|
||||
const char *func,
|
||||
const struct log_config *config,
|
||||
const char *subs,
|
||||
enum log_severity sev,
|
||||
const char *format,
|
||||
...)
|
||||
{
|
||||
va_list list;
|
||||
int saved_errno = errno;
|
||||
|
||||
va_start(list, format);
|
||||
log_lock();
|
||||
log__submit(file, line, func, config, subs, sev, format, list);
|
||||
log_unlock();
|
||||
va_end(list);
|
||||
|
||||
errno = saved_errno;
|
||||
}
|
||||
|
||||
void log_print_init(const char *appname)
|
||||
{
|
||||
if (!appname)
|
||||
appname = "<unknown>";
|
||||
log_format(LOG_DEFAULT_CONF, NULL, LOG_NOTICE,
|
||||
"%s Build #%u %s %s", appname,
|
||||
log_build, __DATE__, __TIME__);
|
||||
}
|
||||
@@ -1,73 +1,270 @@
|
||||
/*
|
||||
* kmscon - Log Control
|
||||
*
|
||||
* Copyright (c) 2011 David Herrmann <dh.herrmann@googlemail.com>
|
||||
* Copyright (c) 2011 University of Tuebingen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files
|
||||
* (the "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
* Log/Debug Interface
|
||||
* Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
|
||||
* Dedicated to the Public Domain
|
||||
*/
|
||||
|
||||
/*
|
||||
* Log Control
|
||||
* This is a fairly simply logging API. It forwards all messages to stderr.
|
||||
* They may be prefixed with a priority level like kernel messages.
|
||||
* To forward the messages to syslog simply connect stderr to the syslog daemon
|
||||
* via your init-manager.
|
||||
* Log/Debug Interface
|
||||
* This interface provides basic logging to a single file or stderr. By default,
|
||||
* all log-messages are forwarded to stderr but you can change this to an
|
||||
* arbitrary file. However, no complex file-rotation/backup functions are
|
||||
* supported so you should use the default (stderr) and use a proper init-system
|
||||
* like systemd to do log-rotations. This can also forward stderr messages into
|
||||
* log-files.
|
||||
*
|
||||
* Besides simple log-functions this also provides run-time filters so special
|
||||
* debug messages can be enabled/disabled. This should be used for
|
||||
* debugging-only because it may slow-down your application if every message is
|
||||
* filtered.
|
||||
*
|
||||
* Define LOG_ENABLE_DEBUG before including this header to enable debug-messages
|
||||
* for this file.
|
||||
*/
|
||||
|
||||
#ifndef KMSCON_LOG_H
|
||||
#define KMSCON_LOG_H
|
||||
#ifndef LOG_H_INCLUDED
|
||||
#define LOG_H_INCLUDED
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* LOG_EMERG and LOG_ALERT do not make sense for this application */
|
||||
#define LOG_CRIT "<2>" /* error that cannot be handled correctly */
|
||||
#define LOG_ERR "<3>" /* error detected */
|
||||
#define LOG_WARN "<4>" /* warn about unexpected conditions */
|
||||
#define LOG_NOTICE "<5>" /* notify about unusual conditions */
|
||||
#define LOG_INFO "<6>" /* basic inforomational messages */
|
||||
#define LOG_DEBUG "<7>" /* debug messages */
|
||||
/*
|
||||
* Log Messages and Filters
|
||||
* A log message consists of:
|
||||
* - file: the source file where the call was made
|
||||
* - line: the corresponding line number
|
||||
* - func: the function name
|
||||
* - config: special configuration for this message only
|
||||
* - subs: the subsystem
|
||||
* - sev: the severity
|
||||
* - format: format string
|
||||
* - args: arguments depending on format string
|
||||
* Depending on this information the log system decides whether the message is
|
||||
* discarded or logged and what information is included. To allow fine-grained
|
||||
* configuration you can add log_filter and log_config objects. A log_filter
|
||||
* object specifies to what messages the log_config object shall apply. If a
|
||||
* log_filter does not match, then the corresponding log_config is ignored.
|
||||
* The log_config specifies whether a message is discarded, logged or whether
|
||||
* other filters shall be searched.
|
||||
*
|
||||
* The "config" field of every log-message does not have a corresponding
|
||||
* log_filter object. Instead, it is assumed that the config object only applies
|
||||
* to this single message. This allows to specify special behavior for every
|
||||
* single message but also refer to global filters.
|
||||
*
|
||||
* Config Object:
|
||||
* A log_config object contains a severity-array. Each severity is the index of
|
||||
* an integer in the array. If the integer is 0, then messages with the given
|
||||
* severity are discarded, if is is 1, then they are logged. If it is 2, then
|
||||
* the config object is ignored and global filters will be used.
|
||||
*
|
||||
* Filter Object:
|
||||
* A filter object specifies what messages are affected by the corresponding
|
||||
* config object. file, func and subs are strings. If they are empty (length=0)
|
||||
* then they are not used for matching. If line is smaller than 0 then it is
|
||||
* ignored. Otherwise all given information must match.
|
||||
*
|
||||
* log_set_config(config):
|
||||
* This sets the global config which is used if no filter applies.
|
||||
*
|
||||
* log_add_filter(filter, config):
|
||||
* This adds a new filter to the global filter-list. If the filter matches the
|
||||
* given config shall apply. This returns a negative error code on failure.
|
||||
* Otherwise it returns an ID which can be used to remove the filter again.
|
||||
* An ID is always >= 0.
|
||||
*
|
||||
* log_rm_filter(id):
|
||||
* This removes the filter with ID=id.
|
||||
*
|
||||
* log_clean_filters():
|
||||
* This removes all filters. This frees all allocated memory by the filters.
|
||||
*
|
||||
*
|
||||
* If you want to set a config option which shall apply to all log-messages in
|
||||
* a single source-file, then you can add the following line to the head of the
|
||||
* source-file:
|
||||
* #define LOG_CONFIG LOG_CONFIG_ALL(options...)
|
||||
* Where LOG_CONFIG_ALL() can be replaced by LOG_CONFIG_DEBUG, LOG_CONFIG_INFO
|
||||
* and so on.
|
||||
* The LOG_CONFIG_*() macros create a log_config object on the stack and are
|
||||
* used for convenience. You can also provide your own log_config object here.
|
||||
* This LOG_CONFIG constant is picked up by all log-helpers which are provided
|
||||
* below. The raw log_submit and log_format() functions are not affected by
|
||||
* this, though.
|
||||
*/
|
||||
|
||||
/* dummy logger which allows gcc to check for printk-format */
|
||||
static inline __attribute__ ((format (printf, 1, 2)))
|
||||
void log_dummy(const char *format, ...)
|
||||
{
|
||||
}
|
||||
enum log_severity {
|
||||
LOG_DEBUG,
|
||||
LOG_INFO,
|
||||
LOG_NOTICE,
|
||||
LOG_WARNING,
|
||||
LOG_ERROR,
|
||||
LOG_CRITICAL,
|
||||
LOG_FATAL,
|
||||
LOG_SEV_NUM,
|
||||
};
|
||||
|
||||
__attribute__ ((format (printf, 1, 2)))
|
||||
void log_printf(const char *format, ...);
|
||||
__attribute__ ((format (printf, 1, 0)))
|
||||
void log_vprintf(const char *format, va_list list);
|
||||
#define LOG_STRMAX 128
|
||||
|
||||
#define log_crit(format, ...) log_printf(LOG_CRIT format, ##__VA_ARGS__)
|
||||
#define log_err(format, ...) log_printf(LOG_ERR format, ##__VA_ARGS__)
|
||||
#define log_warn(format, ...) log_printf(LOG_WARN format, ##__VA_ARGS__)
|
||||
#define log_notice(format, ...) log_printf(LOG_NOTICE format, ##__VA_ARGS__)
|
||||
#define log_info(format, ...) log_printf(LOG_INFO format, ##__VA_ARGS__)
|
||||
struct log_filter {
|
||||
char file[LOG_STRMAX];
|
||||
int line;
|
||||
char func[LOG_STRMAX];
|
||||
char subs[LOG_STRMAX];
|
||||
};
|
||||
|
||||
/* log_debug() should produce zero code if DEBUG is disabled */
|
||||
#ifndef NDEBUG
|
||||
#define log_debug(format, ...) log_printf(LOG_DEBUG format, ##__VA_ARGS__)
|
||||
#else
|
||||
#define log_debug(format, ...) log_dummy(LOG_DEBUG format, ##__VA_ARGS__)
|
||||
struct log_config {
|
||||
int sev[LOG_SEV_NUM];
|
||||
};
|
||||
|
||||
#define LOG_CONFIG_ALL(debug, info, notice, warning, error, critical, fatal) \
|
||||
(struct log_config){ .sev = { \
|
||||
[LOG_DEBUG] = (debug), \
|
||||
[LOG_INFO] = (info), \
|
||||
[LOG_NOTICE] = (notice), \
|
||||
[LOG_WARNING] = (warning), \
|
||||
[LOG_ERROR] = (error), \
|
||||
[LOG_CRITICAL] = (critical), \
|
||||
[LOG_FATAL] = (fatal), \
|
||||
} }
|
||||
|
||||
#define LOG_CONFIG_DEBUG(debug) \
|
||||
LOG_CONFIG_ALL((debug), 2, 2, 2, 2, 2, 2)
|
||||
#define LOG_CONFIG_INFO(debug, info) \
|
||||
LOG_CONFIG_ALL((debug), (info), 2, 2, 2, 2, 2)
|
||||
|
||||
void log_set_config(const struct log_config *config);
|
||||
int log_add_filter(const struct log_filter *filter,
|
||||
const struct log_config *config);
|
||||
void log_rm_filter(int handle);
|
||||
void log_clean_filter();
|
||||
|
||||
/*
|
||||
* Log-Functions
|
||||
* These functions pass a log-message to the log-subsystem. Handy helpers are
|
||||
* provided below. You almost never use these directly.
|
||||
*
|
||||
* log_submit:
|
||||
* Submit the message to the log-subsystem. This is the backend of all other
|
||||
* loggers.
|
||||
*
|
||||
* log_format:
|
||||
* Same as log_submit but first converts the arguments into a va_list object.
|
||||
*
|
||||
* log_set_file(file):
|
||||
* This opens the file specified by \file and redirects all new messages to this
|
||||
* file. If \file is NULL, then the default is used which is stderr.
|
||||
* Messages are appended to the file and no file-locks are used so you cannot
|
||||
* use a single file for multiple processes.
|
||||
* No log-file-roations or other backup/rotation functions are supported. Use a
|
||||
* proper init system like systemd to do this.
|
||||
*
|
||||
* log_print_init(appname):
|
||||
* This prints a message with build-time/date and appname to the log. You should
|
||||
* invoke this very early in your program. It is not required, though. However,
|
||||
* every message is prepended with a time-offset since application-start. This
|
||||
* offset is measured since the first log-message is sent so you should send
|
||||
* some log-message at application start. This is a handy-helper to do this.
|
||||
*/
|
||||
|
||||
void log_submit(const char *file,
|
||||
int line,
|
||||
const char *func,
|
||||
const struct log_config *config,
|
||||
const char *subs,
|
||||
enum log_severity sev,
|
||||
const char *format,
|
||||
va_list args);
|
||||
|
||||
void log_format(const char *file,
|
||||
int line,
|
||||
const char *func,
|
||||
const struct log_config *config,
|
||||
const char *subs,
|
||||
enum log_severity sev,
|
||||
const char *format,
|
||||
...);
|
||||
|
||||
int log_set_file(const char *file);
|
||||
void log_print_init(const char *appname);
|
||||
|
||||
/*
|
||||
* Default values
|
||||
* All helpers automatically pick-up the file, line, func, config and subsystem
|
||||
* parameters for a log-message. file, line and func are generated with
|
||||
* __FILE__, __LINE__ and __func__ and should almost never be replaced. The
|
||||
* config argument is by default NULL so global filters apply. You can use the
|
||||
* #define LOG_CONFIG ...
|
||||
* method to overwrite this. It is described above in detail.
|
||||
* The subsystem is by default an empty string. To overwrite this, add this
|
||||
* line to the top of your source file:
|
||||
* #define LOG_SUBSYSTEM "mysubsystem"
|
||||
* Then all following log-messages will use this string as subsystem.
|
||||
*
|
||||
* If you want to change one of these, you need to directly use log_submit and
|
||||
* log_format. If you want the defaults for file, line and func you can use:
|
||||
* log_format(LOG_DEFAULT_BASE, config, subsys, sev, format, ...);
|
||||
* If you want the default config, use:
|
||||
* log_format(LOG_DEFAULT_CONF, subsys, sev, format, ...);
|
||||
* If you want all default values, use:
|
||||
* log_format(LOG_DEFAULT, sev, format, ...);
|
||||
*
|
||||
* If you want to change a single value, this is the default line that is used
|
||||
* internally. Adjust it to your needs:
|
||||
* log_format(__FILE__, __LINE__, __func__, &LOG_CONFIG, LOG_SUBSYSTEM,
|
||||
* LOG_ERROR, "your format string: %s %d", "some args", 5, ...);
|
||||
*
|
||||
* log_printf is the same as log_format(LOG_DEFAULT, sev, format, ...) and is
|
||||
* the most basic wrapper that you can use.
|
||||
*/
|
||||
|
||||
#ifndef LOG_CONFIG
|
||||
extern const struct log_config LOG_CONFIG;
|
||||
#endif
|
||||
|
||||
#endif /* KMSCON_LOG_H */
|
||||
#ifndef LOG_SUBSYSTEM
|
||||
extern const char *LOG_SUBSYSTEM;
|
||||
#endif
|
||||
|
||||
#define LOG_DEFAULT_BASE __FILE__, __LINE__, __func__
|
||||
#define LOG_DEFAULT_CONF LOG_DEFAULT_BASE, &LOG_CONFIG
|
||||
#define LOG_DEFAULT LOG_DEFAULT_CONF, LOG_SUBSYSTEM
|
||||
|
||||
#define log_printf(sev, format, ...) \
|
||||
log_format(LOG_DEFAULT, (sev), (format), ##__VA_ARGS__)
|
||||
|
||||
/*
|
||||
* Helpers
|
||||
* The pick-up all the default values and submit the message to the
|
||||
* log-subsystem. The log_debug() function produces zero-code if
|
||||
* LOG_ENABLE_DEBUG is not defined. Therefore, it can be heavily used for
|
||||
* debugging and will not have any side-effects.
|
||||
*/
|
||||
|
||||
#ifdef LOG_ENABLE_DEBUG
|
||||
#define log_debug(format, ...) \
|
||||
log_printf(LOG_DEBUG, (format), ##__VA_ARGS__)
|
||||
#else
|
||||
#define log_debug(format, ...)
|
||||
#endif
|
||||
|
||||
#define log_info(format, ...) \
|
||||
log_printf(LOG_INFO, (format), ##__VA_ARGS__)
|
||||
#define log_notice(format, ...) \
|
||||
log_printf(LOG_NOTICE, (format), ##__VA_ARGS__)
|
||||
#define log_warning(format, ...) \
|
||||
log_printf(LOG_WARNING, (format), ##__VA_ARGS__)
|
||||
#define log_error(format, ...) \
|
||||
log_printf(LOG_ERROR, (format), ##__VA_ARGS__)
|
||||
#define log_critical(format, ...) \
|
||||
log_printf(LOG_CRITICAL, (format), ##__VA_ARGS__)
|
||||
#define log_fatal(format, ...) \
|
||||
log_printf(LOG_FATAL, (format), ##__VA_ARGS__)
|
||||
|
||||
#define log_dbg log_debug
|
||||
#define log_warn log_warning
|
||||
#define log_err log_error
|
||||
#define log_crit log_critical
|
||||
|
||||
#endif /* LOG_H_INCLUDED */
|
||||
Reference in new issue
Block a user