]> git.ipfire.org Git - people/ms/libloc.git/blob - src/libloc.c
importer: Ensure that we set timestamps in the announcements table
[people/ms/libloc.git] / src / libloc.c
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
17 #include <arpa/inet.h>
18 #include <netinet/in.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <stdarg.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <ctype.h>
26
27 #include <libloc/libloc.h>
28 #include <libloc/compat.h>
29 #include <libloc/private.h>
30
31 struct loc_ctx {
32 int refcount;
33
34 // Logging
35 struct loc_ctx_logging {
36 int priority;
37
38 // Callback
39 loc_log_callback callback;
40 void* data;
41 } log;
42 };
43
44 void loc_log(struct loc_ctx* ctx,
45 int priority, const char* file, int line, const char* fn,
46 const char* format, ...) {
47 va_list args;
48
49 va_start(args, format);
50 ctx->log.callback(ctx, ctx->log.data, priority, file, line, fn, format, args);
51 va_end(args);
52 }
53
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);
59 }
60
61 static int log_priority(const char* priority) {
62 char *endptr;
63
64 int prio = strtol(priority, &endptr, 10);
65
66 if (endptr[0] == '\0' || isspace(endptr[0]))
67 return prio;
68
69 if (strncmp(priority, "err", 3) == 0)
70 return LOG_ERR;
71
72 if (strncmp(priority, "info", 4) == 0)
73 return LOG_INFO;
74
75 if (strncmp(priority, "debug", 5) == 0)
76 return LOG_DEBUG;
77
78 return 0;
79 }
80
81 LOC_EXPORT int loc_new(struct loc_ctx** ctx) {
82 struct loc_ctx* c = calloc(1, sizeof(*c));
83 if (!c)
84 return 1;
85
86 c->refcount = 1;
87 c->log.callback = log_stderr;
88 c->log.priority = LOG_ERR;
89
90 const char* env = secure_getenv("LOC_LOG");
91 if (env)
92 loc_set_log_priority(c, log_priority(env));
93
94 INFO(c, "ctx %p created\n", c);
95 DEBUG(c, "log_priority=%d\n", c->log.priority);
96 *ctx = c;
97
98 return 0;
99 }
100
101 LOC_EXPORT struct loc_ctx* loc_ref(struct loc_ctx* ctx) {
102 if (!ctx)
103 return NULL;
104
105 ctx->refcount++;
106
107 return ctx;
108 }
109
110 LOC_EXPORT struct loc_ctx* loc_unref(struct loc_ctx* ctx) {
111 if (--ctx->refcount > 0)
112 return NULL;
113
114 INFO(ctx, "context %p released\n", ctx);
115 free(ctx);
116
117 return NULL;
118 }
119
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;
123 }
124
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);
130 }
131
132 LOC_EXPORT int loc_get_log_priority(struct loc_ctx* ctx) {
133 return ctx->log.priority;
134 }
135
136 LOC_EXPORT void loc_set_log_priority(struct loc_ctx* ctx, int priority) {
137 ctx->log.priority = priority;
138 }