]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/libloc.c
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"
32 void (*log_fn
)(struct loc_ctx
* ctx
,
33 int priority
, const char *file
, int line
, const char *fn
,
34 const char *format
, va_list args
);
37 struct loc_database
* db
;
40 void loc_log(struct loc_ctx
* ctx
,
41 int priority
, const char* file
, int line
, const char* fn
,
42 const char* format
, ...) {
45 va_start(args
, format
);
46 ctx
->log_fn(ctx
, priority
, file
, line
, fn
, format
, args
);
50 static void log_stderr(struct loc_ctx
* ctx
,
51 int priority
, const char* file
, int line
, const char* fn
,
52 const char* format
, va_list args
) {
53 fprintf(stderr
, "libloc: %s: ", fn
);
54 vfprintf(stderr
, format
, args
);
57 static int log_priority(const char* priority
) {
60 int prio
= strtol(priority
, &endptr
, 10);
62 if (endptr
[0] == '\0' || isspace(endptr
[0]))
65 if (strncmp(priority
, "err", 3) == 0)
68 if (strncmp(priority
, "info", 4) == 0)
71 if (strncmp(priority
, "debug", 5) == 0)
77 LOC_EXPORT
int loc_new(struct loc_ctx
** ctx
) {
78 struct loc_ctx
* c
= calloc(1, sizeof(*c
));
83 c
->log_fn
= log_stderr
;
84 c
->log_priority
= LOG_ERR
;
88 const char* env
= secure_getenv("LOC_LOG");
90 loc_set_log_priority(c
, log_priority(env
));
92 INFO(c
, "ctx %p created\n", c
);
93 DEBUG(c
, "log_priority=%d\n", c
->log_priority
);
99 LOC_EXPORT
struct loc_ctx
* loc_ref(struct loc_ctx
* ctx
) {
108 LOC_EXPORT
struct loc_ctx
* loc_unref(struct loc_ctx
* ctx
) {
112 if (--ctx
->refcount
> 0)
115 // Release any loaded databases
117 loc_database_unref(ctx
->db
);
119 INFO(ctx
, "context %p released\n", ctx
);
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
;
140 LOC_EXPORT
int loc_load(struct loc_ctx
* ctx
, const char* path
) {
141 FILE* f
= fopen(path
, "r");
145 // Release any previously openend database
147 loc_database_unref(ctx
->db
);
149 // Open the new database
150 int r
= loc_database_new(ctx
, &ctx
->db
, f
);