]> git.proxmox.com Git - mirror_frr.git/blame - pceplib/pcep_utils_logging.c
Merge pull request #12805 from karlquan/kquan_self_orig
[mirror_frr.git] / pceplib / pcep_utils_logging.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: LGPL-2.1-or-later
74971473
JG
2/*
3 * This file is part of the PCEPlib, a PCEP protocol library.
4 *
5 * Copyright (C) 2020 Volta Networks https://voltanet.io/
6 *
74971473
JG
7 * Author : Brady Johnson <brady@voltanet.io>
8 *
9 */
10
11
1f8031f7
DL
12#ifdef HAVE_CONFIG_H
13#include "config.h"
14#endif
15
74971473
JG
16#include <stdarg.h>
17#include <stdio.h>
0f9de11a 18#include "compiler.h"
74971473
JG
19#include "pcep_utils_logging.h"
20
21/* Forward declaration */
0f9de11a
DL
22int pcep_stdout_logger(int priority, const char *format, va_list args)
23 PRINTFRR(2, 0);
74971473
JG
24
25static pcep_logger_func logger_func = pcep_stdout_logger;
26static int logging_level_ = LOG_INFO;
27
28void register_logger(pcep_logger_func logger)
29{
30 logger_func = logger;
31}
32
33void set_logging_level(int level)
34{
35 logging_level_ = level;
36}
37
2816045a 38int get_logging_level(void)
74971473
JG
39{
40 return logging_level_;
41}
42
43void 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
51void 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. */
68int 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}