liblightify
log.c
Go to the documentation of this file.
1 /*
2  liblightify -- library to control OSRAM's LIGHTIFY
3 
4 Copyright (c) 2015, Tobias Frost <tobi@coldtobi.de>
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14  * Neither the name of the author nor the
15  names of its contributors may be used to endorse or promote products
16  derived from this software without specific prior written permission.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
22 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 // Logging support
31 
32 #include "liblightify-private.h"
33 
34 #include "log.h"
35 #include "context.h"
36 
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <stdint.h>
40 #include <syslog.h>
41 #include <ctype.h>
42 #include <string.h>
43 
45  int priority, const char *file, int line, const char *fn,
46  const char *format, ...)
47 {
48  va_list args;
49  va_start(args, format);
50  ctx->log_fn(ctx, priority, file, line, fn, format, args);
51  va_end(args);
52 }
53 
55  int priority, const char *file, int line, const char *fn,
56  const char *format, va_list args)
57 {
58  fprintf(stderr, "liblightify: %s: ", fn);
59  vfprintf(stderr, format, args);
60 }
61 
62 int log_priority(const char *priority)
63 {
64  char *endptr;
65  int prio;
66 
67  prio = strtol(priority, &endptr, 10);
68  if (endptr[0] == '\0' || isspace(endptr[0]))
69  return prio;
70  if (strncmp(priority, "err", 3) == 0)
71  return LOG_ERR;
72  if (strncmp(priority, "info", 4) == 0)
73  return LOG_INFO;
74  if (strncmp(priority, "debug", 5) == 0)
75  return LOG_DEBUG;
76  return 0;
77 }
78 
90  void (*log_fn)(struct lightify_ctx *ctx, int priority, const char *file,
91  int line, const char *fn, const char *format, va_list args))
92 {
93  if (!ctx) return -EINVAL;
94  ctx->log_fn = log_fn;
95  info(ctx, "custom logging function %p registered\n", log_fn);
96  return 0;
97 }
98 
100 {
101  if (!ctx) return -EINVAL;
102  return ctx->log_priority;
103 }
104 
106 {
107  if (!ctx) return -EINVAL;
108  ctx->log_priority = priority;
109  return 0;
110 }
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
static void const char * format
#define info(ctx, arg...)
LIGHTIFY_EXPORT int lightify_set_log_priority(struct lightify_ctx *ctx, int priority)
Definition: log.c:105
#define LIGHTIFY_EXPORT
void lightify_log(struct lightify_ctx *ctx, int priority, const char *file, int line, const char *fn, const char *format,...)
Definition: log.c:44
int log_priority
Definition: context.h:84
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:70
LIGHTIFY_EXPORT int lightify_get_log_priority(struct lightify_ctx *ctx)
Definition: log.c:99
int log_priority(const char *priority)
Definition: log.c:62
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
struct lightify_ctx * ctx
Definition: node.c:46