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 | * |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2021 CESNET, z.s.p.o. |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 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 | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 33 | void (*depr_print_clb)(NC_VERB_LEVEL level, const char *msg); |
| 34 | void (*print_clb)(const struct nc_session *session, NC_VERB_LEVEL level, const char *msg); |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 35 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 36 | API void |
| 37 | nc_verbosity(NC_VERB_LEVEL level) |
| 38 | { |
| 39 | verbose_level = level; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 40 | ly_log_level((LY_LOG_LEVEL)level); |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | struct { |
| 44 | NC_VERB_LEVEL level; |
| 45 | const char *label; |
| 46 | } verb[] = { |
bhart | b186eba | 2018-11-12 15:35:09 -0600 | [diff] [blame] | 47 | {NC_VERB_ERROR, "[ERR]"}, |
| 48 | {NC_VERB_WARNING, "[WRN]"}, |
| 49 | {NC_VERB_VERBOSE, "[INF]"}, |
Michal Vasko | 2733aad | 2020-04-16 09:01:52 +0200 | [diff] [blame] | 50 | {NC_VERB_DEBUG, "[DBG]"}, |
| 51 | {NC_VERB_DEBUG_LOWLVL, "[DBL]"} |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 54 | #ifdef NC_ENABLED_SSH |
| 55 | |
| 56 | API void |
| 57 | nc_libssh_thread_verbosity(int level) |
| 58 | { |
| 59 | ssh_set_log_level(level); |
| 60 | } |
| 61 | |
| 62 | #endif |
| 63 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 64 | static void |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 65 | prv_vprintf(const struct nc_session *session, NC_VERB_LEVEL level, const char *format, va_list args) |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 66 | { |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 67 | #define PRV_MSG_INIT_SIZE 256 |
| 68 | va_list args2; |
| 69 | char *prv_msg; |
| 70 | void *mem; |
| 71 | int req_len; |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 72 | |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 73 | 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 Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 98 | |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 99 | if (print_clb) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 100 | 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 Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 105 | } else { |
| 106 | fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg); |
| 107 | } |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 108 | |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 109 | cleanup: |
| 110 | free(prv_msg); |
| 111 | #undef PRV_MSG_INIT_SIZE |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 115 | prv_printf(const struct nc_session *session, NC_VERB_LEVEL level, const char *format, ...) |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 116 | { |
| 117 | va_list ap; |
| 118 | |
| 119 | va_start(ap, format); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 120 | prv_vprintf(session, level, format, ap); |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 121 | va_end(ap); |
| 122 | } |
| 123 | |
Michal Vasko | fea54dc | 2016-02-17 13:12:16 +0100 | [diff] [blame] | 124 | static void |
| 125 | nc_ly_log_clb(LY_LOG_LEVEL lvl, const char *msg, const char *UNUSED(path)) |
| 126 | { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 127 | 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 Vasko | fea54dc | 2016-02-17 13:12:16 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 134 | API void |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 135 | nc_set_print_clb(void (*clb)(NC_VERB_LEVEL, const char *)) |
| 136 | { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 137 | print_clb = NULL; |
| 138 | depr_print_clb = clb; |
| 139 | ly_set_log_clb(nc_ly_log_clb, 1); |
| 140 | } |
| 141 | |
| 142 | API void |
| 143 | nc_set_print_clb_session(void (*clb)(const struct nc_session *, NC_VERB_LEVEL, const char *)) |
| 144 | { |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 145 | print_clb = clb; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 146 | depr_print_clb = NULL; |
Michal Vasko | 6d8a972 | 2020-02-10 14:56:40 +0100 | [diff] [blame] | 147 | ly_set_log_clb(nc_ly_log_clb, 1); |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 148 | } |