]> git.ipfire.org Git - people/ms/libloc.git/blob - src/libloc.c
Split database into a writer and reader
[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 <stdio.h>
18 #include <stdlib.h>
19 #include <stddef.h>
20 #include <stdarg.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <ctype.h>
25
26 #include <loc/libloc.h>
27 #include "libloc-private.h"
28 #include "database.h"
29
30 struct loc_ctx {
31 int refcount;
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);
35 int log_priority;
36
37 struct loc_database* db;
38 };
39
40 void loc_log(struct loc_ctx* ctx,
41 int priority, const char* file, int line, const char* fn,
42 const char* format, ...) {
43 va_list args;
44
45 va_start(args, format);
46 ctx->log_fn(ctx, priority, file, line, fn, format, args);
47 va_end(args);
48 }
49
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);
55 }
56
57 static int log_priority(const char* priority) {
58 char *endptr;
59
60 int prio = strtol(priority, &endptr, 10);
61
62 if (endptr[0] == '\0' || isspace(endptr[0]))
63 return prio;
64
65 if (strncmp(priority, "err", 3) == 0)
66 return LOG_ERR;
67
68 if (strncmp(priority, "info", 4) == 0)
69 return LOG_INFO;
70
71 if (strncmp(priority, "debug", 5) == 0)
72 return LOG_DEBUG;
73
74 return 0;
75 }
76
77 LOC_EXPORT int loc_new(struct loc_ctx** ctx) {
78 struct loc_ctx* c = calloc(1, sizeof(*c));
79 if (!c)
80 return -ENOMEM;
81
82 c->refcount = 1;
83 c->log_fn = log_stderr;
84 c->log_priority = LOG_ERR;
85
86 c->db = NULL;
87
88 const char* env = secure_getenv("LOC_LOG");
89 if (env)
90 loc_set_log_priority(c, log_priority(env));
91
92 INFO(c, "ctx %p created\n", c);
93 DEBUG(c, "log_priority=%d\n", c->log_priority);
94 *ctx = c;
95
96 return 0;
97 }
98
99 LOC_EXPORT struct loc_ctx* loc_ref(struct loc_ctx* ctx) {
100 if (!ctx)
101 return NULL;
102
103 ctx->refcount++;
104
105 return ctx;
106 }
107
108 LOC_EXPORT struct loc_ctx* loc_unref(struct loc_ctx* ctx) {
109 if (!ctx)
110 return NULL;
111
112 if (--ctx->refcount > 0)
113 return NULL;
114
115 // Release any loaded databases
116 if (ctx->db)
117 loc_database_unref(ctx->db);
118
119 INFO(ctx, "context %p released\n", ctx);
120 free(ctx);
121
122 return NULL;
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 }
139
140 LOC_EXPORT int loc_load(struct loc_ctx* ctx, const char* path) {
141 FILE* f = fopen(path, "r");
142 if (!f)
143 return -errno;
144
145 // Release any previously openend database
146 if (ctx->db)
147 loc_database_unref(ctx->db);
148
149 // Open the new database
150 int r = loc_database_new(ctx, &ctx->db, f);
151 if (r)
152 return r;
153
154 // Close the file
155 fclose(f);
156
157 return 0;
158 }