blob: 66ffe7c8653a28b5221d089afd3a6a0c5e0e329f [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{
Michal Vaskofe5e8c92021-04-14 08:23:12 +020065#define PRV_MSG_INIT_SIZE 256
66 va_list args2;
67 char *prv_msg;
68 void *mem;
69 int req_len;
Radek Krejci5fe60cc2015-09-01 17:14:39 +020070
Michal Vaskofe5e8c92021-04-14 08:23:12 +020071 prv_msg = malloc(PRV_MSG_INIT_SIZE);
72 if (!prv_msg) {
73 return;
74 }
75
76 va_copy(args2, args);
77
78 req_len = vsnprintf(prv_msg, PRV_MSG_INIT_SIZE - 1, format, args);
79 if (req_len == -1) {
80 goto cleanup;
81 } else if (req_len >= PRV_MSG_INIT_SIZE - 1) {
82 /* the length is not enough */
83 ++req_len;
84 mem = realloc(prv_msg, req_len);
85 if (!mem) {
86 goto cleanup;
87 }
88 prv_msg = mem;
89
90 /* now print the full message */
91 req_len = vsnprintf(prv_msg, req_len, format, args2);
92 if (req_len == -1) {
93 goto cleanup;
94 }
95 }
Radek Krejci5fe60cc2015-09-01 17:14:39 +020096
Michal Vasko206d3b12015-12-04 11:08:42 +010097 if (print_clb) {
98 print_clb(level, prv_msg);
99 } else {
100 fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg);
101 }
Radek Krejci5fe60cc2015-09-01 17:14:39 +0200102
Michal Vaskofe5e8c92021-04-14 08:23:12 +0200103cleanup:
104 free(prv_msg);
105#undef PRV_MSG_INIT_SIZE
Radek Krejci5fe60cc2015-09-01 17:14:39 +0200106}
107
108void
109prv_printf(NC_VERB_LEVEL level, const char *format, ...)
110{
111 va_list ap;
112
113 va_start(ap, format);
114 prv_vprintf(level, format, ap);
115 va_end(ap);
116}
117
Michal Vaskofea54dc2016-02-17 13:12:16 +0100118static void
119nc_ly_log_clb(LY_LOG_LEVEL lvl, const char *msg, const char *UNUSED(path))
120{
Radek Krejci99087c72016-02-26 15:03:26 +0100121 print_clb((NC_VERB_LEVEL)lvl, msg);
Michal Vaskofea54dc2016-02-17 13:12:16 +0100122}
123
Radek Krejci5fe60cc2015-09-01 17:14:39 +0200124API void
Michal Vasko206d3b12015-12-04 11:08:42 +0100125nc_set_print_clb(void (*clb)(NC_VERB_LEVEL, const char *))
126{
127 print_clb = clb;
Michal Vasko6d8a9722020-02-10 14:56:40 +0100128 ly_set_log_clb(nc_ly_log_clb, 1);
Michal Vasko206d3b12015-12-04 11:08:42 +0100129}