log.c
Go to the documentation of this file.
/*
liblightify -- library to control OSRAM's LIGHTIFY
Copyright (c) 2015, Tobias Frost <tobi@coldtobi.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of the author nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 <COPYRIGHT HOLDER> 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.
*/
// Logging support
#include "liblightify-private.h"
#include "log.h"
#include "context.h"
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
#include <syslog.h>
#include <ctype.h>
#include <string.h>
void lightify_log(struct lightify_ctx *ctx,
int priority, const char *file, int line, const char *fn,
const char *format, ...)
{
va_list args;
va_start(args, format);
ctx->log_fn(ctx, priority, file, line, fn, format, args);
va_end(args);
}
void log_stderr(struct lightify_ctx *ctx,
int priority, const char *file, int line, const char *fn,
const char *format, va_list args)
{
fprintf(stderr, "liblightify: %s: ", fn);
vfprintf(stderr, format, args);
}
int log_priority(const char *priority)
{
char *endptr;
int prio;
prio = strtol(priority, &endptr, 10);
if (endptr[0] == '\0' || isspace(endptr[0]))
return prio;
if (strncmp(priority, "err", 3) == 0)
return LOG_ERR;
if (strncmp(priority, "info", 4) == 0)
return LOG_INFO;
if (strncmp(priority, "debug", 5) == 0)
return LOG_DEBUG;
return 0;
}
LIGHTIFY_EXPORT int lightify_set_log_fn(struct lightify_ctx *ctx,
void (*log_fn)(struct lightify_ctx *ctx, int priority, const char *file,
int line, const char *fn, const char *format, va_list args))
{
if (!ctx) return -EINVAL;
ctx->log_fn = log_fn;
info(ctx, "custom logging function %p registered\n", log_fn);
return 0;
}
LIGHTIFY_EXPORT int lightify_get_log_priority(struct lightify_ctx *ctx)
{
if (!ctx) return -EINVAL;
return ctx->log_priority;
}
LIGHTIFY_EXPORT int lightify_set_log_priority(struct lightify_ctx *ctx, int priority)
{
if (!ctx) return -EINVAL;
ctx->log_priority = priority;
return 0;
}
context.h
lightify_set_log_fn
LIGHTIFY_EXPORT int lightify_set_log_fn(struct lightify_ctx *ctx, void(*log_fn)(struct lightify_ctx *ctx, int priority, const char *file, int line, const char *fn, const char *format, va_list args))
Definition: log.c:89
format
static void const char * format
Definition: liblightify-private.h:42
info
#define info(ctx, arg...)
lightify_set_log_priority
LIGHTIFY_EXPORT int lightify_set_log_priority(struct lightify_ctx *ctx, int priority)
Definition: log.c:105
lightify_ctx
Definition: context.h:63
liblightify-private.h
LIGHTIFY_EXPORT
#define LIGHTIFY_EXPORT
lightify_log
void lightify_log(struct lightify_ctx *ctx, int priority, const char *file, int line, const char *fn, const char *format,...)
Definition: log.c:44
lightify_ctx::log_priority
int log_priority
Definition: context.h:79
lightify_ctx::log_fn
void(* log_fn)(struct lightify_ctx *ctx, int priority, const char *file, int line, const char *fn, const char *format, va_list args)
Definition: context.h:65
lightify_get_log_priority
LIGHTIFY_EXPORT int lightify_get_log_priority(struct lightify_ctx *ctx)
Definition: log.c:99
log_priority
int log_priority(const char *priority)
Definition: log.c:62
log.h
log_stderr
void log_stderr(struct lightify_ctx *ctx, int priority, const char *file, int line, const char *fn, const char *format, va_list args)
Definition: log.c:54
lightify_node::ctx
struct lightify_ctx * ctx
Definition: node.c:46