blob: e1c3988c72d307808e454c85a25b02a30de02484 [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 Vasko1a38c862016-01-15 15:50:07 +010020#include "libnetconf.h"
Radek Krejci5fe60cc2015-09-01 17:14:39 +020021
22/**
23 * @brief libnetconf verbose level variable
24 */
25volatile uint8_t verbose_level = 0;
26
Michal Vasko206d3b12015-12-04 11:08:42 +010027void (*print_clb)(NC_VERB_LEVEL level, const char *msg);
28
Radek Krejci5fe60cc2015-09-01 17:14:39 +020029API void
30nc_verbosity(NC_VERB_LEVEL level)
31{
32 verbose_level = level;
Michal Vaskocefedf22016-01-20 10:08:12 +010033 ly_verb((LY_LOG_LEVEL)level);
Radek Krejci53691be2016-02-22 13:58:37 +010034#ifdef NC_ENABLED_SSH
Michal Vaskoc6356ea2016-02-03 15:29:09 +010035 ssh_set_log_level(level);
36#endif
Radek Krejci5fe60cc2015-09-01 17:14:39 +020037}
38
39struct {
40 NC_VERB_LEVEL level;
41 const char *label;
42} verb[] = {
43 {NC_VERB_ERROR, "ERROR"},
44 {NC_VERB_WARNING, "WARNING"},
45 {NC_VERB_VERBOSE, "VERBOSE"},
46 {NC_VERB_DEBUG, "DEBUG"}
47};
48
49static void
50prv_vprintf(NC_VERB_LEVEL level, const char *format, va_list args)
51{
52#define PRV_MSG_SIZE 4096
53 char prv_msg[PRV_MSG_SIZE];
54
55 vsnprintf(prv_msg, PRV_MSG_SIZE - 1, format, args);
56 prv_msg[PRV_MSG_SIZE - 1] = '\0';
57
Michal Vasko206d3b12015-12-04 11:08:42 +010058 if (print_clb) {
59 print_clb(level, prv_msg);
60 } else {
61 fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg);
62 }
Radek Krejci5fe60cc2015-09-01 17:14:39 +020063
64#undef PRV_MSG_SIZE
65}
66
67void
68prv_printf(NC_VERB_LEVEL level, const char *format, ...)
69{
70 va_list ap;
71
72 va_start(ap, format);
73 prv_vprintf(level, format, ap);
74 va_end(ap);
75}
76
Michal Vaskofea54dc2016-02-17 13:12:16 +010077static void
78nc_ly_log_clb(LY_LOG_LEVEL lvl, const char *msg, const char *UNUSED(path))
79{
Radek Krejci99087c72016-02-26 15:03:26 +010080 print_clb((NC_VERB_LEVEL)lvl, msg);
Michal Vaskofea54dc2016-02-17 13:12:16 +010081}
82
Radek Krejci5fe60cc2015-09-01 17:14:39 +020083API void
Michal Vasko206d3b12015-12-04 11:08:42 +010084nc_set_print_clb(void (*clb)(NC_VERB_LEVEL, const char *))
85{
86 print_clb = clb;
Michal Vaskofea54dc2016-02-17 13:12:16 +010087 ly_set_log_clb(nc_ly_log_clb, 0);
Michal Vasko206d3b12015-12-04 11:08:42 +010088}