blob: 57ca05cc87e1d06629e8e03a9244025883d6031d [file] [log] [blame]
Radek Krejci5fe60cc2015-09-01 17:14:39 +02001/**
2 * \file log.c
3 * \author Radek Krejci <rkrejci@cesnet.cz>
4 * \brief libnetconf2 - log functions
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci5fe60cc2015-09-01 17:14:39 +020013 */
14
15#include <stdarg.h>
Radek Krejci5fe60cc2015-09-01 17:14:39 +020016#include <stdio.h>
17
Michal Vaskoa601f5c2015-12-08 14:33:03 +010018#include <libyang/libyang.h>
19
Michal Vaskob078fee2016-09-29 11:30:31 +020020#ifdef NC_ENABLED_SSH
21 #include <libssh/libssh.h>
22#endif
23
Michal Vasko1a38c862016-01-15 15:50:07 +010024#include "libnetconf.h"
Michal Vaskob078fee2016-09-29 11:30:31 +020025#include "log.h"
Radek Krejci5fe60cc2015-09-01 17:14:39 +020026
27/**
28 * @brief libnetconf verbose level variable
29 */
30volatile uint8_t verbose_level = 0;
31
Michal Vasko206d3b12015-12-04 11:08:42 +010032void (*print_clb)(NC_VERB_LEVEL level, const char *msg);
33
Radek Krejci5fe60cc2015-09-01 17:14:39 +020034API void
35nc_verbosity(NC_VERB_LEVEL level)
36{
37 verbose_level = level;
Michal Vaskocefedf22016-01-20 10:08:12 +010038 ly_verb((LY_LOG_LEVEL)level);
Radek Krejci5fe60cc2015-09-01 17:14:39 +020039}
40
41struct {
42 NC_VERB_LEVEL level;
43 const char *label;
44} verb[] = {
bhartb186eba2018-11-12 15:35:09 -060045 {NC_VERB_ERROR, "[ERR]"},
46 {NC_VERB_WARNING, "[WRN]"},
47 {NC_VERB_VERBOSE, "[INF]"},
Michal Vasko2733aad2020-04-16 09:01:52 +020048 {NC_VERB_DEBUG, "[DBG]"},
49 {NC_VERB_DEBUG_LOWLVL, "[DBL]"}
Radek Krejci5fe60cc2015-09-01 17:14:39 +020050};
51
Michal Vaskob078fee2016-09-29 11:30:31 +020052#ifdef NC_ENABLED_SSH
53
54API void
55nc_libssh_thread_verbosity(int level)
56{
57 ssh_set_log_level(level);
58}
59
60#endif
61
Radek Krejci5fe60cc2015-09-01 17:14:39 +020062static void
63prv_vprintf(NC_VERB_LEVEL level, const char *format, va_list args)
64{
65#define PRV_MSG_SIZE 4096
66 char prv_msg[PRV_MSG_SIZE];
67
68 vsnprintf(prv_msg, PRV_MSG_SIZE - 1, format, args);
69 prv_msg[PRV_MSG_SIZE - 1] = '\0';
70
Michal Vasko206d3b12015-12-04 11:08:42 +010071 if (print_clb) {
72 print_clb(level, prv_msg);
73 } else {
74 fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg);
75 }
Radek Krejci5fe60cc2015-09-01 17:14:39 +020076
77#undef PRV_MSG_SIZE
78}
79
80void
81prv_printf(NC_VERB_LEVEL level, const char *format, ...)
82{
83 va_list ap;
84
85 va_start(ap, format);
86 prv_vprintf(level, format, ap);
87 va_end(ap);
88}
89
Michal Vaskofea54dc2016-02-17 13:12:16 +010090static void
91nc_ly_log_clb(LY_LOG_LEVEL lvl, const char *msg, const char *UNUSED(path))
92{
Radek Krejci99087c72016-02-26 15:03:26 +010093 print_clb((NC_VERB_LEVEL)lvl, msg);
Michal Vaskofea54dc2016-02-17 13:12:16 +010094}
95
Radek Krejci5fe60cc2015-09-01 17:14:39 +020096API void
Michal Vasko206d3b12015-12-04 11:08:42 +010097nc_set_print_clb(void (*clb)(NC_VERB_LEVEL, const char *))
98{
99 print_clb = clb;
Michal Vasko6d8a9722020-02-10 14:56:40 +0100100 ly_set_log_clb(nc_ly_log_clb, 1);
Michal Vasko206d3b12015-12-04 11:08:42 +0100101}