]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/libloc.c
fe44b3c159d5ab92cf5496c30f8b9ab0ad971ef7
2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
26 #include <loc/libloc.h>
27 #include "libloc-private.h"
31 void (*log_fn
)(struct loc_ctx
* ctx
,
32 int priority
, const char *file
, int line
, const char *fn
,
33 const char *format
, va_list args
);
37 void loc_log(struct loc_ctx
* ctx
,
38 int priority
, const char* file
, int line
, const char* fn
,
39 const char* format
, ...) {
42 va_start(args
, format
);
43 ctx
->log_fn(ctx
, priority
, file
, line
, fn
, format
, args
);
47 static void log_stderr(struct loc_ctx
* ctx
,
48 int priority
, const char* file
, int line
, const char* fn
,
49 const char* format
, va_list args
) {
50 fprintf(stderr
, "libloc: %s: ", fn
);
51 vfprintf(stderr
, format
, args
);
54 static int log_priority(const char* priority
) {
57 int prio
= strtol(priority
, &endptr
, 10);
59 if (endptr
[0] == '\0' || isspace(endptr
[0]))
62 if (strncmp(priority
, "err", 3) == 0)
65 if (strncmp(priority
, "info", 4) == 0)
68 if (strncmp(priority
, "debug", 5) == 0)
74 LOC_EXPORT
int loc_new(struct loc_ctx
** ctx
) {
75 struct loc_ctx
* c
= calloc(1, sizeof(*c
));
80 c
->log_fn
= log_stderr
;
81 c
->log_priority
= LOG_ERR
;
83 const char* env
= secure_getenv("LOC_LOG");
85 loc_set_log_priority(c
, log_priority(env
));
87 INFO(c
, "ctx %p created\n", c
);
88 DEBUG(c
, "log_priority=%d\n", c
->log_priority
);
94 LOC_EXPORT
struct loc_ctx
* loc_ref(struct loc_ctx
* ctx
) {
103 LOC_EXPORT
struct loc_ctx
* loc_unref(struct loc_ctx
* ctx
) {
107 if (--ctx
->refcount
> 0)
110 INFO(ctx
, "context %p released\n", ctx
);
116 LOC_EXPORT
void loc_set_log_fn(struct loc_ctx
* ctx
,
117 void (*log_fn
)(struct loc_ctx
* ctx
, int priority
, const char* file
,
118 int line
, const char* fn
, const char* format
, va_list args
)) {
119 ctx
->log_fn
= log_fn
;
120 INFO(ctx
, "custom logging function %p registered\n", log_fn
);
123 LOC_EXPORT
int loc_get_log_priority(struct loc_ctx
* ctx
) {
124 return ctx
->log_priority
;
127 LOC_EXPORT
void loc_set_log_priority(struct loc_ctx
* ctx
, int priority
) {
128 ctx
->log_priority
= priority
;