Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @file log.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libnetconf2 - log functions |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 5 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 6 | * @copyright |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2021 CESNET, z.s.p.o. |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 8 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
Michal Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 12 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 13 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdarg.h> |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | |
Michal Vasko | a601f5c | 2015-12-08 14:33:03 +0100 | [diff] [blame] | 19 | #include <libyang/libyang.h> |
| 20 | |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 21 | #ifdef NC_ENABLED_SSH |
| 22 | #include <libssh/libssh.h> |
| 23 | #endif |
| 24 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 25 | #include "compat.h" |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 26 | #include "libnetconf.h" |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 27 | #include "log.h" |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * @brief libnetconf verbose level variable |
| 31 | */ |
| 32 | volatile uint8_t verbose_level = 0; |
| 33 | |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 34 | void (*depr_print_clb)(NC_VERB_LEVEL level, const char *msg); |
| 35 | 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] | 36 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 37 | API void |
| 38 | nc_verbosity(NC_VERB_LEVEL level) |
| 39 | { |
| 40 | verbose_level = level; |
Michal Vasko | 7736745 | 2021-02-16 16:32:18 +0100 | [diff] [blame] | 41 | ly_log_level((LY_LOG_LEVEL)level); |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | struct { |
| 45 | NC_VERB_LEVEL level; |
| 46 | const char *label; |
| 47 | } verb[] = { |
bhart | b186eba | 2018-11-12 15:35:09 -0600 | [diff] [blame] | 48 | {NC_VERB_ERROR, "[ERR]"}, |
| 49 | {NC_VERB_WARNING, "[WRN]"}, |
| 50 | {NC_VERB_VERBOSE, "[INF]"}, |
Michal Vasko | 2733aad | 2020-04-16 09:01:52 +0200 | [diff] [blame] | 51 | {NC_VERB_DEBUG, "[DBG]"}, |
| 52 | {NC_VERB_DEBUG_LOWLVL, "[DBL]"} |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
Michal Vasko | b078fee | 2016-09-29 11:30:31 +0200 | [diff] [blame] | 55 | #ifdef NC_ENABLED_SSH |
| 56 | |
| 57 | API void |
| 58 | nc_libssh_thread_verbosity(int level) |
| 59 | { |
| 60 | ssh_set_log_level(level); |
| 61 | } |
| 62 | |
| 63 | #endif |
| 64 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 65 | static void |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 66 | 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] | 67 | { |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 68 | #define PRV_MSG_INIT_SIZE 256 |
| 69 | va_list args2; |
| 70 | char *prv_msg; |
| 71 | void *mem; |
| 72 | int req_len; |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 73 | |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 74 | prv_msg = malloc(PRV_MSG_INIT_SIZE); |
| 75 | if (!prv_msg) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | va_copy(args2, args); |
| 80 | |
| 81 | req_len = vsnprintf(prv_msg, PRV_MSG_INIT_SIZE - 1, format, args); |
| 82 | if (req_len == -1) { |
| 83 | goto cleanup; |
| 84 | } else if (req_len >= PRV_MSG_INIT_SIZE - 1) { |
| 85 | /* the length is not enough */ |
| 86 | ++req_len; |
| 87 | mem = realloc(prv_msg, req_len); |
| 88 | if (!mem) { |
| 89 | goto cleanup; |
| 90 | } |
| 91 | prv_msg = mem; |
| 92 | |
| 93 | /* now print the full message */ |
| 94 | req_len = vsnprintf(prv_msg, req_len, format, args2); |
| 95 | if (req_len == -1) { |
| 96 | goto cleanup; |
| 97 | } |
| 98 | } |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 99 | |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 100 | if (print_clb) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 101 | print_clb(session, level, prv_msg); |
| 102 | } else if (depr_print_clb) { |
| 103 | depr_print_clb(level, prv_msg); |
| 104 | } else if (session && session->id) { |
| 105 | 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] | 106 | } else { |
| 107 | fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg); |
| 108 | } |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 109 | |
Michal Vasko | fe5e8c9 | 2021-04-14 08:23:12 +0200 | [diff] [blame] | 110 | cleanup: |
| 111 | free(prv_msg); |
| 112 | #undef PRV_MSG_INIT_SIZE |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 116 | 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] | 117 | { |
| 118 | va_list ap; |
| 119 | |
| 120 | va_start(ap, format); |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 121 | prv_vprintf(session, level, format, ap); |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 122 | va_end(ap); |
| 123 | } |
| 124 | |
Michal Vasko | fea54dc | 2016-02-17 13:12:16 +0100 | [diff] [blame] | 125 | static void |
| 126 | nc_ly_log_clb(LY_LOG_LEVEL lvl, const char *msg, const char *UNUSED(path)) |
| 127 | { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 128 | if (print_clb) { |
| 129 | print_clb(NULL, (NC_VERB_LEVEL)lvl, msg); |
| 130 | } else if (depr_print_clb) { |
| 131 | depr_print_clb((NC_VERB_LEVEL)lvl, msg); |
| 132 | } |
Michal Vasko | fea54dc | 2016-02-17 13:12:16 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Radek Krejci | 5fe60cc | 2015-09-01 17:14:39 +0200 | [diff] [blame] | 135 | API void |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 136 | nc_set_print_clb(void (*clb)(NC_VERB_LEVEL, const char *)) |
| 137 | { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 138 | print_clb = NULL; |
| 139 | depr_print_clb = clb; |
| 140 | ly_set_log_clb(nc_ly_log_clb, 1); |
| 141 | } |
| 142 | |
| 143 | API void |
| 144 | nc_set_print_clb_session(void (*clb)(const struct nc_session *, NC_VERB_LEVEL, const char *)) |
| 145 | { |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 146 | print_clb = clb; |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 147 | depr_print_clb = NULL; |
Michal Vasko | 6d8a972 | 2020-02-10 14:56:40 +0100 | [diff] [blame] | 148 | ly_set_log_clb(nc_ly_log_clb, 1); |
Michal Vasko | 206d3b1 | 2015-12-04 11:08:42 +0100 | [diff] [blame] | 149 | } |