]> git.proxmox.com Git - mirror_frr.git/blob - pceplib/pcep_utils_logging.c
*: auto-convert to SPDX License IDs
[mirror_frr.git] / pceplib / pcep_utils_logging.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * This file is part of the PCEPlib, a PCEP protocol library.
4 *
5 * Copyright (C) 2020 Volta Networks https://voltanet.io/
6 *
7 * Author : Brady Johnson <brady@voltanet.io>
8 *
9 */
10
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <stdarg.h>
17 #include <stdio.h>
18 #include "compiler.h"
19 #include "pcep_utils_logging.h"
20
21 /* Forward declaration */
22 int pcep_stdout_logger(int priority, const char *format, va_list args)
23 PRINTFRR(2, 0);
24
25 static pcep_logger_func logger_func = pcep_stdout_logger;
26 static int logging_level_ = LOG_INFO;
27
28 void register_logger(pcep_logger_func logger)
29 {
30 logger_func = logger;
31 }
32
33 void set_logging_level(int level)
34 {
35 logging_level_ = level;
36 }
37
38 int get_logging_level(void)
39 {
40 return logging_level_;
41 }
42
43 void pcep_log(int priority, const char *format, ...)
44 {
45 va_list va;
46 va_start(va, format);
47 logger_func(priority, format, va);
48 va_end(va);
49 }
50
51 void pcep_log_hexbytes(int priority, const char *message, const uint8_t *bytes,
52 uint8_t bytes_len)
53 {
54 char byte_str[2048] = {0};
55 int i = 0;
56
57 snprintf(byte_str, 2048, "%s ", message);
58 for (; i < bytes_len; i++) {
59 snprintf(byte_str, 2048, "%02x ", bytes[i]);
60 }
61 snprintf(byte_str, 2048, "\n");
62
63 pcep_log(priority, "%s", byte_str);
64 }
65
66 /* Defined with a return type to match the FRR logging signature.
67 * Assuming glibc printf() is thread-safe. */
68 int pcep_stdout_logger(int priority, const char *format, va_list args)
69 {
70 if (priority <= logging_level_) {
71 vprintf(format, args);
72 printf("\n");
73 }
74
75 return 0;
76 }