blob: 73074c4e51180807d3ae37f6f4ee2bdeba726a90 [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 *
Michal Vasko05532772021-06-03 12:12:38 +02006 * Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
Radek Krejci5fe60cc2015-09-01 17:14:39 +02007 *
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 Vasko05532772021-06-03 12:12:38 +020033void (*depr_print_clb)(NC_VERB_LEVEL level, const char *msg);
34void (*print_clb)(const struct nc_session *session, NC_VERB_LEVEL level, const char *msg);
Michal Vasko206d3b12015-12-04 11:08:42 +010035
Radek Krejci5fe60cc2015-09-01 17:14:39 +020036API void
37nc_verbosity(NC_VERB_LEVEL level)
38{
39 verbose_level = level;
Michal Vasko77367452021-02-16 16:32:18 +010040 ly_log_level((LY_LOG_LEVEL)level);
Radek Krejci5fe60cc2015-09-01 17:14:39 +020041}
42
43struct {
44 NC_VERB_LEVEL level;
45 const char *label;
46} verb[] = {
bhartb186eba2018-11-12 15:35:09 -060047 {NC_VERB_ERROR, "[ERR]"},
48 {NC_VERB_WARNING, "[WRN]"},
49 {NC_VERB_VERBOSE, "[INF]"},
Michal Vasko2733aad2020-04-16 09:01:52 +020050 {NC_VERB_DEBUG, "[DBG]"},
51 {NC_VERB_DEBUG_LOWLVL, "[DBL]"}
Radek Krejci5fe60cc2015-09-01 17:14:39 +020052};
53
Michal Vaskob078fee2016-09-29 11:30:31 +020054#ifdef NC_ENABLED_SSH
55
56API void
57nc_libssh_thread_verbosity(int level)
58{
59 ssh_set_log_level(level);
60}
61
62#endif
63
Radek Krejci5fe60cc2015-09-01 17:14:39 +020064static void
Michal Vasko05532772021-06-03 12:12:38 +020065prv_vprintf(const struct nc_session *session, NC_VERB_LEVEL level, const char *format, va_list args)
Radek Krejci5fe60cc2015-09-01 17:14:39 +020066{
Michal Vaskofe5e8c92021-04-14 08:23:12 +020067#define PRV_MSG_INIT_SIZE 256
68 va_list args2;
69 char *prv_msg;
70 void *mem;
71 int req_len;
Radek Krejci5fe60cc2015-09-01 17:14:39 +020072
Michal Vaskofe5e8c92021-04-14 08:23:12 +020073 prv_msg = malloc(PRV_MSG_INIT_SIZE);
74 if (!prv_msg) {
75 return;
76 }
77
78 va_copy(args2, args);
79
80 req_len = vsnprintf(prv_msg, PRV_MSG_INIT_SIZE - 1, format, args);
81 if (req_len == -1) {
82 goto cleanup;
83 } else if (req_len >= PRV_MSG_INIT_SIZE - 1) {
84 /* the length is not enough */
85 ++req_len;
86 mem = realloc(prv_msg, req_len);
87 if (!mem) {
88 goto cleanup;
89 }
90 prv_msg = mem;
91
92 /* now print the full message */
93 req_len = vsnprintf(prv_msg, req_len, format, args2);
94 if (req_len == -1) {
95 goto cleanup;
96 }
97 }
Radek Krejci5fe60cc2015-09-01 17:14:39 +020098
Michal Vasko206d3b12015-12-04 11:08:42 +010099 if (print_clb) {
Michal Vasko05532772021-06-03 12:12:38 +0200100 print_clb(session, level, prv_msg);
101 } else if (depr_print_clb) {
102 depr_print_clb(level, prv_msg);
103 } else if (session && session->id) {
104 fprintf(stderr, "Session %u %s: %s\n", session->id, verb[level].label, prv_msg);
Michal Vasko206d3b12015-12-04 11:08:42 +0100105 } else {
106 fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg);
107 }
Radek Krejci5fe60cc2015-09-01 17:14:39 +0200108
Michal Vaskofe5e8c92021-04-14 08:23:12 +0200109cleanup:
110 free(prv_msg);
111#undef PRV_MSG_INIT_SIZE
Radek Krejci5fe60cc2015-09-01 17:14:39 +0200112}
113
114void
Michal Vasko05532772021-06-03 12:12:38 +0200115prv_printf(const struct nc_session *session, NC_VERB_LEVEL level, const char *format, ...)
Radek Krejci5fe60cc2015-09-01 17:14:39 +0200116{
117 va_list ap;
118
119 va_start(ap, format);
Michal Vasko05532772021-06-03 12:12:38 +0200120 prv_vprintf(session, level, format, ap);
Radek Krejci5fe60cc2015-09-01 17:14:39 +0200121 va_end(ap);
122}
123
Michal Vaskofea54dc2016-02-17 13:12:16 +0100124static void
125nc_ly_log_clb(LY_LOG_LEVEL lvl, const char *msg, const char *UNUSED(path))
126{
Michal Vasko05532772021-06-03 12:12:38 +0200127 if (print_clb) {
128 print_clb(NULL, (NC_VERB_LEVEL)lvl, msg);
129 } else if (depr_print_clb) {
130 depr_print_clb((NC_VERB_LEVEL)lvl, msg);
131 }
Michal Vaskofea54dc2016-02-17 13:12:16 +0100132}
133
Radek Krejci5fe60cc2015-09-01 17:14:39 +0200134API void
Michal Vasko206d3b12015-12-04 11:08:42 +0100135nc_set_print_clb(void (*clb)(NC_VERB_LEVEL, const char *))
136{
Michal Vasko05532772021-06-03 12:12:38 +0200137 print_clb = NULL;
138 depr_print_clb = clb;
139 ly_set_log_clb(nc_ly_log_clb, 1);
140}
141
142API void
143nc_set_print_clb_session(void (*clb)(const struct nc_session *, NC_VERB_LEVEL, const char *))
144{
Michal Vasko206d3b12015-12-04 11:08:42 +0100145 print_clb = clb;
Michal Vasko05532772021-06-03 12:12:38 +0200146 depr_print_clb = NULL;
Michal Vasko6d8a9722020-02-10 14:56:40 +0100147 ly_set_log_clb(nc_ly_log_clb, 1);
Michal Vasko206d3b12015-12-04 11:08:42 +0100148}