]> git.ipfire.org Git - people/ms/libloc.git/blob - src/libloc.c
Initial commit
[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
29 struct loc_ctx {
30 int refcount;
31 void (*log_fn)(struct loc_ctx* ctx,
32 int priority, const char *file, int line, const char *fn,
33 const char *format, va_list args);
34 int log_priority;
35 };
36
37 void loc_log(struct loc_ctx* ctx,
38 int priority, const char* file, int line, const char* fn,
39 const char* format, ...) {
40 va_list args;
41
42 va_start(args, format);
43 ctx->log_fn(ctx, priority, file, line, fn, format, args);
44 va_end(args);
45 }
46
47 static void log_stderr(struct loc_ctx* ctx,
48 int priority, const char* file, int line, const char* fn,
49 const char* format, va_list args) {
50 fprintf(stderr, "libloc: %s: ", fn);
51 vfprintf(stderr, format, args);
52 }
53
54 static int log_priority(const char* priority) {
55 char *endptr;
56
57 int prio = strtol(priority, &endptr, 10);
58
59 if (endptr[0] == '\0' || isspace(endptr[0]))
60 return prio;
61
62 if (strncmp(priority, "err", 3) == 0)
63 return LOG_ERR;
64
65 if (strncmp(priority, "info", 4) == 0)
66 return LOG_INFO;
67
68 if (strncmp(priority, "debug", 5) == 0)
69 return LOG_DEBUG;
70
71 return 0;
72 }
73
74 LOC_EXPORT int loc_new(struct loc_ctx** ctx) {
75 struct loc_ctx* c = calloc(1, sizeof(*c));
76 if (!c)
77 return -ENOMEM;
78
79 c->refcount = 1;
80 c->log_fn = log_stderr;
81 c->log_priority = LOG_ERR;
82
83 const char* env = secure_getenv("LOC_LOG");
84 if (env)
85 loc_set_log_priority(c, log_priority(env));
86
87 INFO(c, "ctx %p created\n", c);
88 DEBUG(c, "log_priority=%d\n", c->log_priority);
89 *ctx = c;
90
91 return 0;
92 }
93
94 LOC_EXPORT struct loc_ctx* loc_ref(struct loc_ctx* ctx) {
95 if (!ctx)
96 return NULL;
97
98 ctx->refcount++;
99
100 return ctx;
101 }
102
103 LOC_EXPORT struct loc_ctx* loc_unref(struct loc_ctx* ctx) {
104 if (!ctx)
105 return NULL;
106
107 if (--ctx->refcount > 0)
108 return NULL;
109
110 INFO(ctx, "context %p released\n", ctx);
111 free(ctx);
112
113 return NULL;
114 }
115
116 LOC_EXPORT void loc_set_log_fn(struct loc_ctx* ctx,
117 void (*log_fn)(struct loc_ctx* ctx, int priority, const char* file,
118 int line, const char* fn, const char* format, va_list args)) {
119 ctx->log_fn = log_fn;
120 INFO(ctx, "custom logging function %p registered\n", log_fn);
121 }
122
123 LOC_EXPORT int loc_get_log_priority(struct loc_ctx* ctx) {
124 return ctx->log_priority;
125 }
126
127 LOC_EXPORT void loc_set_log_priority(struct loc_ctx* ctx, int priority) {
128 ctx->log_priority = priority;
129 }