Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * 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 Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <stdarg.h> |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | |
Michal Vasko | a601f5c | 2015-12-08 14:33:03 +0100 | [diff] [blame] | 18 | #include <libyang/libyang.h> |
| 19 | |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 20 | #ifdef NC_ENABLED_SSH |
| 21 | #include <libssh/libssh.h> |
| 22 | #endif |
| 23 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 24 | #include "compat.h" |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 25 | #include "libnetconf.h" |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 26 | #include "log.h" |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * @brief libnetconf verbose level variable |
| 30 | */ |
| 31 | volatile uint8_t verbose_level = 0; |
| 32 | |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 33 | void (*print_clb)(NC_VERB_LEVEL level, const char *msg); |
| 34 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 35 | API void |
| 36 | nc_verbosity(NC_VERB_LEVEL level) |
| 37 | { |
| 38 | verbose_level = level; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 39 | ly_log_level((LY_LOG_LEVEL)level); |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | struct { |
| 43 | NC_VERB_LEVEL level; |
| 44 | const char *label; |
| 45 | } verb[] = { |
bhart | b186eba | 2018-11-12 15:35:09 -0600 | [diff] [blame] | 46 | {NC_VERB_ERROR, "[ERR]"}, |
| 47 | {NC_VERB_WARNING, "[WRN]"}, |
| 48 | {NC_VERB_VERBOSE, "[INF]"}, |
Michal Vasko | 2733aad | 2020-04-16 09:01:52 +0200 | [diff] [blame] | 49 | {NC_VERB_DEBUG, "[DBG]"}, |
| 50 | {NC_VERB_DEBUG_LOWLVL, "[DBL]"} |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 53 | #ifdef NC_ENABLED_SSH |
| 54 | |
| 55 | API void |
| 56 | nc_libssh_thread_verbosity(int level) |
| 57 | { |
| 58 | ssh_set_log_level(level); |
| 59 | } |
| 60 | |
| 61 | #endif |
| 62 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 63 | static void |
| 64 | prv_vprintf(NC_VERB_LEVEL level, const char *format, va_list args) |
| 65 | { |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 66 | #define PRV_MSG_INIT_SIZE 256 |
| 67 | va_list args2; |
| 68 | char *prv_msg; |
| 69 | void *mem; |
| 70 | int req_len; |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 71 | |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 72 | 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 Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 97 | |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 98 | if (print_clb) { |
| 99 | print_clb(level, prv_msg); |
| 100 | } else { |
| 101 | fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg); |
| 102 | } |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 103 | |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 104 | cleanup: |
| 105 | free(prv_msg); |
| 106 | #undef PRV_MSG_INIT_SIZE |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void |
| 110 | prv_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 Vasko | fea54dc | 2016-02-17 13:12:16 +0100 | [diff] [blame] | 119 | static void |
| 120 | nc_ly_log_clb(LY_LOG_LEVEL lvl, const char *msg, const char *UNUSED(path)) |
| 121 | { |
Radek Krejci | 99087c7 | 2016-02-26 15:03:26 +0100 | [diff] [blame] | 122 | print_clb((NC_VERB_LEVEL)lvl, msg); |
Michal Vasko | fea54dc | 2016-02-17 13:12:16 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 125 | API void |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 126 | nc_set_print_clb(void (*clb)(NC_VERB_LEVEL, const char *)) |
| 127 | { |
| 128 | print_clb = clb; |
Michal Vasko | 6d8a972 | 2020-02-10 14:56:40 +0100 | [diff] [blame] | 129 | ly_set_log_clb(nc_ly_log_clb, 1); |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 130 | } |