]> git.ipfire.org Git - people/ms/libloc.git/blame - src/libloc.c
importer: Drop EDROP as it has been merged into DROP
[people/ms/libloc.git] / src / libloc.c
CommitLineData
46aded9a
MT
1/*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
5
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.
10
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.
15*/
16
2a30e4de
MT
17#include <arpa/inet.h>
18#include <netinet/in.h>
46aded9a
MT
19#include <stdio.h>
20#include <stdlib.h>
21#include <stddef.h>
22#include <stdarg.h>
23#include <unistd.h>
46aded9a
MT
24#include <string.h>
25#include <ctype.h>
26
c12a1385
MT
27#include <libloc/libloc.h>
28#include <libloc/compat.h>
29#include <libloc/private.h>
46aded9a
MT
30
31struct loc_ctx {
32 int refcount;
33 void (*log_fn)(struct loc_ctx* ctx,
34 int priority, const char *file, int line, const char *fn,
35 const char *format, va_list args);
36 int log_priority;
37};
38
39void loc_log(struct loc_ctx* ctx,
40 int priority, const char* file, int line, const char* fn,
41 const char* format, ...) {
42 va_list args;
43
44 va_start(args, format);
45 ctx->log_fn(ctx, priority, file, line, fn, format, args);
46 va_end(args);
47}
48
49static void log_stderr(struct loc_ctx* ctx,
50 int priority, const char* file, int line, const char* fn,
51 const char* format, va_list args) {
52 fprintf(stderr, "libloc: %s: ", fn);
53 vfprintf(stderr, format, args);
54}
55
56static int log_priority(const char* priority) {
57 char *endptr;
58
59 int prio = strtol(priority, &endptr, 10);
60
61 if (endptr[0] == '\0' || isspace(endptr[0]))
62 return prio;
63
64 if (strncmp(priority, "err", 3) == 0)
65 return LOG_ERR;
66
67 if (strncmp(priority, "info", 4) == 0)
68 return LOG_INFO;
69
70 if (strncmp(priority, "debug", 5) == 0)
71 return LOG_DEBUG;
72
73 return 0;
74}
75
76LOC_EXPORT int loc_new(struct loc_ctx** ctx) {
77 struct loc_ctx* c = calloc(1, sizeof(*c));
78 if (!c)
198e382c 79 return 1;
46aded9a
MT
80
81 c->refcount = 1;
82 c->log_fn = log_stderr;
83 c->log_priority = LOG_ERR;
84
85 const char* env = secure_getenv("LOC_LOG");
86 if (env)
87 loc_set_log_priority(c, log_priority(env));
88
89 INFO(c, "ctx %p created\n", c);
90 DEBUG(c, "log_priority=%d\n", c->log_priority);
91 *ctx = c;
92
93 return 0;
94}
95
96LOC_EXPORT struct loc_ctx* loc_ref(struct loc_ctx* ctx) {
97 if (!ctx)
98 return NULL;
99
100 ctx->refcount++;
101
102 return ctx;
103}
104
105LOC_EXPORT struct loc_ctx* loc_unref(struct loc_ctx* ctx) {
46aded9a
MT
106 if (--ctx->refcount > 0)
107 return NULL;
108
109 INFO(ctx, "context %p released\n", ctx);
110 free(ctx);
111
112 return NULL;
113}
114
115LOC_EXPORT void loc_set_log_fn(struct loc_ctx* ctx,
116 void (*log_fn)(struct loc_ctx* ctx, int priority, const char* file,
117 int line, const char* fn, const char* format, va_list args)) {
118 ctx->log_fn = log_fn;
119 INFO(ctx, "custom logging function %p registered\n", log_fn);
120}
121
122LOC_EXPORT int loc_get_log_priority(struct loc_ctx* ctx) {
123 return ctx->log_priority;
124}
125
126LOC_EXPORT void loc_set_log_priority(struct loc_ctx* ctx, int priority) {
127 ctx->log_priority = priority;
128}