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.
17 #include <arpa/inet.h>
18 #include <netinet/in.h>
27 #include <libloc/libloc.h>
28 #include <libloc/compat.h>
29 #include <libloc/private.h>
35 struct loc_ctx_logging
{
39 loc_log_callback callback
;
44 void loc_log(struct loc_ctx
* ctx
,
45 int priority
, const char* file
, int line
, const char* fn
,
46 const char* format
, ...) {
49 va_start(args
, format
);
50 ctx
->log
.callback(ctx
, ctx
->log
.data
, priority
, file
, line
, fn
, format
, args
);
54 static void log_stderr(struct loc_ctx
* ctx
, void* data
,
55 int priority
, const char* file
, int line
, const char* fn
,
56 const char* format
, va_list args
) {
57 fprintf(stderr
, "libloc: %s: ", fn
);
58 vfprintf(stderr
, format
, args
);
61 static int log_priority(const char* priority
) {
64 int prio
= strtol(priority
, &endptr
, 10);
66 if (endptr
[0] == '\0' || isspace(endptr
[0]))
69 if (strncmp(priority
, "err", 3) == 0)
72 if (strncmp(priority
, "info", 4) == 0)
75 if (strncmp(priority
, "debug", 5) == 0)
81 LOC_EXPORT
int loc_new(struct loc_ctx
** ctx
) {
82 struct loc_ctx
* c
= calloc(1, sizeof(*c
));
87 c
->log
.callback
= log_stderr
;
88 c
->log
.priority
= LOG_ERR
;
90 const char* env
= secure_getenv("LOC_LOG");
92 loc_set_log_priority(c
, log_priority(env
));
94 INFO(c
, "ctx %p created\n", c
);
95 DEBUG(c
, "log_priority=%d\n", c
->log
.priority
);
101 LOC_EXPORT
struct loc_ctx
* loc_ref(struct loc_ctx
* ctx
) {
110 LOC_EXPORT
struct loc_ctx
* loc_unref(struct loc_ctx
* ctx
) {
111 if (--ctx
->refcount
> 0)
114 INFO(ctx
, "context %p released\n", ctx
);
120 LOC_EXPORT
void loc_set_log_callback(struct loc_ctx
* ctx
, loc_log_callback callback
, void* data
) {
121 ctx
->log
.callback
= callback
;
122 ctx
->log
.data
= data
;
125 LOC_EXPORT
void loc_set_log_fn(struct loc_ctx
* ctx
,
126 void (*log_fn
)(struct loc_ctx
* ctx
, int priority
, const char* file
,
127 int line
, const char* fn
, const char* format
, va_list args
)) {
128 //ctx->log_fn = log_fn;
129 INFO(ctx
, "custom logging function %p registered\n", log_fn
);
132 LOC_EXPORT
int loc_get_log_priority(struct loc_ctx
* ctx
) {
133 return ctx
->log
.priority
;
136 LOC_EXPORT
void loc_set_log_priority(struct loc_ctx
* ctx
, int priority
) {
137 ctx
->log
.priority
= priority
;