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