]>
Commit | Line | Data |
---|---|---|
74971473 JG |
1 | /* |
2 | * This file is part of the PCEPlib, a PCEP protocol library. | |
3 | * | |
4 | * Copyright (C) 2020 Volta Networks https://voltanet.io/ | |
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 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 | * You should have received a copy of the GNU Lesser General Public License | |
17 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
18 | * | |
19 | * Author : Brady Johnson <brady@voltanet.io> | |
20 | * | |
21 | */ | |
22 | ||
23 | ||
1f8031f7 DL |
24 | #ifdef HAVE_CONFIG_H |
25 | #include "config.h" | |
26 | #endif | |
27 | ||
74971473 JG |
28 | #include <stdarg.h> |
29 | #include <stdio.h> | |
0f9de11a | 30 | #include "compiler.h" |
74971473 JG |
31 | #include "pcep_utils_logging.h" |
32 | ||
33 | /* Forward declaration */ | |
0f9de11a DL |
34 | int pcep_stdout_logger(int priority, const char *format, va_list args) |
35 | PRINTFRR(2, 0); | |
74971473 JG |
36 | |
37 | static pcep_logger_func logger_func = pcep_stdout_logger; | |
38 | static int logging_level_ = LOG_INFO; | |
39 | ||
40 | void register_logger(pcep_logger_func logger) | |
41 | { | |
42 | logger_func = logger; | |
43 | } | |
44 | ||
45 | void set_logging_level(int level) | |
46 | { | |
47 | logging_level_ = level; | |
48 | } | |
49 | ||
2816045a | 50 | int get_logging_level(void) |
74971473 JG |
51 | { |
52 | return logging_level_; | |
53 | } | |
54 | ||
55 | void pcep_log(int priority, const char *format, ...) | |
56 | { | |
57 | va_list va; | |
58 | va_start(va, format); | |
59 | logger_func(priority, format, va); | |
60 | va_end(va); | |
61 | } | |
62 | ||
63 | void pcep_log_hexbytes(int priority, const char *message, const uint8_t *bytes, | |
64 | uint8_t bytes_len) | |
65 | { | |
66 | char byte_str[2048] = {0}; | |
67 | int i = 0; | |
68 | ||
69 | snprintf(byte_str, 2048, "%s ", message); | |
70 | for (; i < bytes_len; i++) { | |
71 | snprintf(byte_str, 2048, "%02x ", bytes[i]); | |
72 | } | |
73 | snprintf(byte_str, 2048, "\n"); | |
74 | ||
75 | pcep_log(priority, "%s", byte_str); | |
76 | } | |
77 | ||
78 | /* Defined with a return type to match the FRR logging signature. | |
79 | * Assuming glibc printf() is thread-safe. */ | |
80 | int pcep_stdout_logger(int priority, const char *format, va_list args) | |
81 | { | |
82 | if (priority <= logging_level_) { | |
83 | vprintf(format, args); | |
84 | printf("\n"); | |
85 | } | |
86 | ||
87 | return 0; | |
88 | } |