]>
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> | |
30 | #include "pcep_utils_logging.h" | |
31 | ||
32 | /* Forward declaration */ | |
33 | int pcep_stdout_logger(int priority, const char *format, va_list args); | |
34 | ||
35 | static pcep_logger_func logger_func = pcep_stdout_logger; | |
36 | static int logging_level_ = LOG_INFO; | |
37 | ||
38 | void register_logger(pcep_logger_func logger) | |
39 | { | |
40 | logger_func = logger; | |
41 | } | |
42 | ||
43 | void set_logging_level(int level) | |
44 | { | |
45 | logging_level_ = level; | |
46 | } | |
47 | ||
48 | int get_logging_level() | |
49 | { | |
50 | return logging_level_; | |
51 | } | |
52 | ||
53 | void pcep_log(int priority, const char *format, ...) | |
54 | { | |
55 | va_list va; | |
56 | va_start(va, format); | |
57 | logger_func(priority, format, va); | |
58 | va_end(va); | |
59 | } | |
60 | ||
61 | void pcep_log_hexbytes(int priority, const char *message, const uint8_t *bytes, | |
62 | uint8_t bytes_len) | |
63 | { | |
64 | char byte_str[2048] = {0}; | |
65 | int i = 0; | |
66 | ||
67 | snprintf(byte_str, 2048, "%s ", message); | |
68 | for (; i < bytes_len; i++) { | |
69 | snprintf(byte_str, 2048, "%02x ", bytes[i]); | |
70 | } | |
71 | snprintf(byte_str, 2048, "\n"); | |
72 | ||
73 | pcep_log(priority, "%s", byte_str); | |
74 | } | |
75 | ||
76 | /* Defined with a return type to match the FRR logging signature. | |
77 | * Assuming glibc printf() is thread-safe. */ | |
78 | int pcep_stdout_logger(int priority, const char *format, va_list args) | |
79 | { | |
80 | if (priority <= logging_level_) { | |
81 | vprintf(format, args); | |
82 | printf("\n"); | |
83 | } | |
84 | ||
85 | return 0; | |
86 | } |