]>
git.ipfire.org Git - network.git/blob - src/libnetwork/libnetwork.c
09651a066537c9c81630d57043a97d91b2626a49
1 /*#############################################################################
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2017 IPFire Network Development Team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program 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 #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
29 #include <network/libnetwork.h>
30 #include <network/logging.h>
31 #include "libnetwork-private.h"
37 void (*log_fn
)(struct network_ctx
* ctx
,
38 int priority
, const char *file
, int line
, const char *fn
,
39 const char *format
, va_list args
);
43 void network_log(struct network_ctx
* ctx
,
44 int priority
, const char* file
, int line
, const char* fn
,
45 const char* format
, ...) {
48 va_start(args
, format
);
49 ctx
->log_fn(ctx
, priority
, file
, line
, fn
, format
, args
);
53 static void log_stderr(struct network_ctx
* ctx
,
54 int priority
, const char* file
, int line
, const char* fn
,
55 const char* format
, va_list args
) {
56 fprintf(stderr
, "libnetwork: %s: ", fn
);
57 vfprintf(stderr
, format
, args
);
60 static int log_priority(const char* priority
) {
63 int prio
= strtol(priority
, &endptr
, 10);
65 if (endptr
[0] == '\0' || isspace(endptr
[0]))
68 if (strncmp(priority
, "err", 3) == 0)
71 if (strncmp(priority
, "info", 4) == 0)
74 if (strncmp(priority
, "debug", 5) == 0)
80 NETWORK_EXPORT
int network_new(struct network_ctx
** ctx
) {
81 struct network_ctx
* c
= calloc(1, sizeof(*c
));
85 // Initialise basic variables
89 c
->log_fn
= log_stderr
;
90 c
->log_priority
= LOG_ERR
;
92 const char* env
= secure_getenv("NETWORK_LOG");
94 network_set_log_priority(c
, log_priority(env
));
96 INFO(c
, "network ctx %p created\n", c
);
97 DEBUG(c
, "log_priority=%d\n", c
->log_priority
);
103 NETWORK_EXPORT
struct network_ctx
* network_ref(struct network_ctx
* ctx
) {
111 static void network_free(struct network_ctx
* ctx
) {
112 DEBUG(ctx
, "network ctx %p released\n", ctx
);
115 NETWORK_EXPORT
struct network_ctx
* network_unref(struct network_ctx
* ctx
) {
119 if (--ctx
->refcount
> 0)
126 NETWORK_EXPORT
void network_set_log_fn(struct network_ctx
* ctx
,
127 void (*log_fn
)(struct network_ctx
* ctx
, int priority
, const char* file
,
128 int line
, const char* fn
, const char* format
, va_list args
)) {
129 ctx
->log_fn
= log_fn
;
130 INFO(ctx
, "custom logging function %p registered\n", log_fn
);
133 NETWORK_EXPORT
int network_get_log_priority(struct network_ctx
* ctx
) {
134 return ctx
->log_priority
;
137 NETWORK_EXPORT
void network_set_log_priority(struct network_ctx
* ctx
, int priority
) {
138 ctx
->log_priority
= priority
;
141 NETWORK_EXPORT
const char* network_version() {
142 return "network " VERSION
;