blob: 3350b88850103b37d06eeb0ac0ae6f4b04eacb88 [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 Vasko7a20d2e2021-05-19 16:40:23 +020024#include "compat.h"
Michal Vasko1a38c862016-01-15 15:50:07 +010025#include "libnetconf.h"
Michal Vaskob078fee2016-09-29 11:30:31 +020026#include "log.h"
Radek Krejci5fe60cc2015-09-01 17:14:39 +020027
28/**
29 * @brief libnetconf verbose level variable
30 */
31volatile uint8_t verbose_level = 0;
32
Michal Vasko206d3b12015-12-04 11:08:42 +010033void (*print_clb)(NC_VERB_LEVEL level, const char *msg);
34
Radek Krejci5fe60cc2015-09-01 17:14:39 +020035API void
36nc_verbosity(NC_VERB_LEVEL level)
37{
38 verbose_level = level;
Michal Vasko77367452021-02-16 16:32:18 +010039 ly_log_level((LY_LOG_LEVEL)level);
Radek Krejci5fe60cc2015-09-01 17:14:39 +020040}
41
42struct {
43 NC_VERB_LEVEL level;
44 const char *label;
45} verb[] = {
bhartb186eba2018-11-12 15:35:09 -060046 {NC_VERB_ERROR, "[ERR]"},
47 {NC_VERB_WARNING, "[WRN]"},
48 {NC_VERB_VERBOSE, "[INF]"},
Michal Vasko2733aad2020-04-16 09:01:52 +020049 {NC_VERB_DEBUG, "[DBG]"},
50 {NC_VERB_DEBUG_LOWLVL, "[DBL]"}
Radek Krejci5fe60cc2015-09-01 17:14:39 +020051};
52
Michal Vaskob078fee2016-09-29 11:30:31 +020053#ifdef NC_ENABLED_SSH
54
55API void
56nc_libssh_thread_verbosity(int level)
57{
58 ssh_set_log_level(level);
59}
60
61#endif
62
Radek Krejci5fe60cc2015-09-01 17:14:39 +020063static void
64prv_vprintf(NC_VERB_LEVEL level, const char *format, va_list args)
65{
Michal Vaskofe5e8c92021-04-14 08:23:12 +020066#define PRV_MSG_INIT_SIZE 256
67 va_list args2;
68 char *prv_msg;
69 void *mem;
70 int req_len;
Radek Krejci5fe60cc2015-09-01 17:14:39 +020071
Michal Vaskofe5e8c92021-04-14 08:23:12 +020072 prv_msg = malloc(PRV_MSG_INIT_SIZE);
73 if (!prv_msg) {
74 return;
75 }
76
77 va_copy(args2, args);
78
79 req_len = vsnprintf(prv_msg, PRV_MSG_INIT_SIZE - 1, format, args);
80 if (req_len == -1) {
81 goto cleanup;
82 } else if (req_len >= PRV_MSG_INIT_SIZE - 1) {
83 /* the length is not enough */
84 ++req_len;
85 mem = realloc(prv_msg, req_len);
86 if (!mem) {
87 goto cleanup;
88 }
89 prv_msg = mem;
90
91 /* now print the full message */
92 req_len = vsnprintf(prv_msg, req_len, format, args2);
93 if (req_len == -1) {
94 goto cleanup;
95 }
96 }
Radek Krejci5fe60cc2015-09-01 17:14:39 +020097
Michal Vasko206d3b12015-12-04 11:08:42 +010098 if (print_clb) {
99 print_clb(level, prv_msg);
100 } else {
101 fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg);
102 }
Radek Krejci5fe60cc2015-09-01 17:14:39 +0200103
Michal Vaskofe5e8c92021-04-14 08:23:12 +0200104cleanup:
105 free(prv_msg);
106#undef PRV_MSG_INIT_SIZE
Radek Krejci5fe60cc2015-09-01 17:14:39 +0200107}
108
109void
110prv_printf(NC_VERB_LEVEL level, const char *format, ...)
111{
112 va_list ap;
113
114 va_start(ap, format);
115 prv_vprintf(level, format, ap);
116 va_end(ap);
117}
118
Michal Vaskofea54dc2016-02-17 13:12:16 +0100119static void
120nc_ly_log_clb(LY_LOG_LEVEL lvl, const char *msg, const char *UNUSED(path))
121{
Radek Krejci99087c72016-02-26 15:03:26 +0100122 print_clb((NC_VERB_LEVEL)lvl, msg);
Michal Vaskofea54dc2016-02-17 13:12:16 +0100123}
124
Radek Krejci5fe60cc2015-09-01 17:14:39 +0200125API void
Michal Vasko206d3b12015-12-04 11:08:42 +0100126nc_set_print_clb(void (*clb)(NC_VERB_LEVEL, const char *))
127{
128 print_clb = clb;
Michal Vasko6d8a9722020-02-10 14:56:40 +0100129 ly_set_log_clb(nc_ly_log_clb, 1);
Michal Vasko206d3b12015-12-04 11:08:42 +0100130}