]> git.ipfire.org Git - location/libloc.git/blob - src/libloc.c
Don't abuse errno as return code
[location/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 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
39 void 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
49 static 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
56 static 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
76 LOC_EXPORT int loc_new(struct loc_ctx** ctx) {
77 struct loc_ctx* c = calloc(1, sizeof(*c));
78 if (!c)
79 return 1;
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
96 LOC_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
105 LOC_EXPORT struct loc_ctx* loc_unref(struct loc_ctx* ctx) {
106 if (!ctx)
107 return NULL;
108
109 if (--ctx->refcount > 0)
110 return NULL;
111
112 INFO(ctx, "context %p released\n", ctx);
113 free(ctx);
114
115 return NULL;
116 }
117
118 LOC_EXPORT void loc_set_log_fn(struct loc_ctx* ctx,
119 void (*log_fn)(struct loc_ctx* ctx, int priority, const char* file,
120 int line, const char* fn, const char* format, va_list args)) {
121 ctx->log_fn = log_fn;
122 INFO(ctx, "custom logging function %p registered\n", log_fn);
123 }
124
125 LOC_EXPORT int loc_get_log_priority(struct loc_ctx* ctx) {
126 return ctx->log_priority;
127 }
128
129 LOC_EXPORT void loc_set_log_priority(struct loc_ctx* ctx, int priority) {
130 ctx->log_priority = priority;
131 }